Class: Volt::RepoCache::ModelArray

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/volt/repo_cache/model_array.rb

Direct Known Subclasses

Collection

Instance Method Summary collapse

Methods included from Util

adder, arrify, creator, debug, friend?, friends_only, not_yet_implemented, prefix_method, remover, setter, subclass_responsibility, time, unsupported

Constructor Details

#initialize(observer: nil, contents: nil) ⇒ ModelArray

Returns a new instance of ModelArray.



8
9
10
11
12
13
14
# File 'lib/volt/repo_cache/model_array.rb', line 8

def initialize(observer: nil, contents: nil)
  @contents = Volt::ReactiveArray.new(contents || [])
  @id_hash = {}
  @contents.each do |e|
    @id_hash[e.id] = e
  end
end

Instance Method Details

#collect(&block) ⇒ Object Also known as: map



75
76
77
# File 'lib/volt/repo_cache/model_array.rb', line 75

def collect(&block)
  @contents.collect(&block)
end

#count(&block) ⇒ Object



59
60
61
# File 'lib/volt/repo_cache/model_array.rb', line 59

def count(&block)
  @contents.count(&block)
end

#detect(*args, &block) ⇒ Object



39
40
41
# File 'lib/volt/repo_cache/model_array.rb', line 39

def detect(*args, &block)
  @contents.detect(*args, &block)
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/volt/repo_cache/model_array.rb', line 43

def each(&block)
  @contents.each(&block)
end

#each_with_index(&block) ⇒ Object



47
48
49
# File 'lib/volt/repo_cache/model_array.rb', line 47

def each_with_index(&block)
  @contents.each_with_index(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/volt/repo_cache/model_array.rb', line 35

def empty?
  @contents.empty?
end

#firstObject



51
52
53
# File 'lib/volt/repo_cache/model_array.rb', line 51

def first
  @contents.first
end

#index(*args, &block) ⇒ Object



21
22
23
# File 'lib/volt/repo_cache/model_array.rb', line 21

def index(*args, &block)
  @contents.index(*args, &block)
end

#lastObject



55
56
57
# File 'lib/volt/repo_cache/model_array.rb', line 55

def last
  @contents.last
end

#observe(action, model) ⇒ Object

subclasses may override if interested.



17
18
19
# File 'lib/volt/repo_cache/model_array.rb', line 17

def observe(action, model)
  # no op
end

#query(args = nil, &block) ⇒ Object Also known as: where

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/volt/repo_cache/model_array.rb', line 92

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

#reduce(seed, &block) ⇒ Object Also known as: inject



81
82
83
# File 'lib/volt/repo_cache/model_array.rb', line 81

def reduce(seed, &block)
  @contents.reduce(seed, &block)
end

#reject(&block) ⇒ Object



71
72
73
# File 'lib/volt/repo_cache/model_array.rb', line 71

def reject(&block)
  @contents.reject(&block)
end

#select(&block) ⇒ Object



67
68
69
# File 'lib/volt/repo_cache/model_array.rb', line 67

def select(&block)
  @contents.select(&block)
end

#sizeObject



31
32
33
# File 'lib/volt/repo_cache/model_array.rb', line 31

def size
  @contents.size
end

#sort(&block) ⇒ Object



63
64
65
# File 'lib/volt/repo_cache/model_array.rb', line 63

def sort(&block)
  @contents.sort(&block)
end

#to_aObject



25
26
27
28
29
# File 'lib/volt/repo_cache/model_array.rb', line 25

def to_a
  # not sure what reactive array does
  # so map contents into normal array
  @contents.map{|e|e}
end