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 Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#unpaginated_object ⇒ Object
readonly
Returns the value of attribute unpaginated_object.
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
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/jsonapi_compliable/scope.rb', line 29 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 Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object
17 18 19 |
# File 'lib/jsonapi_compliable/scope.rb', line 17 def object @object end |
#unpaginated_object ⇒ Object (readonly)
Returns the value of attribute unpaginated_object
17 18 19 |
# File 'lib/jsonapi_compliable/scope.rb', line 17 def unpaginated_object @unpaginated_object 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
75 76 77 |
# File 'lib/jsonapi_compliable/scope.rb', line 75 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
62 63 64 65 66 67 68 69 70 |
# File 'lib/jsonapi_compliable/scope.rb', line 62 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 } }
49 50 51 |
# File 'lib/jsonapi_compliable/scope.rb', line 49 def resolve_stats Stats::Payload.new(@resource, query_hash, @unpaginated_object).generate end |