Class: JSON::Api::Vanilla::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/json-api-vanilla/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, links: {}, rel_links: {}, meta: {}, keys: {}, objects: {}, errors: [], container: Module.new, superclass: Class.new) ⇒ Document

Returns a new instance of Document.



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/json-api-vanilla/parser.rb', line 201

def initialize(data, links: {}, rel_links: {}, meta: {},
               keys: {}, objects: {}, errors: [],
               container: Module.new, superclass: Class.new)
  @data = data
  @links = links
  @rel_links = rel_links
  @meta = meta
  @keys = keys
  @objects = objects
  @errors = errors
  @container = container
  @superclass = superclass
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



199
200
201
# File 'lib/json-api-vanilla/parser.rb', line 199

def container
  @container
end

#dataObject+ (readonly)

Returns the content of the JSON API data.

Returns:

  • (Object, Array<Object>)

    the content of the JSON API data.



185
186
187
# File 'lib/json-api-vanilla/parser.rb', line 185

def data
  @data
end

#errorsArray (readonly)

Returns a list of errors, if any, otherwise nil.

Returns:

  • (Array)

    a list of errors, if any, otherwise nil.



195
196
197
# File 'lib/json-api-vanilla/parser.rb', line 195

def errors
  @errors
end

#keysHash (readonly)

Returns a map from objects to a Hash from their original field names (non-snake_case’d) to the corresponding object.

Returns:

  • (Hash)

    a map from objects to a Hash from their original field names (non-snake_case’d) to the corresponding object.



198
199
200
# File 'lib/json-api-vanilla/parser.rb', line 198

def keys
  @keys
end

Returns a map from objects (obtained from .data) to their links, as a Hash.

Returns:

  • (Hash)

    a map from objects (obtained from .data) to their links, as a Hash.



188
189
190
# File 'lib/json-api-vanilla/parser.rb', line 188

def links
  @links
end

#metaHash (readonly)

Returns a map from objects to their meta information (a Hash).

Returns:

  • (Hash)

    a map from objects to their meta information (a Hash).



193
194
195
# File 'lib/json-api-vanilla/parser.rb', line 193

def meta
  @meta
end

Returns a map from objects’ relationships (obtained from .data) to the links defined in that relationship, as a Hash.

Returns:

  • (Hash)

    a map from objects’ relationships (obtained from .data) to the links defined in that relationship, as a Hash.



191
192
193
# File 'lib/json-api-vanilla/parser.rb', line 191

def rel_links
  @rel_links
end

#superclassObject (readonly)

Returns the value of attribute superclass.



200
201
202
# File 'lib/json-api-vanilla/parser.rb', line 200

def superclass
  @superclass
end

Instance Method Details

#find(type, id) ⇒ Object

Get a JSON API object.

Parameters:

  • type (String)

    the type of the object we want returned.

  • id (String)

    its id.

Returns:

  • (Object)

    the object with that type and id.



220
221
222
# File 'lib/json-api-vanilla/parser.rb', line 220

def find(type, id)
  @objects[[type, id]]
end

#find_all(type) ⇒ Array<Object>

Get all JSON API objects of a given type.

Parameters:

  • type (String)

    the type of the objects we want returned.

Returns:

  • (Array<Object>)

    the list of objects with that type.



228
229
230
# File 'lib/json-api-vanilla/parser.rb', line 228

def find_all(type)
  @objects.values.select { |obj| obj.type == type }
end