Monkeybars::MethodMapping

Public Class Methods

new(mapping_properties) click to toggle source
# File lib/monkeybars/view_mapping.rb, line 222
def initialize(mapping_properties)
  super
  if using_translation?
    @to_view_translation = @data_translation_hash
    @from_view_translation = @data_translation_hash.invert
  end
end

Public Instance Methods

model_from_view(view, model) click to toggle source
# File lib/monkeybars/view_mapping.rb, line 277
def model_from_view(view, model)
  if using_translation?
    view_data = instance_eval("view.#{@view_property}", __FILE__, __LINE__)
    unless @from_view_translation.has_key? view_data
      raise TranslationError, "The key #{view_data.inspect} for view #{view.class} does not exist #{@from_view_translation.inspect}"
    end
    instance_eval("model.#{@model_property} = @from_view_translation[view_data]", __FILE__, __LINE__)
  elsif :default == @from_view_method
    super
  else
    instance_eval("model.#{@model_property} = view.method(@from_view_method).call(view.#{@view_property})", __FILE__, __LINE__)
  end
rescue NameError => e
  raise InvalidMappingError, "Either model.#{@model_property} or view.#{@from_view_method}(#{@view_property}) is not valid.\nOriginal error: #{e}\n#{e.message}"
rescue SyntaxError, Exception => e
  raise InvalidMappingError, "Error while trying to map for #{view.class}: #{to_s}\n#{e.class} - #{e}", e.backtrace
end
model_to_view(view, model) click to toggle source
# File lib/monkeybars/view_mapping.rb, line 240
def model_to_view(view, model)
  if using_translation?
    model_data = instance_eval("model.#{@model_property}", __FILE__, __LINE__)
    unless @to_view_translation.has_key? model_data
      raise TranslationError, "The key #{model_data.inspect} for model #{model.class} does not exist #{@to_view_translation.inspect}"
    end
    
    instance_eval("view.#{@view_property} = @to_view_translation[model_data]", __FILE__, __LINE__)
  elsif :default == @to_view_method
    super
  else
    instance_eval("view.#{@view_property} = view.method(@to_view_method).call(model.#{@model_property})", __FILE__, __LINE__)
  end
rescue NameError => e
  raise InvalidMappingError, "Either view.#{@view_property} or view.#{@to_view_method}(#{@model_property}) is not valid.\nOriginal error: #{e}\n#{e.message}"
rescue SyntaxError, Exception => e
  raise InvalidMappingError, "Error while trying to map for #{view.class}: #{to_s}\n#{e.class} - #{e}"
end
to_s() click to toggle source
# File lib/monkeybars/view_mapping.rb, line 230
def to_s
  using_key = using_translation? ? ':using_translation' : ':using'
  using_value = using_translation? ? @data_translation_hash.inspect : "[#{@to_view_method.inspect}, #{@from_view_method.inspect}]"
  super << " #{using_key} => #{using_value}"
end
transfer_from_view(view, transfer) click to toggle source
# File lib/monkeybars/view_mapping.rb, line 295
def transfer_from_view(view, transfer)
  if using_translation?
    view_data = instance_eval("view.#{@view_property}", __FILE__, __LINE__)
    unless @from_view_translation.has_key? view_data
      raise TranslationError, "The key #{view_data.inspect} for view #{view.class} does not exist #{@from_view_translation.inspect}"
    end
    instance_eval("transfer[#{@transfer_property.inspect}] = @from_view_translation[view_data]", __FILE__, __LINE__)
  elsif :default == @from_view_method
    super
  else
    instance_eval("transfer[#{@transfer_property.inspect}] = view.method(@from_view_method).call(view.#{@view_property})", __FILE__, __LINE__)
  end
rescue NameError => e
  raise InvalidMappingError, "Either transfer[#{@transfer_property}] or view.#{@from_view_method}(#{@view_property}) is not valid.\nOriginal error: #{e}\n#{e.message}"
rescue SyntaxError, Exception => e
  raise InvalidMappingError, "Error while trying to map for #{view.class}: #{to_s}\n#{e.class} - #{e}"
end
transfer_to_view(view, transfer) click to toggle source
# File lib/monkeybars/view_mapping.rb, line 259
def transfer_to_view(view, transfer)
  if using_translation?
    transfer_data = instance_eval("transfer[#{@transfer_property.inspect}]", __FILE__, __LINE__)
    unless @to_view_translation.has_key? transfer_data
      raise TranslationError, "The key #{transfer_data.inspect} for transfer does not exist #{@to_view_translation.inspect}"
    end
    instance_eval("view.#{@view_property} = @to_view_translation[transfer_data]", __FILE__, __LINE__)
  elsif :default == @to_view_method
    super
  else
    instance_eval("view.#{@view_property} = view.method(@to_view_method).call(transfer[#{@transfer_property.inspect}])", __FILE__, __LINE__)
  end
rescue NameError => e
  raise InvalidMappingError, "Either view.#{@view_property} or view.#{@to_view_method}(#{@transfer_property}) is not valid.\nOriginal error: #{e}\n#{e.message}"
rescue SyntaxError, Exception => e
  raise InvalidMappingError, "Error while trying to map for #{view.class}: #{to_s}\n#{e.class} - #{e}"
end
using_translation?() click to toggle source
# File lib/monkeybars/view_mapping.rb, line 236
def using_translation?
  !@data_translation_hash.nil?
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.