Class: Volt::Model
- Includes:
- ModelHashBehaviour, ModelHelpers, ModelState, ModelWrapper, Validations
- Defined in:
- lib/volt/models/model.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#persistor ⇒ Object
readonly
Returns the value of attribute persistor.
Class Method Summary collapse
-
.nosave ⇒ Object
Temporary stub for no save on client.
Instance Method Summary collapse
-
#! ⇒ Object
Pass through needed.
-
#<<(value) ⇒ Object
Initialize an empty array and append to it.
-
#==(val) ⇒ Object
Pass the comparison through.
-
#_id ⇒ Object
the id is stored in a field named _id, so we setup _id to proxy to this.
- #_id=(val) ⇒ Object
-
#assign_attribute(method_name, *args, &block) ⇒ Object
Do the assignment to a model and trigger a changed event.
-
#buffer ⇒ Object
Returns a buffered version of the model.
-
#expand! ⇒ Object
If this model is nil, it makes it into a hash model, then sets it up to track from the parent.
-
#initialize(attributes = {}, options = {}, initial_state = nil) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
- #new_array_model(attributes, options) ⇒ Object
- #new_model(attributes, options) ⇒ Object
-
#promise_for_errors(errors) ⇒ Object
When errors come in, we mark all fields and return a rejected promise.
-
#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.
-
#read_new_model(method_name) ⇒ Object
Get a new model, make it easy to override.
- #return_undefined_method(method_name) ⇒ Object
- #save! ⇒ Object
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_with_object, #empty?, #false?, #keys, #nil?, #size, #to_h, #true?
Methods included from ModelHelpers
#class_at_path, #deep_unwrap, #event_added, #event_removed
Methods included from ModelWrapper
Constructor Details
#initialize(attributes = {}, options = {}, initial_state = nil) ⇒ Model
Returns a new instance of Model.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/volt/models/model.rb', line 23 def initialize(attributes = {}, = {}, initial_state = nil) @deps = HashDependency.new @size_dep = Dependency.new self. = 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/volt/models/model.rb', line 117 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
#attributes ⇒ Object
Returns the value of attribute attributes.
20 21 22 |
# File 'lib/volt/models/model.rb', line 20 def attributes @attributes end |
#options ⇒ Object
Returns the value of attribute options.
21 22 23 |
# File 'lib/volt/models/model.rb', line 21 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
21 22 23 |
# File 'lib/volt/models/model.rb', line 21 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
21 22 23 |
# File 'lib/volt/models/model.rb', line 21 def path @path end |
#persistor ⇒ Object (readonly)
Returns the value of attribute persistor.
21 22 23 |
# File 'lib/volt/models/model.rb', line 21 def persistor @persistor end |
Class Method Details
.nosave ⇒ Object
Temporary stub for no save on client
361 362 363 |
# File 'lib/volt/models/model.rb', line 361 def self.nosave yield end |
Instance Method Details
#! ⇒ Object
Pass through needed
113 114 115 |
# File 'lib/volt/models/model.rb', line 113 def ! !attributes end |
#<<(value) ⇒ Object
Initialize an empty array and append to it
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/volt/models/model.rb', line 258 def <<(value) if @parent @parent. 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
102 103 104 105 106 107 108 109 110 |
# File 'lib/volt/models/model.rb', line 102 def ==(val) if val.is_a?(Model) # Use normal comparison for a model super else # Compare to attributes otherwise attributes == val end end |
#_id ⇒ Object
the id is stored in a field named _id, so we setup _id to proxy to this
38 39 40 |
# File 'lib/volt/models/model.rb', line 38 def _id @attributes && @attributes[:_id] end |
#_id=(val) ⇒ Object
42 43 44 |
# File 'lib/volt/models/model.rb', line 42 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
135 136 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 |
# File 'lib/volt/models/model.rb', line 135 def assign_attribute(method_name, *args, &block) self. # 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 |
#buffer ⇒ Object
Returns a buffered version of the model
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/volt/models/model.rb', line 334 def buffer model_path = [:path] # When we grab a buffer off of a plual class (subcollection), we get it as a model. if model_path.last.plural? && model_path[-1] != :[] model_klass = class_at_path(model_path + [:[]]) else model_klass = class_at_path(model_path) end = .merge(path: model_path, save_to: self).reject { |k, _| k.to_sym == :persistor } model = model_klass.new({}, , :loading) if state == :loaded setup_buffer(model) else parent.then do setup_buffer(model) end end model end |
#expand! ⇒ Object
If this model is nil, it makes it into a hash model, then sets it up to track from the parent.
246 247 248 249 250 251 252 253 254 255 |
# File 'lib/volt/models/model.rb', line 246 def if attributes.nil? @attributes = {} if @parent @parent. @parent.send(:"_#{@path.last}=", self) end end end |
#inspect ⇒ Object
281 282 283 284 285 |
# File 'lib/volt/models/model.rb', line 281 def inspect Computation.run_without_tracking do "<#{self.class}:#{object_id} #{attributes.inspect}>" end end |
#new_array_model(attributes, options) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/volt/models/model.rb', line 236 def new_array_model(attributes, ) # Start with an empty query = .dup [:query] = {} ArrayModel.new(attributes, ) end |
#new_model(attributes, options) ⇒ Object
232 233 234 |
# File 'lib/volt/models/model.rb', line 232 def new_model(attributes, ) class_at_path([:path]).new(attributes, ) end |
#promise_for_errors(errors) ⇒ Object
When errors come in, we mark all fields and return a rejected promise.
327 328 329 330 331 |
# File 'lib/volt/models/model.rb', line 327 def promise_for_errors(errors) mark_all_fields! Promise.new.reject(errors) 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.
171 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 |
# File 'lib/volt/models/model.rb', line 171 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
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/volt/models/model.rb', line 207 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 |
#return_undefined_method(method_name) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/volt/models/model.rb', line 220 def return_undefined_method(method_name) # Methods called on nil capture an error so the user can know where # their nil calls are. This error can be re-raised at a later point. fail NilMethodCall.new("undefined method `#{method_name}' for #{self}") rescue => e result = e # Cleanup backtrace # TODO: this could be better result.backtrace.reject! { |line| line['lib/models/model.rb'] || line['lib/models/live_value.rb'] } end |
#save! ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/volt/models/model.rb', line 287 def save! # Compute the erros once errors = self.errors if errors.size == 0 save_to = [:save_to] if save_to if save_to.is_a?(ArrayModel) # Add to the collection promise = save_to.append(attributes) else # We have a saved model promise = save_to.assign_attributes(attributes) end return promise.then do |new_model| if new_model # Set the buffer's id to track the main model's id attributes[:_id] = new_model._id [:save_to] = new_model end nil end.fail do |errors| if errors.is_a?(Hash) server_errors.replace(errors) end promise_for_errors(errors) end else fail 'Model is not a buffer, can not be saved, modifications should be persisted as they are made.' end else # Some errors, mark all fields promise_for_errors(errors) end end |