Class: JsonapiCompliable::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_compliable/scope.rb

Instance Method Summary collapse

Constructor Details

#initialize(object, resource, query, opts = {}) ⇒ Scope

Returns a new instance of Scope.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/jsonapi_compliable/scope.rb', line 3

def initialize(object, resource, query, opts = {})
  @object    = object
  @resource  = resource
  @query     = query

  # Namespace for the 'outer' or 'main' resource is its type
  # For its relationships, its the relationship name
  # IOW when hitting /states, it's resource type 'states
  # when hitting /authors?include=state its 'state'
  @namespace = opts.delete(:namespace) || resource.type

  apply_scoping(opts)
end

Instance Method Details

#query_hashObject



31
32
33
# File 'lib/jsonapi_compliable/scope.rb', line 31

def query_hash
  @query_hash ||= @query.to_hash[@namespace]
end

#resolveObject



21
22
23
24
25
26
27
28
29
# File 'lib/jsonapi_compliable/scope.rb', line 21

def resolve
  if @query.zero_results?
    []
  else
    resolved = @resource.resolve(@object)
    sideload(resolved, query_hash[:include]) if query_hash[:include]
    resolved
  end
end

#resolve_statsObject



17
18
19
# File 'lib/jsonapi_compliable/scope.rb', line 17

def resolve_stats
  Stats::Payload.new(@resource, query_hash, @unpaginated_object).generate
end