Class: Model
- Includes:
- ModelHelpers, ModelWrapper, ObjectTracking, ReactiveTags
- Defined in:
- lib/volt/models/model.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#options ⇒ Object
readonly
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.
- #[](val) ⇒ Object
-
#assign_attribute(method_name, *args, &block) ⇒ Object
Do the assignment to a model and trigger a changed event.
- #delete(*args) ⇒ Object
-
#event_added(event, scope_provider, first) ⇒ Object
Pass to the persisotr.
-
#event_removed(event, no_more_events) ⇒ Object
Pass to the persistor.
-
#expand! ⇒ Object
If this model is nil, it makes it into a hash model, then sets it up to track from the parent.
- #false? ⇒ Boolean
-
#initialize(attributes = {}, options = {}) ⇒ Model
constructor
A new instance of Model.
- #inspect ⇒ Object
- #method_missing(method_name, *args, &block) ⇒ Object
- #new_array_model(*args) ⇒ Object
- #new_model(*args) ⇒ Object
- #nil? ⇒ Boolean
-
#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
-
#to_h ⇒ Object
Convert the model to a hash all of the way down.
- #trigger_by_attribute!(event_name, attribute, *passed_args) ⇒ Object
- #true? ⇒ Boolean
Methods included from ModelHelpers
Methods included from ObjectTracking
Methods included from ModelWrapper
Methods included from ReactiveTags
included, #reactive_method_tag
Constructor Details
#initialize(attributes = {}, options = {}) ⇒ Model
Returns a new instance of Model.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/volt/models/model.rb', line 37 def initialize(attributes={}, ={}) @options = @parent = [:parent] @path = [:path] || [] @class_paths = [:class_paths] @persistor = setup_persistor([:persistor]) self.attributes = wrap_values(attributes) @persistor.loaded if @persistor end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/volt/models/model.rb', line 90 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 method directly on attributes. (since they are # not using _ ) attributes.send(method_name, *args, &block) end end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
22 23 24 |
# File 'lib/volt/models/model.rb', line 22 def attributes @attributes end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
23 24 25 |
# File 'lib/volt/models/model.rb', line 23 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
23 24 25 |
# File 'lib/volt/models/model.rb', line 23 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
23 24 25 |
# File 'lib/volt/models/model.rb', line 23 def path @path end |
#persistor ⇒ Object (readonly)
Returns the value of attribute persistor.
23 24 25 |
# File 'lib/volt/models/model.rb', line 23 def persistor @persistor end |
Instance Method Details
#! ⇒ Object
Pass through needed
61 62 63 |
# File 'lib/volt/models/model.rb', line 61 def ! !attributes end |
#<<(value) ⇒ Object
Initialize an empty array and append to it
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/volt/models/model.rb', line 202 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 result end |
#==(val) ⇒ Object
Pass the comparison through
50 51 52 53 54 55 56 57 58 |
# File 'lib/volt/models/model.rb', line 50 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 |
#[](val) ⇒ Object
229 230 231 |
# File 'lib/volt/models/model.rb', line 229 def [](val) raise "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 |
#assign_attribute(method_name, *args, &block) ⇒ Object
Do the assignment to a model and trigger a changed event
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/volt/models/model.rb', line 106 def assign_attribute(method_name, *args, &block) self. # Assign, without the = attribute_name = method_name[0..-2].to_sym value = args[0] __assign_element(attribute_name, value) attributes[attribute_name] = wrap_value(value, [attribute_name]) trigger_by_attribute!('changed', attribute_name) # Let the persistor know something changed @persistor.changed(attribute_name) if @persistor end |
#delete(*args) ⇒ Object
79 80 81 82 83 |
# File 'lib/volt/models/model.rb', line 79 def delete(*args) __clear_element(args[0]) attributes.delete(*args) trigger_by_attribute!('changed', args[0]) end |
#event_added(event, scope_provider, first) ⇒ Object
Pass to the persisotr
66 67 68 |
# File 'lib/volt/models/model.rb', line 66 def event_added(event, scope_provider, first) @persistor.event_added(event, scope_provider, first) if @persistor end |
#event_removed(event, no_more_events) ⇒ Object
Pass to the persistor
71 72 73 |
# File 'lib/volt/models/model.rb', line 71 def event_removed(event, no_more_events) @persistor.event_removed(event, no_more_events) if @persistor end |
#expand! ⇒ Object
If this model is nil, it makes it into a hash model, then sets it up to track from the parent.
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/volt/models/model.rb', line 187 def if attributes.nil? self.attributes = {} if @parent @parent. @parent.attributes[@path.last] = self end end end |
#false? ⇒ Boolean
29 30 31 |
# File 'lib/volt/models/model.rb', line 29 def false? attributes.false? end |
#inspect ⇒ Object
225 226 227 |
# File 'lib/volt/models/model.rb', line 225 def inspect "<#{self.class.to_s}:#{object_id} #{attributes.inspect}>" end |
#new_array_model(*args) ⇒ Object
168 169 170 |
# File 'lib/volt/models/model.rb', line 168 def new_array_model(*args) ArrayModel.new(*args) end |
#new_model(*args) ⇒ Object
164 165 166 |
# File 'lib/volt/models/model.rb', line 164 def new_model(*args) Model.new(*args) 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.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/volt/models/model.rb', line 125 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) elsif attributes && attributes.has_key?(method_name) # Method has the key, look it up directly return attributes[method_name] else return read_new_model(method_name) end end |
#read_new_model(method_name) ⇒ Object
Get a new model, make it easy to override
142 143 144 145 146 147 148 |
# File 'lib/volt/models/model.rb', line 142 def read_new_model(method_name) if @persistor && @persistor.respond_to?(:read_new_model) @persistor.read_new_model(method_name) else return new_model(nil, @options.merge(parent: self, path: path + [method_name])) end end |
#return_undefined_method(method_name) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/volt/models/model.rb', line 150 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 around ReactiveValue's # TODO: this could be better result.backtrace.reject! {|line| line['lib/models/model.rb'] || line['lib/models/live_value.rb'] } end end |
#to_h ⇒ Object
Convert the model to a hash all of the way down.
234 235 236 237 238 239 240 241 |
# File 'lib/volt/models/model.rb', line 234 def to_h hash = {} attributes.each_pair do |key, value| hash[key] = deep_unwrap(value) end return hash end |
#trigger_by_attribute!(event_name, attribute, *passed_args) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/volt/models/model.rb', line 172 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 |