Class: Praxis::Mapper::Resources::QueryProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/mapper/resources/query_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#klassObject (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

#firstObject



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

#lastObject



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