Module: Toy::Querying::ClassMethods
- Defined in:
- lib/toy/querying.rb
Instance Method Summary collapse
- #key?(id, options = nil) ⇒ Boolean (also: #has_key?)
- #load(id, attrs) ⇒ Object
- #read(id, options = nil) ⇒ Object (also: #get, #find)
- #read!(id, options = nil) ⇒ Object (also: #get!, #find!)
- #read_multiple(ids, options = nil) ⇒ Object (also: #get_multiple, #find_multiple)
Instance Method Details
#key?(id, options = nil) ⇒ Boolean Also known as: has_key?
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/toy/querying.rb', line 59 def key?(id, = nil) default_payload = { :id => id, :options => , :model => self, } Toy.instrumenter.instrument('key.toystore', default_payload) { |payload| result = adapter.key?(id, ) 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, = nil) default_payload = { :id => id, :options => , :model => self, :hit => false, # default to not found } Toy.instrumenter.instrument('read.toystore', default_payload) { |payload| if (attrs = adapter.read(id, )) 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, = nil) read(id, ) || 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, = nil) default_payload = { :ids => ids, :options => , :model => self, :hits => 0, :misses => 0, } Toy.instrumenter.instrument('read_multiple.toystore', default_payload) { |payload| result = adapter.read_multiple(ids, ) 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 |