Class: Onsi::Resource
- Inherits:
-
Object
- Object
- Onsi::Resource
- Defined in:
- lib/onsi/resource.rb
Overview
The wrapper for generating a object
Defined Under Namespace
Classes: InvalidResourceError
Instance Attribute Summary collapse
-
#includes ⇒ Array<Onsi::Includes>
readonly
The includes for the object.
-
#object ⇒ Any
readonly
The backing object.
-
#version ⇒ Symbol
readonly
The version to render.
Class Method Summary collapse
-
.as_resource(resource, version) ⇒ Onsi::Resource+
Convert an object into a Onsi::Resource.
-
.render(resource, version) ⇒ Hash
Render a resource to JSON.
Instance Method Summary collapse
-
#as_json(_opts = {}) ⇒ Hash
Creates a raw JSON object.
-
#initialize(object, version = nil, includes: nil) ⇒ Onsi::Resource
constructor
Create a new resouce.
Constructor Details
#initialize(object, version = nil, includes: nil) ⇒ Onsi::Resource
Note:
The object MUST be a single object that includes Onsi::Model
Note:
The includes MUST be an array of Onsi::Include objects.
Create a new resouce.
152 153 154 155 156 157 |
# File 'lib/onsi/resource.rb', line 152 def initialize(object, version = nil, includes: nil) @object = object @version = version || Model::DEFAULT_API_VERSION @includes = includes validate! end |
Instance Attribute Details
#includes ⇒ Array<Onsi::Includes> (readonly)
The includes for the object.
137 138 139 |
# File 'lib/onsi/resource.rb', line 137 def includes @includes end |
#object ⇒ Any (readonly)
Note:
MUST include Onsi::Model
The backing object.
125 126 127 |
# File 'lib/onsi/resource.rb', line 125 def object @object end |
#version ⇒ Symbol (readonly)
The version to render.
131 132 133 |
# File 'lib/onsi/resource.rb', line 131 def version @version end |
Class Method Details
.as_resource(resource, version) ⇒ Onsi::Resource+
Convert an object into a Onsi::Resource
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/onsi/resource.rb', line 71 def as_resource(resource, version) case resource when Onsi::Resource resource when Onsi::Paginate::Result as_resource(resource.query, version) when Enumerable resource.map { |res| as_resource(res, version) } else Onsi::Resource.new(resource, version) end end |
.render(resource, version) ⇒ Hash
Render a resource to JSON
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/onsi/resource.rb', line 93 def render(resource, version) resources = as_resource(resource, version) {}.tap do |root| root[DATA_KEY] = resources.as_json included = all_included(resources) if included.any? root[INCLUDED_KEY] = included end root[META_KEY] = {}.tap do || [:count] = resources.count if resources.respond_to?(:count) if resource.is_a?(Onsi::Paginate::Result) [:pagination] = resource.params end end end end |
Instance Method Details
#as_json(_opts = {}) ⇒ Hash
Creates a raw JSON object.
163 164 165 166 167 168 169 170 171 172 |
# File 'lib/onsi/resource.rb', line 163 def as_json(_opts = {}) {}.tap do |root| root[TYPE_KEY] = type root[ID_KEY] = object_identifier root[ATTRIBUTES_KEY] = generate_attributes append_relationships(root) (root) append_includes(root) end end |