Module: Sofa::Mapping::InstanceMethods

Defined in:
lib/sofa/mapping.rb

Method Summary

Method Details

- (Boolean) ==(obj)

Compares two objects based on their attributes.

Parameters:

  • (Object) obj — 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/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

- (Hash<Symbol, Object>) attributes

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:



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

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

- (Object) update_with_mapping(attributes = {})

Updates all the attributes from the Hash.

Parameters:

  • (Hash<Symbol, Object>) attributes (defaults to: {}) — A hash with attribute names as keys and attribute values as values

See Also:



33
34
35
36
37
38
39
40
41
42
# File 'lib/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