Class: ArrayModel
- Inherits:
-
ReactiveArray
- Object
- ReactiveArray
- ArrayModel
- Includes:
- ModelHelpers, ModelState, ModelWrapper
- Defined in:
- lib/volt/models/array_model.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
-
#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.
Class Method Summary collapse
-
.proxy_with_load_data(*method_names) ⇒ Object
For many methods, we want to call load data as soon as the model is interacted with, so we proxy the method, then call super.
Instance Method Summary collapse
-
#+(*args) ⇒ Object
Make sure it gets wrapped.
-
#<<(model) ⇒ Object
(also: #append)
Make sure it gets wrapped.
- #attributes ⇒ Object
- #buffer ⇒ Object
- #find(*args, &block) ⇒ Object
-
#initialize(array = [], options = {}) ⇒ ArrayModel
constructor
A new instance of ArrayModel.
-
#inject(*args) ⇒ Object
Make sure it gets wrapped.
- #inspect ⇒ Object
- #new_array_model(*args) ⇒ Object
- #new_model(*args) ⇒ Object
- #then(*args, &block) ⇒ Object
-
#to_a ⇒ Object
Convert the model to an array all of the way down.
Methods included from ModelState
#change_state_to, #loaded?, #state
Methods included from ModelHelpers
#class_at_path, #deep_unwrap, #event_added, #event_removed
Methods included from ModelWrapper
Methods inherited from ReactiveArray
#==, #[], #[]=, #all?, #any?, #clear, #count, #delete, #delete_at, #each, #insert, #method_missing, #select, #size
Methods included from Eventable
#on, #remove_listener, #trigger!
Constructor Details
#initialize(array = [], options = {}) ⇒ ArrayModel
Returns a new instance of ArrayModel.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/volt/models/array_model.rb', line 26 def initialize(array=[], ={}) @options = @parent = [:parent] @path = [:path] || [] @persistor = setup_persistor([:persistor]) array = wrap_values(array) super(array) @persistor.loaded if @persistor end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ReactiveArray
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
11 12 13 |
# File 'lib/volt/models/array_model.rb', line 11 def array @array end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/volt/models/array_model.rb', line 11 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
11 12 13 |
# File 'lib/volt/models/array_model.rb', line 11 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/volt/models/array_model.rb', line 11 def path @path end |
#persistor ⇒ Object (readonly)
Returns the value of attribute persistor.
11 12 13 |
# File 'lib/volt/models/array_model.rb', line 11 def persistor @persistor end |
Class Method Details
.proxy_with_load_data(*method_names) ⇒ Object
For many methods, we want to call load data as soon as the model is interacted with, so we proxy the method, then call super.
15 16 17 18 19 20 21 22 |
# File 'lib/volt/models/array_model.rb', line 15 def self.proxy_with_load_data(*method_names) method_names.each do |method_name| define_method(method_name) do |*args| load_data super(*args) end end end |
Instance Method Details
#+(*args) ⇒ Object
Make sure it gets wrapped
83 84 85 86 |
# File 'lib/volt/models/array_model.rb', line 83 def +(*args) args = wrap_values(args) super(*args) end |
#<<(model) ⇒ Object Also known as: append
Make sure it gets wrapped
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/volt/models/array_model.rb', line 60 def <<(model) if model.is_a?(Model) # Set the new path model. = @options.merge(path: @options[:path] + [:[]]) else model = wrap_values([model]).first end super(model) @persistor.added(model, @array.size-1) if @persistor return model end |
#attributes ⇒ Object
55 56 57 |
# File 'lib/volt/models/array_model.rb', line 55 def attributes self end |
#buffer ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/volt/models/array_model.rb', line 116 def buffer model_path = [:path] + [:[]] model_klass = class_at_path(model_path) = .merge(path: model_path, save_to: self).reject {|k,_| k.to_sym == :persistor } model = model_klass.new({}, ) return model end |
#find(*args, &block) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/volt/models/array_model.rb', line 39 def find(*args, &block) if @persistor return @persistor.find(*args, &block) else raise "this model's persistance layer does not support find, try using store" end end |
#inject(*args) ⇒ Object
Make sure it gets wrapped
77 78 79 80 |
# File 'lib/volt/models/array_model.rb', line 77 def inject(*args) args = wrap_values(args) super(*args) end |
#inspect ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/volt/models/array_model.rb', line 106 def inspect if @persistor && @persistor.is_a?(Persistors::ArrayStore) && state == :not_loaded # Show a special message letting users know it is not loaded yet. return "#<#{self.class.to_s}:not loaded, access with [] or size to load>" end # Otherwise inspect normally return super end |
#new_array_model(*args) ⇒ Object
92 93 94 |
# File 'lib/volt/models/array_model.rb', line 92 def new_array_model(*args) ArrayModel.new(*args) end |
#new_model(*args) ⇒ Object
88 89 90 |
# File 'lib/volt/models/array_model.rb', line 88 def new_model(*args) class_at_path([:path]).new(*args) end |
#then(*args, &block) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/volt/models/array_model.rb', line 47 def then(*args, &block) if @persistor return @persistor.then(*args, &block) else raise "this model's persistance layer does not support then, try using store" end end |
#to_a ⇒ Object
Convert the model to an array all of the way down
97 98 99 100 101 102 103 104 |
# File 'lib/volt/models/array_model.rb', line 97 def to_a array = [] attributes.each do |value| array << deep_unwrap(value) end return array end |