Module: Sofa::Mapping::ClassMethods

Defined in:
lib/sofa/mapping.rb

Instance Method Summary collapse

Instance Method Details

#mappingsHash<Symbol, Symbol>

Returns 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 #:nodoc:
  @mappings
end

#mappings_procsHash<Symbol, Proc>

Returns 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 #:nodoc:
  @mappings_procs
end

#maps(hash = {}, &block) ⇒ Object

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