Class: CouchObject::Persistable::HasManyRelation
- Inherits:
-
Array
- Object
- Array
- CouchObject::Persistable::HasManyRelation
- Defined in:
- lib/couch_object/persistable/has_many_relations_array.rb
Overview
This class is used to easily be able to overwrite functionality in Arrays that enable messaging between classes that are related
Instance Method Summary collapse
- #<<(other_object) ⇒ Object
- #delete(object_to_delete) ⇒ Object
-
#disable_call_back_on_add ⇒ Object
When adding objects from the load relations method the call back shouldn’t be made.
- #enable_call_back_on_add ⇒ Object
-
#initialize(owner) ⇒ HasManyRelation
constructor
A new instance of HasManyRelation.
- #original_delete ⇒ Object
- #perform_remove(object_to_remove) ⇒ Object
- #remove(object_to_remove) ⇒ Object
- #remove_callback(other_object) ⇒ Object
-
#to_s ⇒ Object
This method is needed so that HasManyRelations can be compared more resonably :).
Constructor Details
#initialize(owner) ⇒ HasManyRelation
Returns a new instance of HasManyRelation.
9 10 11 12 13 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 9 def initialize(owner) @owner = owner @call_back_on_add = true super() end |
Instance Method Details
#<<(other_object) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 27 def <<(other_object) # Makes sure there is not more than one reference for each relation return self if self.include?(other_object) # Add the new object super(other_object) # We have to find the matching local belongs_to relationsship belongs_to_relationship_name = \ @owner.define_relationship_name(other_object) if @call_back_on_add # Perform a callback to the object so it sets it's # belongs_to relationsship method_to_call = (belongs_to_relationship_name + "=").to_sym unless other_object.send(belongs_to_relationship_name) == @owner other_object.send(method_to_call, @owner) end else # Performs a semi callback to the object so it sets it's # belongs_to relationsship. This semi callback doesn't perform # any callbacks itself method_to_call = (belongs_to_relationship_name + \ "_without_call_back=").to_sym other_object.send(method_to_call, @owner) end self end |
#delete(object_to_delete) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 65 def delete(object_to_delete) remove(object_to_delete) # Issue a delete statement for the object object_to_delete.send(:delete) end |
#disable_call_back_on_add ⇒ Object
When adding objects from the load relations method the call back shouldn’t be made
24 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 24 def disable_call_back_on_add; @call_back_on_add = false; end |
#enable_call_back_on_add ⇒ Object
25 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 25 def enable_call_back_on_add; @call_back_on_add = true; end |
#original_delete ⇒ Object
64 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 64 alias original_delete delete |
#perform_remove(object_to_remove) ⇒ Object
77 78 79 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 77 def perform_remove(object_to_remove) original_delete(object_to_remove) end |
#remove(object_to_remove) ⇒ Object
72 73 74 75 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 72 def remove(object_to_remove) remove_callback(object_to_remove) perform_remove(object_to_remove) end |
#remove_callback(other_object) ⇒ Object
81 82 83 84 85 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 81 def remove_callback(other_object) method_to_call = (@owner.define_relationship_name(other_object) + \ "_without_call_back=").to_sym other_object.send(method_to_call, nil) end |
#to_s ⇒ Object
This method is needed so that HasManyRelations can be compared more resonably :)
17 18 19 20 |
# File 'lib/couch_object/persistable/has_many_relations_array.rb', line 17 def to_s containing_ids = self.map {|v| [v.id, v.revision]} "<HasManyRelation:#{containing_ids.sort.join(",")}>" end |