Module: EM::Sofa::Mapping::InstanceMethods

Defined in:
lib/em-sofa/mapping.rb

Instance Method Summary collapse

Instance Method Details

#==(obj) ⇒ Boolean

Compares two objects based on their attributes.

Parameters:

  • obj (Object)

    The object to compare with

Returns:

  • (Boolean)

    Whether all attributes are the same



48
49
50
51
52
53
54
55
56
57
# File 'lib/em-sofa/mapping.rb', line 48

def ==(obj)
  if (klass = self.class) == obj.class
    klass.mappings.inject(true) do |result, to_from|
      to = to_from.last || to_from.first
      result &&= (obj.send(to) == self.send(to))
    end
  else
    super
  end
end

#attributesHash<Symbol, Object>

Returns hash of all the attributes, with attribute names as keys and attribute values as values.

Returns:

  • (Hash<Symbol, Object>)

    Hash of all attributes

See Also:

  • EM:Sofa::Mapping::ClassMethods#maps


20
21
22
23
24
25
26
27
# File 'lib/em-sofa/mapping.rb', line 20

def attributes
  klass = self.class
  klass.mappings.values.inject({}) do |attrs, to|
    next unless to
    attrs[to] = [:season_list, :episode_list].include?(to) ? method(to) : send(to)
    attrs
  end
end

#update_with_mapping(attributes = {}) ⇒ Object

Updates all the attributes from the Hash.

Parameters:

  • attributes (Hash<Symbol, Object>) (defaults to: {})

    A hash with attribute names as keys and attribute values as values

See Also:

  • EM:Sofa::Mapping::ClassMethods#maps


33
34
35
36
37
38
39
40
41
42
# File 'lib/em-sofa/mapping.rb', line 33

def update_with_mapping(attributes = {})
  attributes.each do |key, value|
    next unless (klass = self.class) and key.respond_to?(:to_sym) and to = klass.mappings[key.to_sym]

    block = klass.mappings_procs[to]
    value = block.call(value) if block

    instance_variable_set("@#{to}", value)
  end
end