Class: Persistors::ModelStore
- Includes:
- StoreState
- Defined in:
- lib/volt/models/persistors/model_store.rb
Constant Summary collapse
- ID_CHARS =
[('a'..'z'), ('A'..'Z'), ('0'..'9')].map {|v| v.to_a }.flatten
Instance Attribute Summary collapse
-
#in_identity_map ⇒ Object
Returns the value of attribute in_identity_map.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Class Method Summary collapse
-
.changed(model_id, data) ⇒ Object
Update the models based on the id/identity map.
Instance Method Summary collapse
- #[](val) ⇒ Object
- #add_to_collection ⇒ Object
- #add_to_identity_map ⇒ Object
-
#changed(attribute_name = nil) ⇒ Object
Called when the model changes.
-
#ensure_setup ⇒ Object
Called the first time a value is assigned into this model.
- #event_added(event, scope_provider, first, first_for_event) ⇒ Object
-
#generate_id ⇒ Object
Create a random unique id that can be used as the mongo id as well.
-
#initialize(model, tasks) ⇒ ModelStore
constructor
A new instance of ModelStore.
- #remove_from_collection ⇒ Object
Methods included from StoreState
#change_state_to, #loaded, #state
Methods inherited from Store
Methods inherited from Base
#added, #event_removed, #loaded, #removed
Constructor Details
#initialize(model, tasks) ⇒ ModelStore
Returns a new instance of ModelStore.
13 14 15 16 17 |
# File 'lib/volt/models/persistors/model_store.rb', line 13 def initialize(model, tasks) super @in_identity_map = false end |
Instance Attribute Details
#in_identity_map ⇒ Object
Returns the value of attribute in_identity_map.
11 12 13 |
# File 'lib/volt/models/persistors/model_store.rb', line 11 def in_identity_map @in_identity_map end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
10 11 12 |
# File 'lib/volt/models/persistors/model_store.rb', line 10 def model @model end |
Class Method Details
.changed(model_id, data) ⇒ Object
Update the models based on the id/identity map. Usually these requests will come from the backend.
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/volt/models/persistors/model_store.rb', line 88 def self.changed(model_id, data) model = @@identity_map.lookup(model_id) if model data.each_pair do |key, value| if key != '_id' model.send(:"#{key}=", value) end end end end |
Instance Method Details
#[](val) ⇒ Object
100 101 102 |
# File 'lib/volt/models/persistors/model_store.rb', line 100 def [](val) raise "Models do not support hash style lookup. Hashes inserted into other models are converted to models, see https://github.com/voltrb/volt#automatic-model-conversion" end |
#add_to_collection ⇒ Object
19 20 21 22 23 |
# File 'lib/volt/models/persistors/model_store.rb', line 19 def add_to_collection @in_collection = true ensure_setup changed end |
#add_to_identity_map ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/volt/models/persistors/model_store.rb', line 38 def add_to_identity_map unless @in_identity_map @@identity_map.add(@model._id, @model) @in_identity_map = true end end |
#changed(attribute_name = nil) ⇒ Object
Called when the model changes
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/volt/models/persistors/model_store.rb', line 55 def changed(attribute_name=nil) path = @model.path promise = Promise.new ensure_setup path_size = path.size if !(defined?($loading_models) && $loading_models) && @tasks && path_size > 0 && !@model.nil? if path_size > 3 && (parent = @model.parent) && source = parent.parent @model.attributes[:"#{path[-4].singularize}_id"] = source._id end @tasks.call('StoreTasks', 'save', collection, self_attributes) do |errors| if errors.size == 0 promise.resolve else promise.reject(errors) end end end return promise end |
#ensure_setup ⇒ Object
Called the first time a value is assigned into this model
30 31 32 33 34 35 36 |
# File 'lib/volt/models/persistors/model_store.rb', line 30 def ensure_setup if @model.attributes @model.attributes[:_id] ||= generate_id add_to_identity_map end end |
#event_added(event, scope_provider, first, first_for_event) ⇒ Object
80 81 82 83 84 |
# File 'lib/volt/models/persistors/model_store.rb', line 80 def event_added(event, scope_provider, first, first_for_event) if first_for_event && event == :changed ensure_setup end end |
#generate_id ⇒ Object
Create a random unique id that can be used as the mongo id as well
47 48 49 50 51 52 |
# File 'lib/volt/models/persistors/model_store.rb', line 47 def generate_id id = [] 12.times { id << ID_CHARS.sample } return id.join end |
#remove_from_collection ⇒ Object
25 26 27 |
# File 'lib/volt/models/persistors/model_store.rb', line 25 def remove_from_collection @in_collection = false end |