Class: JsonapiCompliable::Scope
- Inherits:
-
Object
- Object
- JsonapiCompliable::Scope
- Defined in:
- lib/jsonapi_compliable/scope.rb
Overview
A Scope wraps an underlying object. It modifies that object using the corresponding Resource and Query, and how to resolve that underlying object scope.
Instance Method Summary collapse
-
#initialize(object, resource, query, opts = {}) ⇒ Scope
constructor
A new instance of Scope.
-
#query_hash ⇒ Object
private
The slice of Query#to_hash for the current namespace.
-
#resolve ⇒ Array
Resolve the scope.
-
#resolve_stats ⇒ Hash
private
Resolve the requested stats.
Constructor Details
#initialize(object, resource, query, opts = {}) ⇒ Scope
Returns a new instance of Scope
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jsonapi_compliable/scope.rb', line 27 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_hash ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The slice of Query#to_hash for the current namespace
73 74 75 |
# File 'lib/jsonapi_compliable/scope.rb', line 73 def query_hash @query_hash ||= @query.to_hash[@namespace] end |
#resolve ⇒ Array
Resolve the scope. This is where SQL is actually fired, an HTTP request is actually made, etc.
Does nothing if the user requested zero results, ie /posts?page=0
First resolves the top-level resource, then processes each relevant sideload
60 61 62 63 64 65 66 67 68 |
# File 'lib/jsonapi_compliable/scope.rb', line 60 def resolve if @query.zero_results? [] else resolved = @resource.resolve(@object) sideload(resolved, query_hash[:include]) if query_hash[:include] resolved end end |
#resolve_stats ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Resolve the requested stats. Returns hash like:
{ rating: { average: 5.5, maximum: 9 } }
47 48 49 |
# File 'lib/jsonapi_compliable/scope.rb', line 47 def resolve_stats Stats::Payload.new(@resource, query_hash, @unpaginated_object).generate end |