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

Instance Method Summary collapse

Constructor Details

#initialize(arguments = {}) ⇒ Document

Returns a new instance of Document.



15
16
17
18
19
20
21
22
23
24
# File 'lib/jacoat/document.rb', line 15

def initialize(arguments = {})
  validate_arguments(arguments)
  @has_data = true if arguments.has_key?(:data)
  @data = Data.process(arguments[:data]) if arguments.has_key?(:data)
  @errors = Error.new(arguments[:errors]) if arguments.has_key?(:errors)
  @meta = Meta.new(arguments[:meta]) if arguments.has_key?(:meta) 
  @jsonapi = Jsonapi.new(arguments[:jsonapi]) if arguments.has_key?(:jsonapi)
  @links = Link.process(arguments[:links]) if arguments.has_key?(:links)
  @included = Included.process(arguments[:included]) if arguments.has_key?(:included)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#includedObject (readonly)

Returns the value of attribute included.



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

def included
  @included
end

#jsonapiObject (readonly)

Returns the value of attribute jsonapi.



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

def jsonapi
  @jsonapi
end

Returns the value of attribute links.



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

def links
  @links
end

#metaObject (readonly)

Returns the value of attribute meta.



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

def meta
  @meta
end

Instance Method Details

#data_hashObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jacoat/document.rb', line 43

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



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

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

#validate_arguments(arguments) ⇒ Object

Raises:



26
27
28
29
30
# File 'lib/jacoat/document.rb', line 26

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