Class: Graphiti::ActiveGraph::Deserializer

Inherits:
Deserializer
  • Object
show all
Includes:
Concerns::PathRelationships
Defined in:
lib/graphiti/active_graph/deserializer.rb

Defined Under Namespace

Classes: Conflict

Instance Method Summary collapse

Methods included from Concerns::PathRelationships

#add_path_id_to_relationships!

Constructor Details

#initialize(payload, env = nil, model = nil, parent_map = nil) ⇒ Deserializer

Returns a new instance of Deserializer.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/graphiti/active_graph/deserializer.rb', line 19

def initialize(payload, env = nil, model = nil, parent_map = nil)
  super(payload)

  @params = payload
  @model = model
  @parent_map = parent_map || {}
  @env = env

  return unless data.blank? && env && parsable_content?

  raise ArgumentError, "JSON API payload must contain the 'data' key" 
end

Instance Method Details

#detect_conflict(key, path_value, body_value) ⇒ Object

Raises:



111
112
113
# File 'lib/graphiti/active_graph/deserializer.rb', line 111

def detect_conflict(key, path_value, body_value)
  raise Conflict.new(key, path_value, body_value) if path_value && body_value && body_value != path_value
end

#filter_keys(map) ⇒ Object



103
104
105
# File 'lib/graphiti/active_graph/deserializer.rb', line 103

def filter_keys(map)
  map.map { |key, v| [yield(key), v] }.select(&:first).to_h
end

#filter_keys_presence(map) ⇒ Object



99
100
101
# File 'lib/graphiti/active_graph/deserializer.rb', line 99

def filter_keys_presence(map)
  filter_keys(map) { |key| presence(key) || presence(key.to_s.pluralize.to_sym) }
end

#meta(action: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/graphiti/active_graph/deserializer.rb', line 77

def meta(action: nil)
  results = super
  return results if action.present? || @env.nil?

  action = case @env['REQUEST_METHOD']
           when 'POST' then :create
           when 'PUT', 'PATCH' then :update
           when 'DELETE' then :destroy
           end

  results[:method] = action
  results
end

#meta_paramsObject



44
45
46
# File 'lib/graphiti/active_graph/deserializer.rb', line 44

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

#path_mapObject



91
92
93
94
95
96
97
# File 'lib/graphiti/active_graph/deserializer.rb', line 91

def path_map
  map = @params.select { |key, _| key =~ /_id$/ }.permit!.to_h
  map = filter_keys(map) { |key| key.gsub(/_id$/, '').to_sym }
  map = filter_keys(map) { |key| @parent_map[key] || key }
  map = filter_keys_presence(map) if @model < ActiveGraph::Node
  map
end

#presence(key) ⇒ Object



107
108
109
# File 'lib/graphiti/active_graph/deserializer.rb', line 107

def presence(key)
  key if @model.associations.include?(key)
end

#process_nil_relationship(name) ⇒ Object

change empty relationship as ‘disassociate` hash so they will be removed



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/graphiti/active_graph/deserializer.rb', line 63

def process_nil_relationship(name)
  attributes = {}
  method_name = :disassociate

  {
    meta: {
      jsonapi_type: name.to_sym,
      method: method_name
    },
    attributes: attributes,
    relationships: {}
  }
end

#process_relationship_datum(datum) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/graphiti/active_graph/deserializer.rb', line 32

def process_relationship_datum(datum)
  {
    meta: {
      jsonapi_type: datum[:type],
      temp_id: datum[:'temp-id'],
      method: datum[:method]&.to_sym
    },
    attributes: datum[:id] ? { id: datum[:id] } : {},
    relationships: {}
  }
end

#process_relationships(relationship_hash) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/graphiti/active_graph/deserializer.rb', line 48

def process_relationships(relationship_hash)
  {}.tap do |hash|
    relationship_hash.each_pair do |name, relationship_payload|
      name = name.to_sym
      data_payload = relationship_payload[:data]
      hash[name] = data_payload.nil? ? process_nil_relationship(name) : process_relationship(relationship_payload[:data])
    end
  end
end

#relationship?(name) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/graphiti/active_graph/deserializer.rb', line 58

def relationship?(name)
  relationships[name.to_sym].present?
end