Class: ActiveCouch::HasManyAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_couch/associations/has_many_association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @name, @container, klass = name.to_s, [], options[: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

#containerObject

Returns the value of attribute container.



5
6
7
# File 'lib/active_couch/associations/has_many_association.rb', line 5

def container
  @container
end

#klassObject

Returns the value of attribute klass.



5
6
7
# File 'lib/active_couch/associations/has_many_association.rb', line 5

def klass
  @klass
end

#nameObject

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

#popObject



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_hashObject



30
31
32
# File 'lib/active_couch/associations/has_many_association.rb', line 30

def to_hash
  { @name => @container }
end