Class: Volt::ArrayModel

Inherits:
ReactiveArray show all
Includes:
ModelHelpers, ModelState, ModelWrapper
Defined in:
lib/volt/models/array_model.rb

Direct Known Subclasses

Cursor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#wrap_value, #wrap_values

Methods inherited from ReactiveArray

#==, #[], #[]=, #all?, #any?, #clear, #count, #delete, #delete_at, #each, #empty?, #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.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/volt/models/array_model.rb', line 41

def initialize(array = [], options = {})
  @options   = options
  @parent    = options[:parent]
  @path      = options[:path] || []
  @persistor = setup_persistor(options[: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 Volt::ReactiveArray

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



12
13
14
# File 'lib/volt/models/array_model.rb', line 12

def array
  @array
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/volt/models/array_model.rb', line 12

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



12
13
14
# File 'lib/volt/models/array_model.rb', line 12

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/volt/models/array_model.rb', line 12

def path
  @path
end

#persistorObject (readonly)

Returns the value of attribute persistor.



12
13
14
# File 'lib/volt/models/array_model.rb', line 12

def persistor
  @persistor
end

Class Method Details

.proxy_to_persistor(*method_names) ⇒ Object

Some methods get passed down to the persistor.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/volt/models/array_model.rb', line 26

def self.proxy_to_persistor(*method_names)
  method_names.each do |method_name|
    define_method(method_name) do |*args, &block|
      if @persistor.respond_to?(method_name)
        @persistor.send(method_name, *args, &block)
      else
        raise "this model's persistance layer does not support #{method_name}, try using store"
      end
    end
  end
end

.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.



16
17
18
19
20
21
22
23
# File 'lib/volt/models/array_model.rb', line 16

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



101
102
103
104
# File 'lib/volt/models/array_model.rb', line 101

def +(*args)
  args = wrap_values(args)
  super(*args)
end

#<<(model) ⇒ Object

Make sure it gets wrapped



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/volt/models/array_model.rb', line 59

def <<(model)
  if model.is_a?(Model)
    # Set the new path
    model.options = @options.merge(path: @options[:path] + [:[]])
  else
    model = wrap_values([model]).first
  end

  super(model)

  if @persistor
    @persistor.added(model, @array.size - 1)
  else
    nil
  end
end

#append(model) ⇒ Object

Works like << except it returns a promise



77
78
79
80
81
82
83
84
# File 'lib/volt/models/array_model.rb', line 77

def append(model)
  promise, model = self.send(:<<, model)

  # Return a promise if one doesn't exist
  promise ||= Promise.new.resolve(model)

  promise
end

#attributesObject



54
55
56
# File 'lib/volt/models/array_model.rb', line 54

def attributes
  self
end

#bufferObject



133
134
135
136
137
138
139
140
141
# File 'lib/volt/models/array_model.rb', line 133

def buffer
  model_path  = options[:path] + [:[]]
  model_klass = class_at_path(model_path)

  new_options = options.merge(path: model_path, save_to: self).reject { |k, _| k.to_sym == :persistor }
  model       = model_klass.new({}, new_options)

  model
end

#find_one(*args, &block) ⇒ Object

Find one does a query, but only returns the first item or nil if there is no match. Unlike #find, #find_one does not return another cursor that you can call .then on.



90
91
92
# File 'lib/volt/models/array_model.rb', line 90

def find_one(*args, &block)
  find(*args, &block).limit(1)[0]
end

#inject(*args) ⇒ Object

Make sure it gets wrapped



95
96
97
98
# File 'lib/volt/models/array_model.rb', line 95

def inject(*args)
  args = wrap_values(args)
  super(*args)
end

#inspectObject



123
124
125
126
127
128
129
130
131
# File 'lib/volt/models/array_model.rb', line 123

def inspect
  if @persistor && @persistor.is_a?(Persistors::ArrayStore) && state == :not_loaded
    # Show a special message letting users know it is not loaded yet.
    "#<#{self.class}:not loaded, access with [] or size to load>"
  else
    # Otherwise inspect normally
    super
  end
end

#new_array_model(*args) ⇒ Object



110
111
112
# File 'lib/volt/models/array_model.rb', line 110

def new_array_model(*args)
  ArrayModel.new(*args)
end

#new_model(*args) ⇒ Object



106
107
108
# File 'lib/volt/models/array_model.rb', line 106

def new_model(*args)
  class_at_path(options[:path]).new(*args)
end

#to_aObject

Convert the model to an array all of the way down



115
116
117
118
119
120
121
# File 'lib/volt/models/array_model.rb', line 115

def to_a
  array = []
  attributes.each do |value|
    array << deep_unwrap(value)
  end
  array
end