Class: ActiveCouch::HasManyAssociation
- Inherits:
-
Object
- Object
- ActiveCouch::HasManyAssociation
- Defined in:
- lib/active_couch/associations/has_many_association.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
Returns the value of attribute container.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(name, options = {}) ⇒ HasManyAssociation
constructor
A new instance of HasManyAssociation.
- #pop ⇒ Object
- #push(obj) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ HasManyAssociation
Returns a new instance of HasManyAssociation.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_couch/associations/has_many_association.rb', line 7 def initialize(name, = {}) @name, @container, klass = name.to_s, [], [:class] if !klass.nil? && klass.is_a?(Class) @klass = klass else # Use the inflector to get the correct class if it is not defined # in the :class key in the options hash # so has_many :contacts (will try to find the class Contact and set it to @klass) @klass = Inflector.constantize(Inflector.classify(@name)) end end |
Instance Attribute Details
#container ⇒ Object
Returns the value of attribute container.
5 6 7 |
# File 'lib/active_couch/associations/has_many_association.rb', line 5 def container @container end |
#klass ⇒ Object
Returns the value of attribute klass.
5 6 7 |
# File 'lib/active_couch/associations/has_many_association.rb', line 5 def klass @klass end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/active_couch/associations/has_many_association.rb', line 5 def name @name end |
Instance Method Details
#pop ⇒ Object
26 27 28 |
# File 'lib/active_couch/associations/has_many_association.rb', line 26 def pop @container.pop end |
#push(obj) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/active_couch/associations/has_many_association.rb', line 19 def push(obj) unless obj.is_a?(klass) raise InvalidCouchTypeError, "The object that you are trying to add is not a #{klass}" end @container << obj end |
#to_hash ⇒ Object
30 31 32 |
# File 'lib/active_couch/associations/has_many_association.rb', line 30 def to_hash { @name => @container } end |