class Volt::RepoCache::ModelArray

Public Class Methods

new(observer: nil, contents: nil) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 13
def initialize(observer: nil, contents: nil)
  @contents = Volt::ReactiveArray.new(contents || [])
  @id_hash = {}
  @contents.each do |e|
    @id_hash[e.id] = e
  end
end
reactive_array?() click to toggle source

for benefit of Volt::Watch

# File lib/volt/repo_cache/model_array.rb, line 9
def self.reactive_array?
  true
end

Public Instance Methods

[](index) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 44
def [](index)
  @contents[index]
end
collect(&block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 88
def collect(&block)
  @contents.collect(&block)
end
Also aliased as: map
count(&block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 72
def count(&block)
  @contents.count(&block)
end
detect(*args, &block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 52
def detect(*args, &block)
  @contents.detect(*args, &block)
end
each(&block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 56
def each(&block)
  @contents.each(&block)
end
each_with_index(&block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 60
def each_with_index(&block)
  @contents.each_with_index(&block)
end
empty?() click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 48
def empty?
  @contents.empty
end
first() click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 64
def first
  @contents.first
end
index(*args, &block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 30
def index(*args, &block)
  @contents.index(*args, &block)
end
inject(seed, &block)
Alias for: reduce
last() click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 68
def last
  @contents.last
end
map(&block)
Alias for: collect
observe(action, model) click to toggle source

subclasses may override if interested.

# File lib/volt/repo_cache/model_array.rb, line 26
def observe(action, model)
  # no op
end
query(args = nil, &block) click to toggle source

Query is simple for now:

  • a hash of keys and values to match by equality

  • or a select block

TODO: would prefer a splat to the hash, but Opal fails to parse calls with **splats

# File lib/volt/repo_cache/model_array.rb, line 105
def query(args = nil, &block)
  if args.nil? || args.empty?
    if block
      select &block
    else
      raise ArgumentError, 'query requires splat of key-value pairs, or a select block'
    end
  elsif args.size == 1
    k, v = args.first
    if k == :id
      [@id_hash[v]]
    else
      select {|e| e.send(k) == v}
    end
  else
    query do |e|
      match = true
      args.each do |k, v|
        unless e.send(k) == v
          match = false
          break
        end
      end
      match
    end
  end
end
Also aliased as: where
reactive_array?() click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 21
def reactive_array?
  self.class.reactive_array?
end
reduce(seed, &block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 94
def reduce(seed, &block)
  @contents.reduce(seed, &block)
end
Also aliased as: inject
reject(&block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 84
def reject(&block)
  @contents.reject(&block)
end
select(&block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 80
def select(&block)
  @contents.select(&block)
end
size() click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 40
def size
  @contents.size
end
sort(&block) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 76
def sort(&block)
  @contents.sort(&block)
end
to_a() click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 34
def to_a
  # not sure what reactive array does
  # so map contents into normal array
  @contents.map{|e|e}
end
where(args = nil, &block)
Alias for: query

Private Instance Methods

__append__(model, notify: true) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 170
def __append__(model, notify: true)
  @contents.append(model)
  @id_hash[model.id] = model
  observe(:add, model) if notify
  self
end
__clear__() click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 151
def __clear__
  @contents.clear
  @id_hash.clear
end
__delete__(model, notify: true) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 146
def __delete__(model, notify: true)
  i = find_index(model)
  __delete_at__(i, notify: notify) if i
end
__delete_at__(index, notify: true) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 137
def __delete_at__(index, notify: true)
  model = @contents.delete_at(index)
  if model
    @id_hash.delete(model.id)
    observe(:remove, model) if notify
  end
  model
end
__remove__(model, error_if_absent: true) click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 160
def __remove__(model, error_if_absent: true)
  index = index {|e| e.id == model.id }
  if index
    __delete_at__(index)
  elsif error_if_absent
    msg = "could not find #{model.class.name} with id #{model.id} to delete"
    raise RuntimeError, msg
  end
end
__remove_if_present__() click to toggle source
# File lib/volt/repo_cache/model_array.rb, line 156
def __remove_if_present__
  __remove__(model, error_if_absent: false)
end