Module: ExposeDB::Querying
- Included in:
- Model
- Defined in:
- lib/expose_db/model/querying.rb
Instance Method Summary collapse
- #all ⇒ Object
- #filter(query, *values) ⇒ Object
-
#find(id) ⇒ Object
Find a record and raise RecordNotFound if it isn’t found.
-
#find_by_id(id) ⇒ Object
Find a record or return nil if it isn’t found.
Instance Method Details
#all ⇒ Object
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) = {} if query [:query] = {q: query} [:query][:values] = values if values end results = MultiJson.load get("/#{exposed_as}", ) 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 |