Class: Volt::Persistors::ModelStore

Inherits:
Store show all
Includes:
StoreState
Defined in:
lib/volt/models/persistors/model_store.rb

Constant Summary collapse

ID_CHARS =
[('a'..'f'), ('0'..'9')].map(&:to_a).flatten

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Store

#clear_identity_map, #inspect, #read_new_model, #saved?

Methods inherited from Base

#added, #clear, #event_removed, #removed, #root_model

Constructor Details

#initialize(model, tasks) ⇒ ModelStore

Returns a new instance of ModelStore.



19
20
21
22
23
# File 'lib/volt/models/persistors/model_store.rb', line 19

def initialize(model, tasks)
  super

  @in_identity_map = false
end

Instance Attribute Details

#in_identity_mapObject

Returns the value of attribute in_identity_map.



17
18
19
# File 'lib/volt/models/persistors/model_store.rb', line 17

def in_identity_map
  @in_identity_map
end

#modelObject (readonly)

Returns the value of attribute model.



16
17
18
# File 'lib/volt/models/persistors/model_store.rb', line 16

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.



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/volt/models/persistors/model_store.rb', line 153

def self.changed(model_id, data)
  Model.no_save do
    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
end

Instance Method Details

#[](val) ⇒ Object



167
168
169
# File 'lib/volt/models/persistors/model_store.rb', line 167

def [](val)
  fail '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_collectionObject



33
34
35
36
37
38
39
# File 'lib/volt/models/persistors/model_store.rb', line 33

def add_to_collection
  @in_collection = true
  ensure_setup

  # Call changed, return the promise
  changed
end

#add_to_identity_mapObject



55
56
57
58
59
60
61
# File 'lib/volt/models/persistors/model_store.rb', line 55

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/volt/models/persistors/model_store.rb', line 80

def changed(attribute_name = nil)
  path = @model.path

  promise = Promise.new

  ensure_setup

  path_size = path.size
  if save_changes? && path_size > 0 && !@model.nil?
    if path_size > 3 && (parent = @model.parent) && (source = parent.parent)
      @model.attributes[:"#{path[-4].singularize}_id"] = source._id
    end

    if !collection
      puts 'Attempting to save model directly on store.'
      fail 'Attempting to save model directly on store.'
    else
      if RUBY_PLATFORM == 'opal'
        @save_promises ||= []
        @save_promises << promise

        queue_client_save
      else
        errors = save_to_db!(self_attributes)
        if errors.size == 0
          promise.resolve(nil)
        else
          promise.reject(errors)
        end
      end
    end
  end

  promise
end

#ensure_setupObject

Called the first time a value is assigned into this model



46
47
48
49
50
51
52
53
# File 'lib/volt/models/persistors/model_store.rb', line 46

def ensure_setup
  if (attrs = @model.attributes)
    # Do a nil check incase there is a nil model there
    @model.__id = generate_id if attrs[:_id].nil?

    add_to_identity_map
  end
end

#event_added(event, first, first_for_event) ⇒ Object



145
146
147
148
149
# File 'lib/volt/models/persistors/model_store.rb', line 145

def event_added(event, first, first_for_event)
  if first_for_event && event == :changed
    ensure_setup
  end
end

#generate_idObject

Create a random unique id that can be used as the mongo id as well



64
65
66
67
68
69
# File 'lib/volt/models/persistors/model_store.rb', line 64

def generate_id
  id = []
  24.times { id << ID_CHARS.sample }

  id.join
end

#loaded(initial_state = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/volt/models/persistors/model_store.rb', line 25

def loaded(initial_state = nil)
  if model.path == []
    initial_state = :loaded
  end

  model.change_state_to(:loaded_state, initial_state)
end

#queue_client_saveObject



116
117
118
119
120
121
122
# File 'lib/volt/models/persistors/model_store.rb', line 116

def queue_client_save
  `
  if (!self.saveTimer) {
    self.saveTimer = setImmediate(self.$run_save.bind(self));
  }
  `
end

#remove_from_collectionObject



41
42
43
# File 'lib/volt/models/persistors/model_store.rb', line 41

def remove_from_collection
  @in_collection = false
end

#run_saveObject

Run save is called on the client side after a queued setImmediate. It does the saving on the front-end. Adding a setImmediate allows multiple changes to be batched together.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/volt/models/persistors/model_store.rb', line 127

def run_save
  # Clear the save timer
  `
  clearImmediate(self.saveTimer);
  delete self.saveTimer;
  `

  StoreTasks.save(collection, @model.path, self_attributes).then do
    save_promises = @save_promises
    @save_promises = nil
    save_promises.each { |promise|  promise.resolve(nil) }
  end.fail do |errors|
    save_promises = @save_promises
    @save_promises = nil
    save_promises.each { |promise|  promise.reject(errors) }
  end
end

#save_changes?Boolean

Returns:



71
72
73
74
75
76
77
# File 'lib/volt/models/persistors/model_store.rb', line 71

def save_changes?
  if RUBY_PLATFORM == 'opal'
    !(defined?($loading_models) && $loading_models) && @tasks
  else
    true
  end
end