Class: Munson::Document
- Inherits:
-
Object
- Object
- Munson::Document
- Defined in:
- lib/munson/document.rb
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.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/munson/document.rb', line 6 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].clone @attributes = jsonapi_document[:data][:attributes].clone else @original_attributes = {} @attributes = {} end end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/munson/document.rb', line 3 def id @id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
4 5 6 |
# File 'lib/munson/document.rb', line 4 def type @type end |
Instance Method Details
#[](key) ⇒ Object
80 81 82 |
# File 'lib/munson/document.rb', line 80 def [](key) attributes[key] end |
#attributes ⇒ Object
40 41 42 |
# File 'lib/munson/document.rb', line 40 def attributes @attributes end |
#attributes=(attrs) ⇒ Object
44 45 46 |
# File 'lib/munson/document.rb', line 44 def attributes=(attrs) @attributes.merge!(attrs) end |
#changed ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/munson/document.rb', line 57 def changed attributes.reduce({}) do |memo, (k,v)| if @original_attributes[k] != attributes[k] memo[k] = attributes[k] end memo end end |
#changes ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/munson/document.rb', line 48 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
32 33 34 |
# File 'lib/munson/document.rb', line 32 def data @jsonapi_document[:data] end |
#errors ⇒ Object
84 85 86 |
# File 'lib/munson/document.rb', line 84 def errors data[:errors] || [] end |
#included ⇒ Object
36 37 38 |
# File 'lib/munson/document.rb', line 36 def included @jsonapi_document[:included] || [] end |
#links ⇒ Object
93 94 95 |
# File 'lib/munson/document.rb', line 93 def links data[:links] || {} end |
#meta ⇒ Object
97 98 99 |
# File 'lib/munson/document.rb', line 97 def data[:meta] || {} end |
#payload ⇒ Hash
Returns hash for persisting this JSON API Resource via POST/PATCH/PUT.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/munson/document.rb', line 21 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
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/munson/document.rb', line 103 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
116 117 118 |
# File 'lib/munson/document.rb', line 116 def relationship_data(name) relationships[name] ? relationships[name][:data] : nil end |
#relationships ⇒ Object
Raw relationship hashes
89 90 91 |
# File 'lib/munson/document.rb', line 89 def relationships data[:relationships] || {} end |
#save(agent) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/munson/document.rb', line 66 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
76 77 78 |
# File 'lib/munson/document.rb', line 76 def url links[:self] end |