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, #included, #relationships, #transform_key

Constructor Details

#initialize(folder, options = {}) ⇒ Collection



139
140
141
142
143
# File 'lib/simple_ams/adapters/jsonapi.rb', line 139

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

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



137
138
139
# File 'lib/simple_ams/adapters/jsonapi.rb', line 137

def adapter
  @adapter
end

#folderObject (readonly)

Returns the value of attribute folder.



137
138
139
# File 'lib/simple_ams/adapters/jsonapi.rb', line 137

def folder
  @folder
end

#optionsObject (readonly)

Returns the value of attribute options.



137
138
139
# File 'lib/simple_ams/adapters/jsonapi.rb', line 137

def options
  @options
end

Instance Method Details

#as_jsonObject



145
146
147
148
149
150
151
152
153
# File 'lib/simple_ams/adapters/jsonapi.rb', line 145

def as_json
  hash = {
    data: documents
  }
  hash.merge!(meta: metas) unless metas.empty?
  hash.merge!(links: links) unless links.empty?

  return hash
end

#documentsObject



155
156
157
158
159
# File 'lib/simple_ams/adapters/jsonapi.rb', line 155

def documents
  return folder.map{|document|
    adapter.new(document).as_json[:data]
  } || []
end


168
169
170
171
172
173
# File 'lib/simple_ams/adapters/jsonapi.rb', line 168

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

#metasObject



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

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