Module: Toy::Querying::ClassMethods

Defined in:
lib/toy/querying.rb

Instance Method Summary collapse

Instance Method Details

#key?(id, options = nil) ⇒ Boolean Also known as: has_key?

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/toy/querying.rb', line 59

def key?(id, options = nil)
  default_payload = {
    :id      => id,
    :options => options,
    :model => self,
  }

  Toy.instrumenter.instrument('key.toystore', default_payload) { |payload|
    result = adapter.key?(id, options)
    payload[:hit] = result
    result
  }
end

#load(id, attrs) ⇒ Object



74
75
76
77
78
# File 'lib/toy/querying.rb', line 74

def load(id, attrs)
  attrs ||= {}
  instance = constant_from_attrs(attrs).allocate
  instance.initialize_from_database(attrs.update('id' => id))
end

#read(id, options = nil) ⇒ Object Also known as: get, find



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/toy/querying.rb', line 6

def read(id, options = nil)
  default_payload = {
    :id      => id,
    :options => options,
    :model   => self,
    :hit     => false, # default to not found
  }

  Toy.instrumenter.instrument('read.toystore', default_payload) { |payload|
    if (attrs = adapter.read(id, options))
      payload[:hit] = true
      load(id, attrs)
    end
  }
end

#read!(id, options = nil) ⇒ Object Also known as: get!, find!



25
26
27
# File 'lib/toy/querying.rb', line 25

def read!(id, options = nil)
  read(id, options) || raise(Toy::NotFound.new(id))
end

#read_multiple(ids, options = nil) ⇒ Object Also known as: get_multiple, find_multiple



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/toy/querying.rb', line 32

def read_multiple(ids, options = nil)
  default_payload = {
    :ids     => ids,
    :options => options,
    :model   => self,
    :hits    => 0,
    :misses  => 0,
  }

  Toy.instrumenter.instrument('read_multiple.toystore', default_payload) { |payload|
    result = adapter.read_multiple(ids, options)
    result.each do |id, attrs|
      result[id] = if attrs.nil?
        payload[:misses] += 1
        nil
      else
        payload[:hits] += 1
        load(id, attrs)
      end
    end
    result
  }
end