Class: 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.
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
-
#read_attribute(method_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
- #trigger_by_attribute!(event_name, attribute, *passed_args) ⇒ Object
Methods included from ModelState
#change_state_to, #loaded?, #state
Methods included from Validations
#errors, included, #mark_field!, #marked_errors, #marked_fields
Methods included from ModelHashBehaviour
#clear, #delete, #each_with_object, #empty?, #false?, #nil?, #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 |
# File 'lib/volt/models/model.rb', line 23 def initialize(attributes={}, ={}, initial_state=nil) @deps = HashDependency.new self. = 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
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/volt/models/model.rb', line 112 def method_missing(method_name, *args, &block) if method_name[0] == '_' if method_name[-1] == '=' # Assigning an attribute with = assign_attribute(method_name, *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 |
Instance Method Details
#! ⇒ Object
Pass through needed
107 108 109 |
# File 'lib/volt/models/model.rb', line 107 def ! !attributes end |
#<<(value) ⇒ Object
Initialize an empty array and append to it
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/volt/models/model.rb', line 243 def <<(value) if @parent @parent. else raise "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 return nil end |
#==(val) ⇒ Object
Pass the comparison through
96 97 98 99 100 101 102 103 104 |
# File 'lib/volt/models/model.rb', line 96 def ==(val) if val.is_a?(Model) # Use normal comparison for a model return super else # Compare to attributes otherwise return attributes == val end end |
#_id ⇒ Object
the id is stored in a field named _id, so we setup _id to proxy to this
37 38 39 |
# File 'lib/volt/models/model.rb', line 37 def _id @attributes && @attributes[:_id] end |
#_id=(val) ⇒ Object
41 42 43 |
# File 'lib/volt/models/model.rb', line 41 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
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/volt/models/model.rb', line 127 def assign_attribute(method_name, *args, &block) self. # Assign, without the = attribute_name = method_name[1..-2].to_sym value = args[0] @attributes[attribute_name] = wrap_value(value, [attribute_name]) @deps.changed!(attribute_name) # Let the persistor know something changed @persistor.changed(attribute_name) if @persistor end |
#buffer ⇒ Object
Returns a buffered version of the model
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/volt/models/model.rb', line 312 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 self.parent.then do setup_buffer(model) end end return 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.
231 232 233 234 235 236 237 238 239 240 |
# File 'lib/volt/models/model.rb', line 231 def if attributes.nil? @attributes = {} if @parent @parent. @parent.send(:"_#{@path.last}=", self) end end end |
#inspect ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/volt/models/model.rb', line 266 def inspect str = nil Computation.run_without_tracking do str = "<#{self.class.to_s}:#{object_id} #{attributes.inspect}>" end return str end |
#new_array_model(attributes, options) ⇒ Object
208 209 210 211 212 213 214 |
# File 'lib/volt/models/model.rb', line 208 def new_array_model(attributes, ) # Start with an empty query = .dup [:query] = {} ArrayModel.new(attributes, ) end |
#new_model(attributes, options) ⇒ Object
204 205 206 |
# File 'lib/volt/models/model.rb', line 204 def new_model(attributes, ) class_at_path([:path]).new(attributes, ) end |
#read_attribute(method_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.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/volt/models/model.rb', line 146 def read_attribute(method_name) # Reading an attribute, we may get back a nil model. method_name = method_name.to_sym if method_name[0] != '_' && @attributes == nil # The method we are calling is on a nil model, return a wrapped # exception. return return_undefined_method(method_name) else attr_name = method_name[1..-1].to_sym # See if the value is in attributes value = (@attributes && @attributes[attr_name]) # Track dependency @deps.depend(attr_name) if value # key was in attributes or cache return value else # TODO: implement a timed out cache flusing new_model = read_new_model(attr_name) @attributes ||= {} @attributes[attr_name] = new_model return new_model end end end |
#read_new_model(method_name) ⇒ Object
Get a new model, make it easy to override
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/volt/models/model.rb', line 177 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
190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/volt/models/model.rb', line 190 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. begin raise NilMethodCall.new("undefined method `#{method_name}' for #{self.to_s}") 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 end |
#save! ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/volt/models/model.rb', line 275 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 new_model = save_to << self.attributes # Set the buffer's id to track the main model's id self.attributes[:_id] = new_model._id [:save_to] = new_model # TODO: return a promise that resolves if the append works else # We have a saved model return save_to.assign_attributes(self.attributes) end else raise "Model is not a buffer, can not be saved, modifications should be persisted as they are made." end return Promise.new.resolve({}) else # Some errors, mark all fields self.class.validations.keys.each do |key| mark_field!(key.to_sym) end return Promise.new.reject(errors) end end |
#trigger_by_attribute!(event_name, attribute, *passed_args) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/volt/models/model.rb', line 216 def trigger_by_attribute!(event_name, attribute, *passed_args) trigger_by_scope!(event_name, *passed_args) do |scope| method_name, *args, block = scope # TODO: Opal bug args ||= [] # Any methods without _ are not directly related to one attribute, so # they should all trigger !method_name || method_name[0] != '_' || (method_name == attribute.to_sym && args.size == 0) end end |