Class: Volt::RepoCache::Collection

Inherits:
ModelArray show all
Includes:
Util
Defined in:
lib/volt/repo_cache/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

adder, arrify, creator, debug, friend?, friends_only, not_yet_implemented, prefix_method, remover, setter, subclass_responsibility, time, unsupported

Methods inherited from ModelArray

#[], #collect, #count, #detect, #each, #each_with_index, #empty?, #first, #index, #last, #query, #reactive_array?, reactive_array?, #reduce, #reject, #select, #size, #sort, #to_a

Constructor Details

#initialize(cache: nil, name: nil, options: {}) ⇒ Collection

Returns a new instance of Collection.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/volt/repo_cache/collection.rb', line 22

def initialize(cache: nil, name: nil, options: {})
  # debug __method__, __LINE__, "name: #{name} options: #{options}"
  super(observer: self)
  # debug __method__, __LINE__
  @cache = cache
  @name = name
  @load_query = options[:query] || options[:where]
  @read_only = options[:read_only].nil? ? true : options[:read_only]
  @marked_for_destruction = {}
  @model_class_name = @name.to_s.singularize.camelize
  @model_class = Object.const_get(@model_class_name)
  @repo_collection = @cache.repo.send(name)
  init_associations(options)
  load
  # debug __method__, __LINE__
end

Instance Attribute Details

#associationsObject (readonly)

Returns the value of attribute associations.



19
20
21
# File 'lib/volt/repo_cache/collection.rb', line 19

def associations
  @associations
end

#cacheObject (readonly)

Returns the value of attribute cache.



10
11
12
# File 'lib/volt/repo_cache/collection.rb', line 10

def cache
  @cache
end

#load_queryObject (readonly)

Returns the value of attribute load_query.



15
16
17
# File 'lib/volt/repo_cache/collection.rb', line 15

def load_query
  @load_query
end

#loadedObject (readonly)

Promise



17
18
19
# File 'lib/volt/repo_cache/collection.rb', line 17

def loaded
  @loaded
end

#loaded_idsObject (readonly)

Returns the value of attribute loaded_ids.



16
17
18
# File 'lib/volt/repo_cache/collection.rb', line 16

def loaded_ids
  @loaded_ids
end

#marked_for_destructionObject (readonly)

Returns the value of attribute marked_for_destruction.



18
19
20
# File 'lib/volt/repo_cache/collection.rb', line 18

def marked_for_destruction
  @marked_for_destruction
end

#model_classObject (readonly)

Returns the value of attribute model_class.



13
14
15
# File 'lib/volt/repo_cache/collection.rb', line 13

def model_class
  @model_class
end

#model_class_nameObject (readonly)

Returns the value of attribute model_class_name.



12
13
14
# File 'lib/volt/repo_cache/collection.rb', line 12

def model_class_name
  @model_class_name
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/volt/repo_cache/collection.rb', line 11

def name
  @name
end

#read_onlyObject (readonly)

Returns the value of attribute read_only.



20
21
22
# File 'lib/volt/repo_cache/collection.rb', line 20

def read_only
  @read_only
end

#repo_collectionObject (readonly)

Returns the value of attribute repo_collection.



14
15
16
# File 'lib/volt/repo_cache/collection.rb', line 14

def repo_collection
  @repo_collection
end

Instance Method Details

#<<(model) ⇒ Object

Returns self after appending the given model



88
89
90
91
# File 'lib/volt/repo_cache/collection.rb', line 88

def <<(model)
  append(model)
  self
end

#append(model, error_if_present: true, error_unless_new: true, notify: true) ⇒ Object

Appends a model to the collection. Model may be a hash which will be converted. (See #induct for more.) If the model belongs_to any other owners, the foreign id(s) MUST be already set to ensure associational integrity in the cache - it is easier to ask the owner for a new instance (e.g. product.recipe.new_ingredient). NB: Returns the newly appended model.



81
82
83
84
85
# File 'lib/volt/repo_cache/collection.rb', line 81

def append(model, error_if_present: true, error_unless_new: true,  notify: true)
  model = induct(model, error_unless_new: error_unless_new, error_if_present: error_if_present)
  __append__(model, notify: notify)
  model
end

#create(hash = {}) ⇒ Object

Create a new model from given hash and append it to the collection. Returns the new model



69
70
71
# File 'lib/volt/repo_cache/collection.rb', line 69

def create(hash = {})
  append(hash.to_h)
end

#flush!Object

Flushes each model in the array. Returns a single Promise when element promises are resolved. TODO: error handling



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/volt/repo_cache/collection.rb', line 52

def flush!
  promises = []
  unless read_only
    # models are removed from @marked_from_destruction as
    # they are flushed, so we need a copy of them to enumerate
    @marked_for_destruction.values.dup.each do |e|
      promises << e.flush!
    end
    each do |e|
      promises << e.flush!
    end
  end
  Promise.when(*promises)
end

#inspectObject

hide circular reference to cache



40
41
42
43
44
45
46
# File 'lib/volt/repo_cache/collection.rb', line 40

def inspect
  __tmp = @cache
  @cache = '{{hidden for inspect}}'
  result = super
  @cache = __tmp
  result
end