Class: Munson::Document

Inherits:
Object
  • 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.



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

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/munson/document.rb', line 3

def id
  @id
end

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

#attributesObject



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

#changedObject



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

#changesObject



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

#dataObject



32
33
34
# File 'lib/munson/document.rb', line 32

def data
  @jsonapi_document[:data]
end

#errorsObject



84
85
86
# File 'lib/munson/document.rb', line 84

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

#includedObject



36
37
38
# File 'lib/munson/document.rb', line 36

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


93
94
95
# File 'lib/munson/document.rb', line 93

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

#metaObject



97
98
99
# File 'lib/munson/document.rb', line 97

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



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

Parameters:

  • name (Symbol)

    of relationship



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

#relationshipsObject

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

#urlObject



76
77
78
# File 'lib/munson/document.rb', line 76

def url
  links[:self]
end