Module: Toy::Querying::ClassMethods

Defined in:
lib/toy/querying.rb

Instance Method Summary collapse

Instance Method Details

#get(id) ⇒ Object



6
7
8
9
10
# File 'lib/toy/querying.rb', line 6

def get(id)
  if (attrs = adapter.read(id))
    load(id, attrs)
  end
end

#get!(id) ⇒ Object



12
13
14
# File 'lib/toy/querying.rb', line 12

def get!(id)
  get(id) || raise(Toy::NotFound.new(id))
end

#get_multi(*ids) ⇒ Object



16
17
18
# File 'lib/toy/querying.rb', line 16

def get_multi(*ids)
  ids.flatten.map { |id| get(id) }
end

#get_or_create(id) ⇒ Object



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

def get_or_create(id)
  get(id) || create(:id => id)
end

#get_or_new(id) ⇒ Object



20
21
22
# File 'lib/toy/querying.rb', line 20

def get_or_new(id)
  get(id) || new(:id => id)
end

#key?(id) ⇒ Boolean Also known as: has_key?

Returns:



28
29
30
# File 'lib/toy/querying.rb', line 28

def key?(id)
  adapter.key?(id)
end

#load(id, attrs) ⇒ Object



33
34
35
36
37
# File 'lib/toy/querying.rb', line 33

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