Class: Fauxsql::ManymanyWrapper

Inherits:
AttributeWrapper show all
Defined in:
lib/fauxsql/manymany_wrapper.rb

Defined Under Namespace

Classes: InvalidManymanyAssociationClass, ThroughOptionMissing

Instance Attribute Summary

Attributes inherited from AttributeWrapper

#attribute, #name, #options, #record

Instance Method Summary collapse

Methods inherited from AttributeWrapper

#dirty!, #get_nested_record

Constructor Details

#initialize(attribute, record, name, *classes) ⇒ ManymanyWrapper

Returns a new instance of ManymanyWrapper.



11
12
13
14
15
16
# File 'lib/fauxsql/manymany_wrapper.rb', line 11

def initialize(attribute, record, name, *classes)
  super(attribute, record, name, {})
  options = classes.extract_options!
  raise ThroughOptionMissing unless options[:through]
  @classes, @through = classes, options[:through]
end

Instance Method Details

#<<(other) ⇒ Object



18
19
20
21
22
# File 'lib/fauxsql/manymany_wrapper.rb', line 18

def <<(other)
  raise InvalidManymanyAssociationClass unless @classes.include?(other.class)
  other.send(@through).clean_push(record)
  clean_push(other)
end

#delete(*others) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/fauxsql/manymany_wrapper.rb', line 24

def delete(*others)
  raise InvalidManymanyAssociationClass if (@classes - others.map{|other| other.class }).any?
  clean_subtract(others)
  others.each{ |other| other.send(@through).clean_subtract([record]) }
  # # DataMapper.transaction do # TODO the transaction?
    others.each{|other| other.save}
    record.save
  # end
end