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.



152
153
154
155
156
# File 'lib/simple_ams/adapters/jsonapi.rb', line 152

def initialize(folder, options = {})
  @folder = folder
  @adapter = folder.adapter.value
  @options = options
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



150
151
152
# File 'lib/simple_ams/adapters/jsonapi.rb', line 150

def adapter
  @adapter
end

#folderObject (readonly)

Returns the value of attribute folder.



150
151
152
# File 'lib/simple_ams/adapters/jsonapi.rb', line 150

def folder
  @folder
end

#optionsObject (readonly)

Returns the value of attribute options.



150
151
152
# File 'lib/simple_ams/adapters/jsonapi.rb', line 150

def options
  @options
end

Instance Method Details

#as_jsonObject



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

  return hash
end

#documentsObject



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

#includedObject



178
179
180
# File 'lib/simple_ams/adapters/jsonapi.rb', line 178

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


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

#metasObject



182
183
184
185
186
187
# File 'lib/simple_ams/adapters/jsonapi.rb', line 182

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