Class: Jacoat::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/jacoat/document.rb,
lib/jacoat/document/data.rb,
lib/jacoat/document/link.rb,
lib/jacoat/document/meta.rb,
lib/jacoat/document/error.rb,
lib/jacoat/document/jsonapi.rb,
lib/jacoat/document/included.rb,
lib/jacoat/document/resource.rb,
lib/jacoat/document/attribute.rb,
lib/jacoat/document/relationship.rb,
lib/jacoat/document/resource_identifier.rb

Defined Under Namespace

Classes: Attributes, Data, Error, Included, Invalid, Jsonapi, Link, Meta, Relationship, Resource, ResourceIdentifier

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



14
15
16
# File 'lib/jacoat/document.rb', line 14

def data
  @data
end

#errorsObject

Returns the value of attribute errors.



15
16
17
# File 'lib/jacoat/document.rb', line 15

def errors
  @errors
end

#includedObject

Returns the value of attribute included.



15
16
17
# File 'lib/jacoat/document.rb', line 15

def included
  @included
end

#jsonapiObject

Returns the value of attribute jsonapi.



15
16
17
# File 'lib/jacoat/document.rb', line 15

def jsonapi
  @jsonapi
end

Returns the value of attribute links.



15
16
17
# File 'lib/jacoat/document.rb', line 15

def links
  @links
end

#metaObject

Returns the value of attribute meta.



15
16
17
# File 'lib/jacoat/document.rb', line 15

def meta
  @meta
end

Class Method Details

.from_jsonapi(arguments) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jacoat/document.rb', line 17

def self.from_jsonapi(arguments)
  document = Document.new
  document.data = Data.from_jsonapi(arguments[:data]) if arguments.has_key?(:data)
  
  
  document.errors = Error.from_jsonapi(arguments[:errors]) if arguments.has_key?(:errors)
  document.meta = Meta.from_jsonapi(arguments[:meta]) if arguments.has_key?(:meta)
  document.jsonapi = Jsonapi.from_jsonapi(arguments[:jsonapi]) if arguments.has_key?(:jsonapi)
  document.links = Link.from_jsonapi(arguments[:links]) if arguments.has_key?(:links)
  document.included = Included.from_jsonapi(arguments[:included]) if arguments.has_key?(:included)
  document
end

Instance Method Details

#data_hashObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jacoat/document.rb', line 59

def data_hash
  return nil if @data.nil? and @has_data
  if @data.is_a?(Array)
    array = []
    @data.each do |data|
      array << data.to_hash
    end
    return array
  else
    @data.to_hash
  end
end

#to_hashObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/jacoat/document.rb', line 48

def to_hash
  hash = {}
  hash[:data] = data_hash if @has_data
  %w{ errors meta jsonapi links included }.each do |type|
    if self.instance_variable_defined?("@#{type}".to_sym)
      hash[type.to_sym] = instance_variable_get("@#{type}".to_sym).to_hash
    end
  end
  hash
end

#valid?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/jacoat/document.rb', line 35

def valid?
  validate_arguments
  true
rescue Invalid
  return false
end

#validate_argumentsObject

Raises:



42
43
44
45
46
# File 'lib/jacoat/document.rb', line 42

def validate_arguments
  raise Invalid.new('included key without data key') if @included && !@data
  raise Invalid.new('must contain data, errors or meta key') if !@data && !@errors && !@meta
  raise Invalid.new('data and errors keys set') if @data && @errors
end