Class: Volt::RepoCache::ModelArray

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

Direct Known Subclasses

Collection

Class Method Summary collapse

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.



13
14
15
16
17
18
19
# 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

Class Method Details

.reactive_array?Boolean

for benefit of Volt::Watch

Returns:

  • (Boolean)


9
10
11
# File 'lib/volt/repo_cache/model_array.rb', line 9

def self.reactive_array?
  true
end

Instance Method Details

#[](index) ⇒ Object



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

def [](index)
  @contents[index]
end

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



88
89
90
# File 'lib/volt/repo_cache/model_array.rb', line 88

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

#count(&block) ⇒ Object



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

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

#detect(*args, &block) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#each_with_index(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @contents.empty
end

#firstObject



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

def first
  @contents.first
end

#index(*args, &block) ⇒ Object



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

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

#lastObject



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

def last
  @contents.last
end

#observe(action, model) ⇒ Object

subclasses may override if interested.



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

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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

#reactive_array?Boolean

Returns:

  • (Boolean)


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

def reactive_array?
  self.class.reactive_array?
end

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



94
95
96
# File 'lib/volt/repo_cache/model_array.rb', line 94

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

#reject(&block) ⇒ Object



84
85
86
# File 'lib/volt/repo_cache/model_array.rb', line 84

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

#select(&block) ⇒ Object



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

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

#sizeObject



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

def size
  @contents.size
end

#sort(&block) ⇒ Object



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

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

#to_aObject



34
35
36
37
38
# 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