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.



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

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.



202
203
204
# File 'lib/json-api-vanilla/parser.rb', line 202

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.



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

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.



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

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.



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

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.



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

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).



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

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.



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

def rel_links
  @rel_links
end

#superclassObject (readonly)

Returns the value of attribute superclass.



203
204
205
# File 'lib/json-api-vanilla/parser.rb', line 203

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.



223
224
225
# File 'lib/json-api-vanilla/parser.rb', line 223

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.



231
232
233
# File 'lib/json-api-vanilla/parser.rb', line 231

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