Class: Volt::Model

Inherits:
Object show all
Includes:
Buffer, ModelHashBehaviour, ModelHelpers, ModelState, ModelWrapper, Validations
Defined in:
lib/volt/models/model.rb

Direct Known Subclasses

User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Buffer

#buffer, #promise_for_errors, #save!

Methods included from ModelState

#change_state_to, #loaded?, #state

Methods included from Validations

#clear_server_errors, #errors, included, #mark_all_fields!, #mark_field!, #marked_errors, #marked_fields, #server_errors

Methods included from ModelHashBehaviour

#clear, #delete, #each, #each_pair, #each_with_object, #empty?, #false?, #key?, #keys, #nil?, #size, #to_h, #true?

Methods included from ModelHelpers

#class_at_path, #deep_unwrap, #event_added, #event_removed

Methods included from ModelWrapper

#wrap_value, #wrap_values

Constructor Details

#initialize(attributes = {}, options = {}, initial_state = nil) ⇒ Model

Returns a new instance of Model.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/volt/models/model.rb', line 25

def initialize(attributes = {}, options = {}, initial_state = nil)
  @deps        = HashDependency.new
  @size_dep    = Dependency.new
  self.options = options

  send(:attributes=, attributes, true)

  # Models start in a loaded state since they are normally setup from an
  # ArrayModel, which will have the data when they get added.
  @state = :loaded

  @persistor.loaded(initial_state) if @persistor
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/volt/models/model.rb', line 119

def method_missing(method_name, *args, &block)
  if method_name[0] == '_'

    # Remove underscore
    method_name = method_name[1..-1]
    if method_name[-1] == '='
      # Assigning an attribute without the =
      assign_attribute(method_name[0..-2], *args, &block)
    else
      read_attribute(method_name)
    end
  else
    # Call on parent
    super
  end
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



22
23
24
# File 'lib/volt/models/model.rb', line 22

def attributes
  @attributes
end

#optionsObject

Returns the value of attribute options.



23
24
25
# File 'lib/volt/models/model.rb', line 23

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



23
24
25
# File 'lib/volt/models/model.rb', line 23

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/volt/models/model.rb', line 23

def path
  @path
end

#persistorObject (readonly)

Returns the value of attribute persistor.



23
24
25
# File 'lib/volt/models/model.rb', line 23

def persistor
  @persistor
end

Class Method Details

.nosaveObject

Temporary stub for no save on client



277
278
279
# File 'lib/volt/models/model.rb', line 277

def self.nosave
  yield
end

Instance Method Details

#!Object

Pass through needed



115
116
117
# File 'lib/volt/models/model.rb', line 115

def !
  !attributes
end

#<<(value) ⇒ Object

Initialize an empty array and append to it



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/volt/models/model.rb', line 247

def <<(value)
  if @parent
    @parent.expand!
  else
    fail 'Model data should be stored in sub collections.'
  end

  # Grab the last section of the path, so we can do the assign on the parent
  path   = @path.last
  result = @parent.send(path)

  if result.nil?
    # If this isn't a model yet, instantiate it
    @parent.send(:"#{path}=", new_array_model([], @options))
    result = @parent.send(path)
  end

  # Add the new item
  result << value

  nil
end

#==(val) ⇒ Object

Pass the comparison through



104
105
106
107
108
109
110
111
112
# File 'lib/volt/models/model.rb', line 104

def ==(val)
  if val.is_a?(Model)
    # Use normal comparison for a model
    super
  else
    # Compare to attributes otherwise
    attributes == val
  end
end

#_idObject

the id is stored in a field named _id, so we setup _id to proxy to this



40
41
42
# File 'lib/volt/models/model.rb', line 40

def _id
  @attributes && @attributes[:_id]
end

#_id=(val) ⇒ Object



44
45
46
# File 'lib/volt/models/model.rb', line 44

def _id=(val)
  self.__id = val
end

#assign_attribute(method_name, *args, &block) ⇒ Object

Do the assignment to a model and trigger a changed event



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/volt/models/model.rb', line 137

def assign_attribute(method_name, *args, &block)
  self.expand!
  # Assign, without the =
  attribute_name = method_name.to_sym

  value = args[0]

  old_value = @attributes[attribute_name]
  new_value = wrap_value(value, [attribute_name])

  if old_value != new_value
    @attributes[attribute_name] = new_value

    @deps.changed!(attribute_name)

    if old_value == nil || new_value == nil
      @size_dep.changed!
    end

    # TODO: Can we make this so it doesn't need to be handled for non store collections
    # (maybe move it to persistor, though thats weird since buffers don't have a persistor)
    clear_server_errors(attribute_name) if @server_errors

    # Don't save right now if we're in a nosave block
    if !defined?(Thread) || !Thread.current['nosave']
      # Let the persistor know something changed
      @persistor.changed(attribute_name) if @persistor
    end
  end
end

#expand!Object

If this model is nil, it makes it into a hash model, then sets it up to track from the parent.



235
236
237
238
239
240
241
242
243
244
# File 'lib/volt/models/model.rb', line 235

def expand!
  if attributes.nil?
    @attributes = {}
    if @parent
      @parent.expand!

      @parent.send(:"_#{@path.last}=", self)
    end
  end
end

#inspectObject



270
271
272
# File 'lib/volt/models/model.rb', line 270

def inspect
  "<#{self.class}:#{object_id} #{attributes.inspect}>"
end

#new_array_model(attributes, options) ⇒ Object



225
226
227
228
229
230
231
# File 'lib/volt/models/model.rb', line 225

def new_array_model(attributes, options)
  # Start with an empty query
  options         = options.dup
  options[:query] = {}

  ArrayModel.new(attributes, options)
end

#new_model(attributes, options) ⇒ Object



221
222
223
# File 'lib/volt/models/model.rb', line 221

def new_model(attributes, options)
  class_at_path(options[:path]).new(attributes, options)
end

#read_attribute(attr_name) ⇒ Object

When reading an attribute, we need to handle reading on: 1) a nil model, which returns a wrapped error 2) reading directly from attributes 3) trying to read a key that doesn’t exist.



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/volt/models/model.rb', line 172

def read_attribute(attr_name)
  # Reading an attribute, we may get back a nil model.
  attr_name = attr_name.to_sym

  # Track dependency
  # @deps.depend(attr_name)

  # See if the value is in attributes
  if @attributes && @attributes.key?(attr_name)
    # Track dependency
    @deps.depend(attr_name)

    return @attributes[attr_name]
  else
    new_model              = read_new_model(attr_name)
    @attributes            ||= {}
    @attributes[attr_name] = new_model

    # Trigger size change
    # TODO: We can probably improve Computations to just make this work
    # without the delay
    if Volt.in_browser?
      `setImmediate(function() {`
        @size_dep.changed!
      `});`
    else
      @size_dep.changed!
    end

    # Depend on attribute
    @deps.depend(attr_name)
    return new_model
  end
end

#read_new_model(method_name) ⇒ Object

Get a new model, make it easy to override



208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/volt/models/model.rb', line 208

def read_new_model(method_name)
  if @persistor && @persistor.respond_to?(:read_new_model)
    return @persistor.read_new_model(method_name)
  else
    opts = @options.merge(parent: self, path: path + [method_name])
    if method_name.plural?
      return new_array_model([], opts)
    else
      return new_model(nil, opts)
    end
  end
end