public
Description: Ruby interface to CouchDB
Homepage: http://code.google.com/p/activecouch/
Clone URL: git://github.com/JackDanger/active_couch.git
arun.thampi (author)
Sat Feb 02 22:44:18 -0800 2008
commit  8b4e9c074f31df170a41605520f5e218be9e43a8
tree    fbac9eca0b8f2932e304095e2dfaae19590ecd63
parent  36a0f6f74f5c591b1695c83104de4ef424cc89f5
active_couch / lib / active_couch / associations / has_many_association.rb
100644 35 lines (29 sloc) 0.957 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# TODO Consider implementing Enumerable semantics.
 
module ActiveCouch
  class HasManyAssociation
    attr_accessor :name, :klass, :container
    
    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 = @name.classify.constantize #Inflector.constantize(Inflector.classify(@name))
      end
    end
 
    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
    
    def pop
      @container.pop
    end
    
    def to_hash
      { @name => @container }
    end
    
  end
end