Class: ArrayModel
- Inherits:
-
ReactiveArray
- Object
- ReactiveArray
- ArrayModel
- Includes:
- ModelHelpers, ModelWrapper
- Defined in:
- lib/volt/models/array_model.rb
Instance Attribute Summary collapse
-
#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
-
#+(*args) ⇒ Object
Make sure it gets wrapped.
-
#<<(model) ⇒ Object
Make sure it gets wrapped.
-
#[](index) ⇒ Object
For stored items, tell the collection to load the data when it is requested.
- #attributes ⇒ Object
- #find(*args) ⇒ Object
-
#initialize(array = [], options = {}) ⇒ ArrayModel
constructor
A new instance of ArrayModel.
-
#inject(*args) ⇒ Object
Make sure it gets wrapped.
- #inspect ⇒ Object
- #loaded? ⇒ Boolean
- #new_array_model(*args) ⇒ Object
- #new_model(attributes, options) ⇒ Object
- #size ⇒ Object
- #state ⇒ Object
-
#to_a ⇒ Object
Convert the model to an array all of the way down.
Methods included from ModelHelpers
#class_at_path, #deep_unwrap, #event_added, #event_removed
Methods included from ModelWrapper
Methods inherited from ReactiveArray
#==, #[]=, #clear, #delete, #delete_at, #each, #insert, #method_missing, #split_scope, #trigger_for_index!, #trigger_on_direct_listeners!, #trigger_size_change!
Methods included from ObjectTracking
Methods included from ReactiveTags
included, #reactive_method_tag
Constructor Details
#initialize(array = [], options = {}) ⇒ ArrayModel
Returns a new instance of ArrayModel.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/volt/models/array_model.rb', line 10 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
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/volt/models/array_model.rb', line 8 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
8 9 10 |
# File 'lib/volt/models/array_model.rb', line 8 def parent @parent end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/volt/models/array_model.rb', line 8 def path @path end |
#persistor ⇒ Object (readonly)
Returns the value of attribute persistor.
8 9 10 |
# File 'lib/volt/models/array_model.rb', line 8 def persistor @persistor end |
Instance Method Details
#+(*args) ⇒ Object
Make sure it gets wrapped
84 85 86 87 |
# File 'lib/volt/models/array_model.rb', line 84 def +(*args) args = wrap_values(args) super(*args) end |
#<<(model) ⇒ Object
Make sure it gets wrapped
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/volt/models/array_model.rb', line 64 def <<(model) if model.cur.is_a?(Model) # Set the new path model.cur. = @options.merge(path: @options[:path] + [:[]]) else model = wrap_values([model]).first end super(model) @persistor.added(model, @array.size-1) if @persistor end |
#[](index) ⇒ Object
For stored items, tell the collection to load the data when it is requested.
25 26 27 28 |
# File 'lib/volt/models/array_model.rb', line 25 def [](index) load_data super end |
#attributes ⇒ Object
59 60 61 |
# File 'lib/volt/models/array_model.rb', line 59 def attributes self end |
#find(*args) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/volt/models/array_model.rb', line 51 def find(*args) if @persistor return @persistor.find(*args) else raise "this model's persistance layer does not support find, try using store" end end |
#inject(*args) ⇒ Object
Make sure it gets wrapped
78 79 80 81 |
# File 'lib/volt/models/array_model.rb', line 78 def inject(*args) args = wrap_values(args) super(*args) end |
#inspect ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/volt/models/array_model.rb', line 107 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 super end |
#loaded? ⇒ Boolean
43 44 45 |
# File 'lib/volt/models/array_model.rb', line 43 def loaded? state == :loaded end |
#new_array_model(*args) ⇒ Object
93 94 95 |
# File 'lib/volt/models/array_model.rb', line 93 def new_array_model(*args) ArrayModel.new(*args) end |
#new_model(attributes, options) ⇒ Object
89 90 91 |
# File 'lib/volt/models/array_model.rb', line 89 def new_model(attributes, ) class_at_path([:path]).new(attributes, ) end |
#size ⇒ Object
30 31 32 33 |
# File 'lib/volt/models/array_model.rb', line 30 def size load_data super end |
#state ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/volt/models/array_model.rb', line 35 def state if @persistor @persistor.state else :loaded end end |
#to_a ⇒ Object
Convert the model to an array all of the way down
98 99 100 101 102 103 104 105 |
# File 'lib/volt/models/array_model.rb', line 98 def to_a array = [] attributes.each do |value| array << deep_unwrap(value) end return array end |