Module: ExposeDB::Querying

Included in:
Model
Defined in:
lib/expose_db/model/querying.rb

Instance Method Summary collapse

Instance Method Details

#allObject



3
4
5
# File 'lib/expose_db/model/querying.rb', line 3

def all
  filter(nil)
end

#filter(query, *values) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/expose_db/model/querying.rb', line 7

def filter(query, *values)
  options = {}
  if query
    options[:query] = {q: query}
    options[:query][:values] = values if values
  end

  results = MultiJson.load get("/#{exposed_as}", options)
  results.map { |json| new(json) }
end

#find(id) ⇒ Object

Find a record and raise RecordNotFound if it isn’t found.



19
20
21
22
23
24
25
# File 'lib/expose_db/model/querying.rb', line 19

def find(id)
  find_by_id(id).tap { |result|
    if result.nil?
      raise RecordNotFound, "#{self.class.name}#find with ID #{id.inspect} was not found"
    end
  }
end

#find_by_id(id) ⇒ Object

Find a record or return nil if it isn’t found.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/expose_db/model/querying.rb', line 28

def find_by_id(id)
  resp = get("/#{exposed_as}/#{id}")
  case resp.response.code.to_i
  when 200
    result = MultiJson.load resp.parsed_response
    new(result)
  when 404
    nil
  else
    raise "#{self.class.name}#try_find with ID #{id.inspect} returned unexpected response: #{resp.inspect}"
  end
end