Class: MongoDoc::Proxy

Inherits:
Object show all
Defined in:
lib/mongodoc/proxy.rb

Constant Summary collapse

ARRAY_METHODS =

List of array methods (that are not in Object) that need to be delegated to collection.

(Array.instance_methods - Object.instance_methods).map { |n| n.to_s }
MUST_DEFINE =

List of additional methods that must be delegated to collection.

%w[to_a to_ary inspect to_bson ==]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Proxy

Returns a new instance of Proxy.



33
34
35
36
37
38
39
# File 'lib/mongodoc/proxy.rb', line 33

def initialize(options)
  @assoc_name = options[:assoc_name]
  @collection = []
  @collection_class = options[:collection_class]
  @_root = options[:root]
  @_parent = options[:parent]
end

Instance Attribute Details

#_parentObject

Returns the value of attribute _parent.



20
21
22
# File 'lib/mongodoc/proxy.rb', line 20

def _parent
  @_parent
end

#_rootObject

Returns the value of attribute _root.



20
21
22
# File 'lib/mongodoc/proxy.rb', line 20

def _root
  @_root
end

#assoc_nameObject (readonly)

Returns the value of attribute assoc_name.



20
21
22
# File 'lib/mongodoc/proxy.rb', line 20

def assoc_name
  @assoc_name
end

#collectionObject (readonly)

Returns the value of attribute collection.



20
21
22
# File 'lib/mongodoc/proxy.rb', line 20

def collection
  @collection
end

#collection_classObject (readonly)

Returns the value of attribute collection_class.



20
21
22
# File 'lib/mongodoc/proxy.rb', line 20

def collection_class
  @collection_class
end

Instance Method Details

#<<(items) ⇒ Object Also known as: push, concat



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mongodoc/proxy.rb', line 42

def <<(items)
  items = [items] unless items.kind_of?(Array)
  items.each do |item|
    item = collection_class.new(item) if Hash === item
    raise NotADocumentError unless collection_class === item
    append item
    item._parent = self
    item._root = _root
  end
  self
end

#appendObject



41
# File 'lib/mongodoc/proxy.rb', line 41

alias_method :append, :<<

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Lie about our class. Borrowed from Rake::FileList Note: Does not work for case equality (===)

Returns:

  • (Boolean)


58
59
60
# File 'lib/mongodoc/proxy.rb', line 58

def is_a?(klass)
  klass == Array || super(klass)
end

#path_to_root(attrs) ⇒ Object



63
64
65
# File 'lib/mongodoc/proxy.rb', line 63

def path_to_root(attrs)
  _parent.path_to_root(attrs)
end