Class: Jacoat::Document
- Inherits:
-
Object
- Object
- Jacoat::Document
- 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
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#included ⇒ Object
readonly
Returns the value of attribute included.
-
#jsonapi ⇒ Object
readonly
Returns the value of attribute jsonapi.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
Instance Method Summary collapse
- #data_hash ⇒ Object
-
#initialize(arguments = {}) ⇒ Document
constructor
A new instance of Document.
- #to_hash ⇒ Object
- #validate_arguments(arguments) ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
14 15 16 |
# File 'lib/jacoat/document.rb', line 14 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
14 15 16 |
# File 'lib/jacoat/document.rb', line 14 def errors @errors end |
#included ⇒ Object (readonly)
Returns the value of attribute included.
14 15 16 |
# File 'lib/jacoat/document.rb', line 14 def included @included end |
#jsonapi ⇒ Object (readonly)
Returns the value of attribute jsonapi.
14 15 16 |
# File 'lib/jacoat/document.rb', line 14 def jsonapi @jsonapi end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
14 15 16 |
# File 'lib/jacoat/document.rb', line 14 def links @links end |
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
14 15 16 |
# File 'lib/jacoat/document.rb', line 14 def @meta end |
Instance Method Details
#data_hash ⇒ Object
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_hash ⇒ Object
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
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 |