Class: Volt::RepoCache::Collection
- Inherits:
-
ModelArray
- Object
- ModelArray
- Volt::RepoCache::Collection
- Includes:
- Util
- Defined in:
- lib/volt/repo_cache/collection.rb
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#load_query ⇒ Object
readonly
Returns the value of attribute load_query.
-
#loaded ⇒ Object
readonly
Promise.
-
#loaded_ids ⇒ Object
readonly
Returns the value of attribute loaded_ids.
-
#marked_for_destruction ⇒ Object
readonly
Returns the value of attribute marked_for_destruction.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#model_class_name ⇒ Object
readonly
Returns the value of attribute model_class_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#read_only ⇒ Object
readonly
Returns the value of attribute read_only.
-
#repo_collection ⇒ Object
readonly
Returns the value of attribute repo_collection.
Instance Method Summary collapse
-
#<<(model) ⇒ Object
Returns self after appending the given model.
-
#append(model, error_if_present: true, error_unless_new: true, notify: true) ⇒ Object
Appends a model to the collection.
-
#create(hash = {}) ⇒ Object
Create a new model from given hash and append it to the collection.
-
#flush! ⇒ Object
Flushes each model in the array.
-
#initialize(cache: nil, name: nil, options: {}) ⇒ Collection
constructor
A new instance of Collection.
-
#inspect ⇒ Object
hide circular reference to cache.
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 = [:query] || [:where] @read_only = [:read_only].nil? ? true : [: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() load # debug __method__, __LINE__ end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
19 20 21 |
# File 'lib/volt/repo_cache/collection.rb', line 19 def associations @associations end |
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
10 11 12 |
# File 'lib/volt/repo_cache/collection.rb', line 10 def cache @cache end |
#load_query ⇒ Object (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 |
#loaded ⇒ Object (readonly)
Promise
17 18 19 |
# File 'lib/volt/repo_cache/collection.rb', line 17 def loaded @loaded end |
#loaded_ids ⇒ Object (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_destruction ⇒ Object (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_class ⇒ Object (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_name ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/volt/repo_cache/collection.rb', line 11 def name @name end |
#read_only ⇒ Object (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_collection ⇒ Object (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 |
#inspect ⇒ Object
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 |