Class: MongoWatchable::Proxy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, array_key, array_count_key, target_class) ⇒ Proxy

Returns a new instance of Proxy.



8
9
10
# File 'lib/mongo_watchable/proxy.rb', line 8

def initialize(model, array_key, array_count_key, target_class)
  @model, @array_key, @array_count_key, @target_class = model, array_key, array_count_key, target_class
end

Instance Attribute Details

#array_count_keyObject (readonly)

Returns the value of attribute array_count_key.



4
5
6
# File 'lib/mongo_watchable/proxy.rb', line 4

def array_count_key
  @array_count_key
end

#array_keyObject (readonly)

Returns the value of attribute array_key.



3
4
5
# File 'lib/mongo_watchable/proxy.rb', line 3

def array_key
  @array_key
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/mongo_watchable/proxy.rb', line 5

def model
  @model
end

#target_classObject (readonly)

Returns the value of attribute target_class.



6
7
8
# File 'lib/mongo_watchable/proxy.rb', line 6

def target_class
  @target_class
end

Instance Method Details

#<<(entry) ⇒ Object



50
51
52
53
54
# File 'lib/mongo_watchable/proxy.rb', line 50

def << (entry)
  array << entry.id
  increment_array_count
  @fetch ? @fetch << entry : fetch_all
end

#all(opts = {}) ⇒ Object



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

def all(opts = {})
  return fetch_all if opts.empty?
  target_class.all(opts.merge(:id.in => array))
end

#countObject Also known as: size



16
17
18
# File 'lib/mongo_watchable/proxy.rb', line 16

def count
  array.size
end

#delete(entry) ⇒ Object



56
57
58
59
# File 'lib/mongo_watchable/proxy.rb', line 56

def delete(entry)
  decrement_array_count if array.delete(entry.id)
  @fetch ? @fetch.delete(entry) : fetch_all
end

#each(&block) ⇒ Object



25
26
27
# File 'lib/mongo_watchable/proxy.rb', line 25

def each(&block)
  fetch_all.each {|entry| yield entry}
end

#empty?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/mongo_watchable/proxy.rb', line 44

def empty?
  array.empty?
end

#find(id) ⇒ Object



29
30
31
32
# File 'lib/mongo_watchable/proxy.rb', line 29

def find(id)
  return nil unless array.include?(id)
  target_class.find(id)
end

#first(opts = {}) ⇒ Object



34
35
36
37
# File 'lib/mongo_watchable/proxy.rb', line 34

def first(opts = {})
  return @first ||= target_class.find(array.first) if opts.empty?
  target_class.first(opts.merge(:_id.in => array))
end

#inspectObject



61
62
63
# File 'lib/mongo_watchable/proxy.rb', line 61

def inspect
  all.inspect
end

#last(opts = {}) ⇒ Object



39
40
41
42
# File 'lib/mongo_watchable/proxy.rb', line 39

def last(opts = {})
  return @last ||= target_class.find(array.last) if opts.empty?
  target_class.last(opts.merge(:_id.in => array))
end

#to_aObject



12
13
14
# File 'lib/mongo_watchable/proxy.rb', line 12

def to_a
  fetch_all.to_a
end