Class: McCracken::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
-
#destroy(agent) ⇒ Object
Allow destroying via DELETE.
- #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/mccracken/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/mccracken/document.rb', line 5 def id @id end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/mccracken/document.rb', line 6 def type @type end |
Instance Method Details
#[](key) ⇒ Object
87 88 89 |
# File 'lib/mccracken/document.rb', line 87 def [](key) attributes[key] end |
#attributes ⇒ Object
42 43 44 |
# File 'lib/mccracken/document.rb', line 42 def attributes @attributes end |
#attributes=(attrs) ⇒ Object
46 47 48 |
# File 'lib/mccracken/document.rb', line 46 def attributes=(attrs) @attributes.merge!(attrs) end |
#changed ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/mccracken/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/mccracken/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/mccracken/document.rb', line 34 def data @jsonapi_document[:data] end |
#destroy(agent) ⇒ Object
Allow destroying via DELETE
69 70 71 |
# File 'lib/mccracken/document.rb', line 69 def destroy(agent) agent.delete(id: id).success? end |
#errors ⇒ Object
91 92 93 |
# File 'lib/mccracken/document.rb', line 91 def errors data[:errors] || [] end |
#included ⇒ Object
38 39 40 |
# File 'lib/mccracken/document.rb', line 38 def included @jsonapi_document[:included] || [] end |
#links ⇒ Object
100 101 102 |
# File 'lib/mccracken/document.rb', line 100 def links data[:links] || {} end |
#meta ⇒ Object
104 105 106 |
# File 'lib/mccracken/document.rb', line 104 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/mccracken/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 McCracken::Document from #relationships
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mccracken/document.rb', line 110 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, " The relationship `\#{name}` was called, but does not exist on the document.\n Relationships available are: \#{relationships.keys.join(',')}\n ERR\n end\nend\n" |
#relationship_data(name) ⇒ Object
123 124 125 |
# File 'lib/mccracken/document.rb', line 123 def relationship_data(name) relationships[name] ? relationships[name][:data] : nil end |
#relationships ⇒ Object
Raw relationship hashes
96 97 98 |
# File 'lib/mccracken/document.rb', line 96 def relationships data[:relationships] || {} end |
#save(agent) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/mccracken/document.rb', line 73 def save(agent) response = if id agent.patch(id: id.to_s, body: payload) else agent.post(body: payload) end McCracken::Document.new(response.body) end |
#url ⇒ Object
83 84 85 |
# File 'lib/mccracken/document.rb', line 83 def url links[:self] end |