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.
152 153 154 155 156 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 152 def initialize(folder, = {}) @folder = folder @adapter = folder.adapter.value = end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
150 151 152 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 150 def adapter @adapter end |
#folder ⇒ Object (readonly)
Returns the value of attribute folder.
150 151 152 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 150 def folder @folder end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
150 151 152 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 150 def end |
Instance Method Details
#as_json ⇒ Object
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 158 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? return hash end |
#documents ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 169 def documents @included = [] return folder.map{|document| _doc = adapter.new(document, ).as_json @included << _doc[:included] _doc[:data] } || [] end |
#included ⇒ Object
178 179 180 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 178 def included (@included || []).flatten end |
#links ⇒ Object
189 190 191 192 193 194 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 189 def links @links ||= folder.links.inject({}){ |hash, link| hash[transform_key(link.name)] = link.value hash } end |
#metas ⇒ Object
182 183 184 185 186 187 |
# File 'lib/simple_ams/adapters/jsonapi.rb', line 182 def ||= folder..inject({}){ |hash, | hash[transform_key(.name)] = .value hash } end |