class SuperMapper
Constants
- VERSION
Public Class Methods
new() { |self| ... }
click to toggle source
# File lib/super_mapper.rb, line 6 def initialize yield self if block_given? end
Public Instance Methods
define_mapping(source_class, target_class, &block)
click to toggle source
Define a new mapping schema from a source class, mapping getters to setters
@param [Class] source_class the class that will act as the source for this mapping definition
# File lib/super_mapper.rb, line 14 def define_mapping source_class, target_class, &block mapping_registry["#{source_class}-#{target_class}"] = block end
map(source, target)
click to toggle source
Maps a source object to a target object using previously registered mappings
@param [Object] source the source object @param [Object | Class] target the target object or class. If a class is given, it should have a no-args constructor since the new instance will be created calling target.new
@return [Object] the target object, or a new instance of the target class, filled with values coming from the source
# File lib/super_mapper.rb, line 24 def map source, target return if source.nil? raise ArgumentError, 'target cannot be nil' if target.nil? target.is_a?(Class) ? map_object(source, target.new) : map_object(source, target) end
Private Instance Methods
map_object(source, target)
click to toggle source
# File lib/super_mapper.rb, line 38 def map_object source, target mapping_proc = mapping_registry["#{source.class}-#{target.class}"] mapping_proc.call source, target target end
mapping_registry()
click to toggle source
# File lib/super_mapper.rb, line 34 def mapping_registry @mapping_registry ||= {} end