Module: Replicate::AR::InstanceMethods
- Defined in:
- lib/replicate/active_record.rb
Overview
Mixin for the ActiveRecord instance.
Instance Method Summary collapse
-
#dump_all_association_replicants(dumper, association_type) ⇒ Object
Dump all associations of a given type.
-
#dump_association_replicants(dumper, association) ⇒ Object
Dump objects associated with an AR object through an association name.
-
#dump_has_and_belongs_to_many_replicant(dumper, reflection) ⇒ Object
Dump the special Habtm object used to establish many-to-many relationships between objects that have already been dumped.
-
#dump_replicant(dumper) ⇒ Object
Replicate::Dumper calls this method on objects to trigger dumping a replicant object tuple.
-
#replicant_attributes ⇒ Object
Attributes hash used to persist this object.
-
#replicant_id ⇒ Object
The replicant id is a two tuple containing the class and object id.
Instance Method Details
#dump_all_association_replicants(dumper, association_type) ⇒ Object
Dump all associations of a given type.
dumper - The Dumper object used to dump additional objects. association_type - :has_one, :belongs_to, :has_many
Returns nothing.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/replicate/active_record.rb', line 60 def dump_all_association_replicants(dumper, association_type) self.class.reflect_on_all_associations(association_type).each do |reflection| next if (dependent = __send__(reflection.name)).nil? case dependent when ActiveRecord::Base, Array dumper.dump(dependent) else warn "warn: #{self.class}##{reflection.name} #{association_type} association " \ "unexpectedly returned a #{dependent.class}. skipping." end end end |
#dump_association_replicants(dumper, association) ⇒ Object
Dump objects associated with an AR object through an association name.
object - AR object instance. association - Name of the association whose objects should be dumped.
Returns nothing.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/replicate/active_record.rb', line 79 def dump_association_replicants(dumper, association) if reflection = self.class.reflect_on_association(association) objects = __send__(reflection.name) dumper.dump(objects) if reflection.macro == :has_and_belongs_to_many dump_has_and_belongs_to_many_replicant(dumper, reflection) end else warn "error: #{self.class}##{association} is invalid" end end |
#dump_has_and_belongs_to_many_replicant(dumper, reflection) ⇒ Object
Dump the special Habtm object used to establish many-to-many relationships between objects that have already been dumped. Note that this object and all objects referenced must have already been dumped before calling this method.
95 96 97 |
# File 'lib/replicate/active_record.rb', line 95 def dump_has_and_belongs_to_many_replicant(dumper, reflection) dumper.dump Habtm.new(self, reflection) end |
#dump_replicant(dumper) ⇒ Object
Replicate::Dumper calls this method on objects to trigger dumping a replicant object tuple. The default implementation dumps all belongs_to associations, then self, then all has_one associations, then any has_many or has_and_belongs_to_many associations declared with the replicate_associations macro.
dumper - Dumper object whose #write method must be called with the
type, id, and attributes hash.
Returns nothing.
22 23 24 25 26 27 28 29 |
# File 'lib/replicate/active_record.rb', line 22 def dump_replicant(dumper) dump_all_association_replicants dumper, :belongs_to dumper.write self.class.to_s, id, replicant_attributes, self dump_all_association_replicants dumper, :has_one self.class.replicate_associations.each do |association| dump_association_replicants dumper, association end end |
#replicant_attributes ⇒ Object
Attributes hash used to persist this object. This consists of simply typed values (no complex types or objects) with the exception of special foreign key values. When an attribute value is [:id, “SomeClass:1234”], the loader will handle translating the id value to the local system’s version of the same object.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/replicate/active_record.rb', line 36 def replicant_attributes attributes = self.attributes.dup self.class.reflect_on_all_associations(:belongs_to).each do |reflection| foreign_key = (reflection.[:foreign_key] || "#{reflection.name}_id").to_s if id = attributes[foreign_key] attributes[foreign_key] = [:id, reflection.klass.to_s, id] end end attributes end |
#replicant_id ⇒ Object
The replicant id is a two tuple containing the class and object id. This is used by Replicant::Dumper to determine if the object has already been dumped or not.
50 51 52 |
# File 'lib/replicate/active_record.rb', line 50 def replicant_id [self.class.name, id] end |