Class: Munson::Document

Inherits:
Object show all
Defined in:
lib/munson/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/munson/document.rb', line 5

def id
  @id
end

#typeObject (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

#attributesObject



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

#changedObject



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

#changesObject



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

#dataObject



34
35
36
# File 'lib/munson/document.rb', line 34

def data
  @jsonapi_document[:data]
end

#errorsObject



86
87
88
# File 'lib/munson/document.rb', line 86

def errors
  data[:errors] || []
end

#includedObject



38
39
40
# File 'lib/munson/document.rb', line 38

def included
  @jsonapi_document[:included] || []
end


95
96
97
# File 'lib/munson/document.rb', line 95

def links
  data[:links] || {}
end

#metaObject



99
100
101
# File 'lib/munson/document.rb', line 99

def meta
  data[:meta] || {}
end

#payloadHash

Returns hash for persisting this JSON API Resource via POST/PATCH/PUT.

Returns:

  • (Hash)

    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

Parameters:

  • name (Symbol)

    of relationship



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

#relationshipsObject

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

#urlObject



78
79
80
# File 'lib/munson/document.rb', line 78

def url
  links[:self]
end