Class: Praxis::Mapper::Resources::QueryProxy
- Inherits:
-
Object
- Object
- Praxis::Mapper::Resources::QueryProxy
- Defined in:
- lib/praxis/mapper/resources/query_proxy.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
-
#all(condition = {}) ⇒ Object
Retrieve all or many wrapped resources .all -> returns them all .all(name: ‘foo’) -> returns all that match the name.
- #first ⇒ Object
-
#get(condition) ⇒ Object
Can pass extra includes through :_includes TODO: We should not use the AR includes, but first pass them through the properties, cause we need to expand based on the resource methods, not the model methods.
- #get!(condition) ⇒ Object
- #including(includes) ⇒ Object
-
#initialize(klass:) ⇒ QueryProxy
constructor
A new instance of QueryProxy.
- #last ⇒ Object
Constructor Details
#initialize(klass:) ⇒ QueryProxy
Returns a new instance of QueryProxy.
9 10 11 |
# File 'lib/praxis/mapper/resources/query_proxy.rb', line 9 def initialize(klass:) @klass = klass end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
7 8 9 |
# File 'lib/praxis/mapper/resources/query_proxy.rb', line 7 def klass @klass end |
Instance Method Details
#all(condition = {}) ⇒ Object
Retrieve all or many wrapped resources .all -> returns them all .all(name: ‘foo’) -> returns all that match the name
39 40 41 42 43 44 |
# File 'lib/praxis/mapper/resources/query_proxy.rb', line 39 def all(condition = {}) base = klass.model._add_includes(klass.model, @_includes) # includes(nil) seems to have no effect records = base._all(condition) klass.wrap(records) end |
#first ⇒ Object
46 47 48 49 |
# File 'lib/praxis/mapper/resources/query_proxy.rb', line 46 def first record = klass.model._first record.nil? ? nil : klass.wrap(record) end |
#get(condition) ⇒ Object
Can pass extra includes through :_includes TODO: We should not use the AR includes, but first pass them through the properties, cause
we need to expand based on the resource methods, not the model methods
21 22 23 24 25 26 |
# File 'lib/praxis/mapper/resources/query_proxy.rb', line 21 def get(condition) base = klass.model._add_includes(klass.model, @_includes) # includes(nil) seems to have no effect record = base._get(condition) record.nil? ? nil : klass.for_record(record) end |
#get!(condition) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/praxis/mapper/resources/query_proxy.rb', line 28 def get!(condition) resource = get(condition) # TODO: passing the :id if there is one in the condition...but can be more complex...think if we want to expose more raise Praxis::Mapper::ResourceNotFound.new(type: @klass, id: condition[:id]) unless resource resource end |
#including(includes) ⇒ Object
13 14 15 16 |
# File 'lib/praxis/mapper/resources/query_proxy.rb', line 13 def including(includes) @_includes = includes self end |
#last ⇒ Object
51 52 53 54 |
# File 'lib/praxis/mapper/resources/query_proxy.rb', line 51 def last record = klass.model._last record.nil? ? nil : klass.wrap(record) end |