Class: SimpleAMS::Adapters::JSONAPI::Collection

Inherits:
SimpleAMS::Adapters::JSONAPI show all
Defined in:
lib/simple_ams/adapters/jsonapi.rb

Constant Summary

Constants inherited from SimpleAMS::Adapters::JSONAPI

DEFAULT_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from SimpleAMS::Adapters::JSONAPI

#document

Instance Method Summary collapse

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, options = {})
  @folder = folder
  @adapter = folder.adapter.value
  @options = options
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



134
135
136
# File 'lib/simple_ams/adapters/jsonapi.rb', line 134

def adapter
  @adapter
end

#folderObject (readonly)

Returns the value of attribute folder.



134
135
136
# File 'lib/simple_ams/adapters/jsonapi.rb', line 134

def folder
  @folder
end

#optionsObject (readonly)

Returns the value of attribute options.



134
135
136
# File 'lib/simple_ams/adapters/jsonapi.rb', line 134

def options
  @options
end

Instance Method Details

#as_jsonObject



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: metas) unless metas.empty?
  hash.merge!(links: links) unless links.empty?
  hash.merge!(included: included) unless included.empty?

  hash
end

#documentsObject



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, options).as_json
    @included << doc[:included]
    doc[:data]
  end || []
end

#includedObject



162
163
164
# File 'lib/simple_ams/adapters/jsonapi.rb', line 162

def included
  (@included || []).flatten
end


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

#metasObject



166
167
168
169
170
# File 'lib/simple_ams/adapters/jsonapi.rb', line 166

def metas
  @metas ||= folder.metas.each_with_object({}) do |meta, hash|
    hash[transform_key(meta.name)] = meta.value
  end
end