Module: SimpleMapper::Collection::CommonMethods

Included in:
Array, Hash
Defined in:
lib/simple_mapper/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



5
6
7
# File 'lib/simple_mapper/collection.rb', line 5

def attribute
  @attribute
end

#change_trackingObject

Returns the value of attribute change_tracking.



6
7
8
# File 'lib/simple_mapper/collection.rb', line 6

def change_tracking
  @change_tracking
end

Instance Method Details

#[]=(key, value) ⇒ Object



36
37
38
39
# File 'lib/simple_mapper/collection.rb', line 36

def []=(key, value)
  member_changed!(key, value)
  super(key, value)
end

#build(*args) ⇒ Object



41
42
43
# File 'lib/simple_mapper/collection.rb', line 41

def build(*args)
  attribute.mapper.new(*args)
end

#changed_membersObject



12
13
14
# File 'lib/simple_mapper/collection.rb', line 12

def changed_members
  simple_mapper_changes.keys
end

#is_member?(key) ⇒ Boolean

Predicate that returns true if key is present in the collection. Returns nil by default; this must be implemented appropriately per class that uses this module.

Returns:

  • (Boolean)


32
33
34
# File 'lib/simple_mapper/collection.rb', line 32

def is_member?(key)
  nil
end

#member_changed!(key, value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/simple_mapper/collection.rb', line 16

def member_changed!(key, value)
  return nil unless change_tracking
  # If the key is new to the collection, we're fine.
  # If the key is already in the collection, then we have to consider
  # whether or not value is itself a mapper.  If it is, we want it to consider
  # all of its attributes changed, since they are all replacing whatever was
  # previously associated with +key+ in the collection.
  if ! value.nil? and value.respond_to?(:all_changed!)
    value.all_changed!
  end
  simple_mapper_changes[key] = true
end

#simple_mapper_changesObject



8
9
10
# File 'lib/simple_mapper/collection.rb', line 8

def simple_mapper_changes
  @simple_mapper_changes ||= SimpleMapper::ChangeHash.new
end