Class: Zermelo::Associations::HasMany
- Inherits:
-
Object
- Object
- Zermelo::Associations::HasMany
- Extended by:
- Forwardable
- Defined in:
- lib/zermelo/associations/has_many.rb
Instance Method Summary collapse
- #<<(record) ⇒ Object
- #add(*records) ⇒ Object
-
#delete(*records) ⇒ Object
TODO support dependent delete, for now just deletes the association.
-
#initialize(parent, name) ⇒ HasMany
constructor
A new instance of HasMany.
Constructor Details
#initialize(parent, name) ⇒ HasMany
Returns a new instance of HasMany.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/zermelo/associations/has_many.rb', line 16 def initialize(parent, name) @parent = parent @backend = parent.send(:backend) @record_ids_key = Zermelo::Records::Key.new( :klass => parent.class.send(:class_key), :id => parent.id, :name => "#{name}_ids", :type => :set, :object => :association ) parent.class.send(:with_association_data, name.to_sym) do |data| @associated_class = data.data_klass @lock_klasses = [data.data_klass] + data. @inverse = data.inverse @callbacks = data.callbacks end end |
Instance Method Details
#<<(record) ⇒ Object
37 38 39 40 |
# File 'lib/zermelo/associations/has_many.rb', line 37 def <<(record) add(record) self # for << 'a' << 'b' end |
#add(*records) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/zermelo/associations/has_many.rb', line 42 def add(*records) raise 'No records to add' if records.empty? raise 'Invalid record class' if records.any? {|r| !r.is_a?(@associated_class)} raise 'Record(s) must have been saved' unless records.all? {|r| r.persisted?} # may need to be moved @parent.class.lock(*@lock_klasses) do ba = @callbacks[:before_add] if ba.nil? || !@parent.respond_to?(ba) || !@parent.send(ba, *records).is_a?(FalseClass) unless @inverse.nil? records.each do |record| @associated_class.send(:load, record.id).send("#{@inverse}=", @parent) end end new_txn = @backend.begin_transaction @backend.add(@record_ids_key, records.map(&:id)) @backend.commit_transaction if new_txn aa = @callbacks[:after_add] @parent.send(aa, *records) if !aa.nil? && @parent.respond_to?(aa) end end end |
#delete(*records) ⇒ Object
TODO support dependent delete, for now just deletes the association
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/zermelo/associations/has_many.rb', line 65 def delete(*records) raise 'No records to delete' if records.empty? raise 'Invalid record class' if records.any? {|r| !r.is_a?(@associated_class)} raise 'Record(s) must have been saved' unless records.all? {|r| r.persisted?} # may need to be moved @parent.class.lock(*@lock_klasses) do br = @callbacks[:before_remove] if br.nil? || !@parent.respond_to?(br) || !@parent.send(br, *records).is_a?(FalseClass) unless @inverse.nil? records.each do |record| @associated_class.send(:load, record.id).send("#{@inverse}=", nil) end end new_txn = @backend.begin_transaction @backend.delete(@record_ids_key, records.map(&:id)) @backend.commit_transaction if new_txn ar = @callbacks[:after_remove] @parent.send(ar, *records) if !ar.nil? && @parent.respond_to?(ar) end end end |