Class: MongoDoc::Associations::CollectionProxy

Inherits:
Object
  • Object
show all
Includes:
ProxyBase
Defined in:
lib/mongo_doc/associations/collection_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 ==]
DO_NOT_DEFINE =
%w[concat insert replace]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ProxyBase

included, is_document?

Constructor Details

#initialize(options) ⇒ CollectionProxy

Returns a new instance of CollectionProxy.



52
53
54
55
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 52

def initialize(options)
  @collection = []
  super
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



25
26
27
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 25

def collection
  @collection
end

Instance Method Details

#<<(item) ⇒ Object Also known as: push



58
59
60
61
62
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 58

def <<(item)
  attach(item)
  _append item
  self
end

#[]=(index, item) ⇒ Object Also known as: insert



66
67
68
69
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 66

def []=(index, item)
  attach(item)
  add(index, item)
end

#_appendObject



57
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 57

alias _append <<

#_modifier_path=(path) ⇒ Object



27
28
29
30
31
32
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 27

def _modifier_path=(path)
  super
  collection.each do |item|
    item._modifier_path = _modifier_path + '.$' if ProxyBase.is_document?(item)
  end
end

#_root=(value) ⇒ Object



34
35
36
37
38
39
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 34

def _root=(value)
  @_root = value
  collection.each do |item|
    item._root = value if ProxyBase.is_document?(item)
  end
end

#_unshiftObject



95
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 95

alias _unshift unshift

#addObject



65
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 65

alias add []=

#build(attrs) ⇒ Object



72
73
74
75
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 72

def build(attrs)
  item = _assoc_class.new(attrs)
  push(item)
end

#concat(array) ⇒ Object



77
78
79
80
81
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 77

def concat(array)
  array.each do |item|
    push(item)
  end
end

#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:



85
86
87
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 85

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

#replace(other) ⇒ Object



90
91
92
93
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 90

def replace(other)
  clear
  concat(other)
end

#unshift(item) ⇒ Object



96
97
98
99
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 96

def unshift(item)
  attach(item)
  _unshift(item)
end

#valid?Boolean

Returns:



101
102
103
104
105
106
107
108
109
# File 'lib/mongo_doc/associations/collection_proxy.rb', line 101

def valid?
  all? do |child|
    if ProxyBase.is_document?(child)
      child.valid?
    else
      true
    end
  end
end