Class: 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

#==, #[]=, #clear, #count, #delete, #delete_at, #each, #insert, #method_missing, #split_scope, #trigger_for_index!, #trigger_on_direct_listeners!, #trigger_size_change!

Methods included from ObjectTracking

#__setup_tracking

Methods included from ReactiveTags

included, #reactive_method_tag

Constructor Details

#initialize(array = [], options = {}) ⇒ ArrayModel

Returns a new instance of ArrayModel.



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

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 ReactiveArray

Instance Attribute Details

#arrayObject (readonly)

Returns the value of attribute array.



10
11
12
# File 'lib/volt/models/array_model.rb', line 10

def array
  @array
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/volt/models/array_model.rb', line 10

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



10
11
12
# File 'lib/volt/models/array_model.rb', line 10

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/volt/models/array_model.rb', line 10

def path
  @path
end

#persistorObject (readonly)

Returns the value of attribute persistor.



10
11
12
# File 'lib/volt/models/array_model.rb', line 10

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
23
# 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|
      # puts "CALL #{method_name} - Load Data on #{self.inspect}"
      load_data
      super(*args)
    end
  end
end

Instance Method Details

#+(*args) ⇒ Object

Make sure it gets wrapped



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

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

#<<(model) ⇒ Object Also known as: append

Make sure it gets wrapped



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/volt/models/array_model.rb', line 68

def <<(model)
  if model.cur.is_a?(Model)
    # Set the new path
    model.cur.options = @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

#attributesObject



63
64
65
# File 'lib/volt/models/array_model.rb', line 63

def attributes
  self
end

#bufferObject



127
128
129
130
131
132
133
134
135
# File 'lib/volt/models/array_model.rb', line 127

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)

  return ReactiveValue.new(model)
end

#fetch(*args, &block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/volt/models/array_model.rb', line 55

def fetch(*args, &block)
  if @persistor
    return @persistor.fetch(*args, &block)
  else
    raise "this model's persistance layer does not support fetch, try using store"
  end
end

#find(*args) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/volt/models/array_model.rb', line 44

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



85
86
87
88
# File 'lib/volt/models/array_model.rb', line 85

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

#inspectObject



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

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

#new_array_model(*args) ⇒ Object



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

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

#new_model(*args) ⇒ Object



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

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



105
106
107
108
109
110
111
112
# File 'lib/volt/models/array_model.rb', line 105

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

  return array
end