Class: MongoDoc::Associations::CollectionProxy

Inherits:
Object
  • Object
show all
Includes:
ProxyBase
Defined in:
lib/mongodoc/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

#_parent=, #_path_to_root, #attach, included

Constructor Details

#initialize(options) ⇒ CollectionProxy

Returns a new instance of CollectionProxy.



34
35
36
37
# File 'lib/mongodoc/associations/collection_proxy.rb', line 34

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

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



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

def collection
  @collection
end

Instance Method Details

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



40
41
42
43
44
# File 'lib/mongodoc/associations/collection_proxy.rb', line 40

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

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



48
49
50
51
# File 'lib/mongodoc/associations/collection_proxy.rb', line 48

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

#_appendObject



39
# File 'lib/mongodoc/associations/collection_proxy.rb', line 39

alias _append <<

#_root=(root) ⇒ Object



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

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

#_unshiftObject



77
# File 'lib/mongodoc/associations/collection_proxy.rb', line 77

alias _unshift unshift

#addObject



47
# File 'lib/mongodoc/associations/collection_proxy.rb', line 47

alias add []=

#build(attrs) ⇒ Object



54
55
56
57
# File 'lib/mongodoc/associations/collection_proxy.rb', line 54

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

#concat(array) ⇒ Object



59
60
61
62
63
# File 'lib/mongodoc/associations/collection_proxy.rb', line 59

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:

  • (Boolean)


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

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

#replace(other) ⇒ Object



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

def replace(other)
  clear
  concat(other)
end

#unshift(item) ⇒ Object



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

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

#valid?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
# File 'lib/mongodoc/associations/collection_proxy.rb', line 83

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