Class: Munson::Document
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #attributes ⇒ Object
- #attributes=(attrs) ⇒ Object
- #changed ⇒ Object
- #changes ⇒ Object
- #data ⇒ Object
- #errors ⇒ Object
- #included ⇒ Object
-
#initialize(jsonapi_document) ⇒ Document
constructor
A new instance of Document.
- #links ⇒ Object
- #meta ⇒ Object
-
#payload ⇒ Hash
Hash for persisting this JSON API Resource via POST/PATCH/PUT.
-
#relationship(name) ⇒ Object
Initialized Document from #relationships.
- #relationship_data(name) ⇒ Object
-
#relationships ⇒ Object
Raw relationship hashes.
- #save(agent) ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(jsonapi_document) ⇒ Document
Returns a new instance of Document.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/munson/document.rb', line 8 def initialize(jsonapi_document) @id = jsonapi_document[:data][:id] @type = jsonapi_document[:data][:type].to_sym @jsonapi_document = jsonapi_document if jsonapi_document[:data] && jsonapi_document[:data][:attributes] @original_attributes = jsonapi_document[:data][:attributes] @attributes = jsonapi_document[:data][:attributes].deep_dup else @original_attributes = {} @attributes = {} end end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
5 6 7 |
# File 'lib/munson/document.rb', line 5 def id @id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/munson/document.rb', line 6 def type @type end |
Instance Method Details
#[](key) ⇒ Object
82 83 84 |
# File 'lib/munson/document.rb', line 82 def [](key) attributes[key] end |
#attributes ⇒ Object
42 43 44 |
# File 'lib/munson/document.rb', line 42 def attributes @attributes end |
#attributes=(attrs) ⇒ Object
46 47 48 |
# File 'lib/munson/document.rb', line 46 def attributes=(attrs) @attributes.merge!(attrs) end |
#changed ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/munson/document.rb', line 59 def changed attributes.reduce({}) do |memo, (k,v)| if @original_attributes[k] != attributes[k] memo[k] = attributes[k] end memo end end |
#changes ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/munson/document.rb', line 50 def changes attributes.reduce({}) do |memo, (k,v)| if @original_attributes[k] != attributes[k] memo[k] = [@original_attributes[k], attributes[k]] end memo end end |
#data ⇒ Object
34 35 36 |
# File 'lib/munson/document.rb', line 34 def data @jsonapi_document[:data] end |
#errors ⇒ Object
86 87 88 |
# File 'lib/munson/document.rb', line 86 def errors data[:errors] || [] end |
#included ⇒ Object
38 39 40 |
# File 'lib/munson/document.rb', line 38 def included @jsonapi_document[:included] || [] end |
#links ⇒ Object
95 96 97 |
# File 'lib/munson/document.rb', line 95 def links data[:links] || {} end |
#meta ⇒ Object
99 100 101 |
# File 'lib/munson/document.rb', line 99 def data[:meta] || {} end |
#payload ⇒ Hash
Returns hash for persisting this JSON API Resource via POST/PATCH/PUT.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/munson/document.rb', line 23 def payload doc = { data: { type: @type } } if id doc[:data][:id] = id doc[:data][:attributes] = changed else doc[:data][:attributes] = attributes end doc end |
#relationship(name) ⇒ Object
Initialized Munson::Document from #relationships
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/munson/document.rb', line 105 def relationship(name) if relationship_data(name).is_a?(Array) relationship_data(name).map { || find_included_item() } elsif relationship_data(name).is_a?(Hash) find_included_item(relationship_data(name)) else raise RelationshipNotFound, <<-ERR The relationship `#{name}` was called, but does not exist on the document. Relationships available are: #{relationships.keys.join(',')} ERR end end |
#relationship_data(name) ⇒ Object
118 119 120 |
# File 'lib/munson/document.rb', line 118 def relationship_data(name) relationships[name] ? relationships[name][:data] : nil end |
#relationships ⇒ Object
Raw relationship hashes
91 92 93 |
# File 'lib/munson/document.rb', line 91 def relationships data[:relationships] || {} end |
#save(agent) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/munson/document.rb', line 68 def save(agent) response = if id agent.patch(id: id.to_s, body: payload) else agent.post(body: payload) end Munson::Document.new(response.body) end |
#url ⇒ Object
78 79 80 |
# File 'lib/munson/document.rb', line 78 def url links[:self] end |