Class: Volt::Model
- Includes:
- FieldHelpers, Associations, Buffer, ClassEventable, Dirty, ListenerTracker, Permissions, ModelHashBehaviour, ModelHelpers, ModelWrapper, Modes, ReactiveAccessors, StateHelpers, StateManager, UserValidatorHelpers, Validations
- Defined in:
- lib/volt/models/model.rb,
lib/volt/models/permissions.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Permissions
Constant Summary collapse
- INVALID_FIELD_NAMES =
{ :attributes => true, :parent => true, :path => true, :options => true, :persistor => true }
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
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.
-
#==(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
- #add_list ⇒ Object
-
#assign_attributes(attrs, initial_setup = false, skip_changes = false) ⇒ Object
(also: #attributes=)
Assign multiple attributes as a hash, directly.
- #destroy ⇒ Object
-
#get(attr_name, expand = false) ⇒ 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.
-
#initialize(attributes = {}, options = {}, initial_state = nil) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
-
#new? ⇒ Boolean
Return true if the model hasn’t been saved yet.
- #new_array_model(attributes, options) ⇒ Object
- #new_model(*args) ⇒ Object
-
#read_new_model(method_name) ⇒ Object
Get a new model, make it easy to override.
- #remove_list ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
-
#set(attribute_name, value, &block) ⇒ Object
Do the assignment to a model and trigger a changed event.
- #state_for(*args) ⇒ Object
Methods included from ReactiveAccessors
#__reactive_dependency_get, included
Methods included from Associations
Methods included from Permissions
#action_allowed?, #allow, #allow_and_deny_fields, #can_create?, #can_delete?, #can_read?, #deny, #filtered_attributes, included, #owner?
Methods included from ListenerTracker
#listener_added, #listener_removed
Methods included from Modes
Methods included from ClassEventable
Methods included from Dirty
#attribute_will_change!, #changed?, #changed_attributes, #changes, #clear_tracked_changes!, #revert_changes!, #was
Methods included from FieldHelpers
Methods included from Buffer
#buffer, #buffer?, #promise_for_errors, #save!, #save_to
Methods included from Validations
#clear_server_errors, #error_in_changed_attributes?, #errors, included, #mark_all_fields!, #mark_field!, #marked_errors, #marked_fields, #server_errors, #validate!
Methods included from StateHelpers
Methods included from StateManager
Methods included from ModelHashBehaviour
#clear, #delete, #each, #each_pair, #each_with_object, #empty?, #key?, #keys, #nil?, #size, #to_h
Methods included from ModelHelpers
#deep_unwrap, #event_added, #event_removed, included
Methods included from ModelWrapper
Constructor Details
#initialize(attributes = {}, options = {}, initial_state = nil) ⇒ Model
Returns a new instance of Model.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/volt/models/model.rb', line 59 def initialize(attributes = {}, = {}, initial_state = nil) # The listener event counter keeps track of how many computations are listening on this model @listener_event_counter = EventCounter.new( -> { parent.try(:persistor).try(:listener_added) }, -> { parent.try(:persistor).try(:listener_removed) } ) # The root dependency is used to track if anything is using the data from this # model. That information is relayed to the ArrayModel so it knows when it can # stop subscribing. # @root_dep = Dependency.new(@listener_event_counter.method(:add), @listener_event_counter.method(:remove)) @root_dep = Dependency.new(-> { add_list }, -> { remove_list }) @deps = HashDependency.new @size_dep = Dependency.new self. = @new = (initial_state != :loaded) assign_attributes(attributes, true) # The persistor is usually responsible for setting up the loaded_state, if # there is no persistor, we set it to loaded if @persistor @persistor.loaded(initial_state) else change_state_to(:loaded_state, initial_state || :loaded, false) end # Trigger the new event, pass in :new trigger!(:new, :new) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/volt/models/model.rb', line 190 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 = set(method_name[0..-2], args[0], &block) else # If the method has an ! on the end, then we assign an empty # collection if no result exists already. = (method_name[-1] == '!') method_name = method_name[0..-2] if get(method_name, ) end else # Call on parent super end end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
49 50 51 |
# File 'lib/volt/models/model.rb', line 49 def attributes @attributes end |
#options ⇒ Object
Returns the value of attribute options.
49 50 51 |
# File 'lib/volt/models/model.rb', line 49 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
49 50 51 |
# File 'lib/volt/models/model.rb', line 49 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
49 50 51 |
# File 'lib/volt/models/model.rb', line 49 def path @path end |
#persistor ⇒ Object (readonly)
Returns the value of attribute persistor.
49 50 51 |
# File 'lib/volt/models/model.rb', line 49 def persistor @persistor end |
Instance Method Details
#! ⇒ Object
Pass through needed
186 187 188 |
# File 'lib/volt/models/model.rb', line 186 def ! !attributes end |
#==(val) ⇒ Object
Pass the comparison through
175 176 177 178 179 180 181 182 183 |
# File 'lib/volt/models/model.rb', line 175 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
106 107 108 |
# File 'lib/volt/models/model.rb', line 106 def _id __id end |
#_id=(val) ⇒ Object
110 111 112 |
# File 'lib/volt/models/model.rb', line 110 def _id=(val) self.__id = val end |
#add_list ⇒ Object
92 93 94 |
# File 'lib/volt/models/model.rb', line 92 def add_list @listener_event_counter.add end |
#assign_attributes(attrs, initial_setup = false, skip_changes = false) ⇒ Object Also known as: attributes=
Assign multiple attributes as a hash, directly.
129 130 131 132 133 134 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 166 167 168 169 170 |
# File 'lib/volt/models/model.rb', line 129 def assign_attributes(attrs, initial_setup=false, skip_changes=false) @attributes ||= {} attrs = wrap_values(attrs) if attrs # When doing a mass-assign, we don't validate or save until the end. if initial_setup || skip_changes Model.no_change_tracking do assign_all_attributes(attrs, skip_changes) end else assign_all_attributes(attrs) end else # Assign to nil @attributes = attrs end # Trigger and change all @deps.changed_all! @deps = HashDependency.new # Save the changes if initial_setup # Run initial validation if Volt.in_mode?(:no_validate) # No validate, resolve nil Promise.new.resolve(nil) else return validate!.then do |errs| if errs && errs.size > 0 Promise.new.reject(errs) else Promise.new.resolve(nil) end end end else return run_changed end end |
#destroy ⇒ Object
339 340 341 342 343 344 345 346 347 348 |
# File 'lib/volt/models/model.rb', line 339 def destroy if parent result = parent.delete(self) # Wrap result in a promise if it isn't one return Promise.new.then { result } else fail "Model does not have a parent and cannot be deleted." end end |
#get(attr_name, expand = false) ⇒ 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.
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/volt/models/model.rb', line 249 def get(attr_name, =false) # Reading an attribute, we may get back a nil model. attr_name = attr_name.to_sym check_valid_field_name(attr_name) # Track that something is listening @root_dep.depend # Track dependency @deps.depend(attr_name) # See if the value is in attributes if @attributes && @attributes.key?(attr_name) return @attributes[attr_name] else # If we're expanding, or the get is for a collection, in which # case we always expand. plural_attr = attr_name.plural? if || plural_attr new_value = read_new_model(attr_name) # A value was generated, store it if new_value # Assign directly. Since this is the first time # we're loading, we can just assign. # # Don't track changes if we're setting a collection Volt.run_in_mode_if(plural_attr, :no_change_tracking) do set(attr_name, new_value) end end return new_value else return nil end end end |
#inspect ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/volt/models/model.rb', line 320 def inspect Computation.run_without_tracking do str = "<#{self.class}:#{object_id}" # Get path, loaded_state, and persistor, but cache in local var path = self.path str += " path:#{path}" if path loaded_state = self.loaded_state str += " state:#{loaded_state}" if loaded_state persistor = self.persistor # str += " persistor:#{persistor.inspect}" if persistor str += " #{attributes.inspect}>" str end end |
#new? ⇒ Boolean
Return true if the model hasn’t been saved yet
115 116 117 |
# File 'lib/volt/models/model.rb', line 115 def new? @new end |
#new_array_model(attributes, options) ⇒ Object
312 313 314 315 316 317 318 |
# File 'lib/volt/models/model.rb', line 312 def new_array_model(attributes, ) # Start with an empty query = .dup [:query] = [] ArrayModel.new(attributes, ) end |
#new_model(*args) ⇒ Object
308 309 310 |
# File 'lib/volt/models/model.rb', line 308 def new_model(*args) Volt::Model.class_at_path([:path]).new(*args) end |
#read_new_model(method_name) ⇒ Object
Get a new model, make it easy to override
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/volt/models/model.rb', line 294 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({}, opts) end end end |
#remove_list ⇒ Object
96 97 98 |
# File 'lib/volt/models/model.rb', line 96 def remove_list @listener_event_counter.remove end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
289 290 291 |
# File 'lib/volt/models/model.rb', line 289 def respond_to_missing?(method_name, include_private=false) method_name.to_s.start_with?('_') || super end |
#set(attribute_name, value, &block) ⇒ Object
Do the assignment to a model and trigger a changed event
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/volt/models/model.rb', line 212 def set(attribute_name, value, &block) # Assign, without the = attribute_name = attribute_name.to_sym check_valid_field_name(attribute_name) old_value = @attributes[attribute_name] new_value = wrap_value(value, [attribute_name]) if old_value != new_value # Track the old value, skip if we are in no_validate attribute_will_change!(attribute_name, old_value) unless Volt.in_mode?(:no_change_tracking) # Assign the 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.present? # Save the changes run_changed(attribute_name) unless Volt.in_mode?(:no_change_tracking) end new_value end |
#state_for(*args) ⇒ Object
100 101 102 103 |
# File 'lib/volt/models/model.rb', line 100 def state_for(*args) @root_dep.depend super end |