Module: Sofa::Mapping::ClassMethods

Defined in:
lib/sofa/mapping.rb

Method Summary

Method Details

- (Hash<Symbol, Symbol>) mappings

Mapping of symbols to attribute names

Returns:

  • (Hash<Symbol, Symbol>) — Mapping of symbols to attribute names


96
97
98
# File 'lib/sofa/mapping.rb', line 96

def mappings 
  @mappings
end

- (Hash<Symbol, Proc>) mappings_procs

Mapping of attribute names to a Proc filter.

Returns:

  • (Hash<Symbol, Proc>) — Mapping of attribute names to a Proc filter.

See Also:



103
104
105
# File 'lib/sofa/mapping.rb', line 103

def mappings_procs 
  @mappings_procs
end

- (Object) maps(hash = {}, &block)

Class method to define mappings.

Examples:

Maps :examplename to :name and :link to itself.

  class Example1
    include Sofa::Mapping
    maps(
      :examplename => :name,
      :link        => nil
    )
  end
  example1 = Example1.new.update_with_mapping(:epnum => "1", :link => "http://google.com")
  example1.name       # => "1"
  example1.link       # => "http://google.com"
  example1.attributes # => {:name => "1", :link => "http://google.com"}

Maps :airdate to :air_date and stores the block.

  class Example2
    include Sofa::Mapping

    maps(:airdate => :air_date) do |value|
      Date.parse(value)
    end
  end
  example2 = Example2.new.update_with_mapping(:airdate => "2007-07-04")
  example2.attributes # => {:air_date => Wed, 04 Jul 2007}


85
86
87
88
89
90
91
92
# File 'lib/sofa/mapping.rb', line 85

def maps(hash = {}, &block)
  hash.each do |from, to|
    method = to || from
    @mappings[from.to_sym] = method
    attr_reader method
    @mappings_procs[method] = block
  end
end