Class: SimpleAMS::Adapters::JSONAPI::Collection
- Inherits:
-
SimpleAMS::Adapters::JSONAPI
- Object
- SimpleAMS::Adapters::JSONAPI
- SimpleAMS::Adapters::JSONAPI::Collection
- Defined in:
- lib/simple_ams/adapters/jsonapi.rb
Constant Summary
Constants inherited from SimpleAMS::Adapters::JSONAPI
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#folder ⇒ Object
readonly
Returns the value of attribute folder.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Attributes inherited from SimpleAMS::Adapters::JSONAPI
Instance Method Summary collapse
- #as_json ⇒ Object
- #documents ⇒ Object
- #included ⇒ Object
-
#initialize(folder, options = {}) ⇒ Collection
constructor
A new instance of Collection.
- #links ⇒ Object
- #metas ⇒ Object
Methods inherited from SimpleAMS::Adapters::JSONAPI
#data, #embedded_relation_data_for, #embedded_relation_links_for, #fields, #forms, #relationships, #transform_key
Constructor Details
#initialize(folder, options = {}) ⇒ Collection
Returns a new instance of Collection.
136 137 138 139 140 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 136 def initialize(folder, = {}) @folder = folder @adapter = folder.adapter.value @options = end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
134 135 136 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 134 def adapter @adapter end |
#folder ⇒ Object (readonly)
Returns the value of attribute folder.
134 135 136 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 134 def folder @folder end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
134 135 136 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 134 def @options end |
Instance Method Details
#as_json ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 142 def as_json hash = { data: documents } hash.merge!(meta: ) unless .empty? hash.merge!(links: links) unless links.empty? hash.merge!(included: included) unless included.empty? hash end |
#documents ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 153 def documents @included = [] folder.map do |document| doc = adapter.new(document, ).as_json @included << doc[:included] doc[:data] end || [] end |
#included ⇒ Object
162 163 164 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 162 def included (@included || []).flatten end |
#links ⇒ Object
172 173 174 175 176 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 172 def links @links ||= folder.links.each_with_object({}) do |link, hash| hash[transform_key(link.name)] = link.value end end |
#metas ⇒ Object
166 167 168 169 170 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 166 def @metas ||= folder..each_with_object({}) do |, hash| hash[transform_key(.name)] = .value end end |