Module: Graphiti::ActiveGraph::Deserializer
- Defined in:
- lib/graphiti/active_graph/deserializer.rb
Defined Under Namespace
Classes: Conflict
Instance Method Summary collapse
- #add_path_id_to_relationships!(params) ⇒ Object
- #detect_conflict(key, path_value, body_value) ⇒ Object
- #filter_keys(map) ⇒ Object
- #filter_keys_presence(map) ⇒ Object
- #initialize(payload, env = nil, model = nil, parent_map = nil) ⇒ Object
- #meta(action: nil) ⇒ Object
- #path_map ⇒ Object
- #path_relationships_updated! ⇒ Object
- #path_relationships_updated? ⇒ Boolean
- #presence(key) ⇒ Object
-
#process_nil_relationship(name) ⇒ Object
change empty relationship as ‘disassociate` hash so they will be removed.
- #process_relationships(relationship_hash) ⇒ Object
- #update_params(params, rel_name, path_value) ⇒ Object
- #update_realationships(rel_name, path_value) ⇒ Object
Instance Method Details
#add_path_id_to_relationships!(params) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 65 def add_path_id_to_relationships!(params) return params if path_relationships_updated? detect_conflict(:id, @params[:id]&.to_s, attributes[:id]&.to_s) path_map.each do |rel_name, path_value| body_value = relationships.dig(rel_name, :attributes, :id) if body_value detect_conflict(rel_name, path_value&.to_s, body_value&.to_s) else update_params(params, rel_name, path_value) update_realationships(rel_name, path_value) end end path_relationships_updated! params end |
#detect_conflict(key, path_value, body_value) ⇒ Object
124 125 126 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 124 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
116 117 118 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 116 def filter_keys(map) map.map { |key, v| [yield(key), v] }.select(&:first).to_h end |
#filter_keys_presence(map) ⇒ Object
112 113 114 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 112 def filter_keys_presence(map) filter_keys(map) { |key| presence(key) || presence(key.to_s.pluralize.to_sym) } end |
#initialize(payload, env = nil, model = nil, parent_map = nil) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 17 def initialize(payload, env=nil, model=nil, parent_map=nil) super(payload) @params = payload @model = model @parent_map = parent_map || {} @env = env end |
#meta(action: nil) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 51 def (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 |
#path_map ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 104 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 |
#path_relationships_updated! ⇒ Object
81 82 83 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 81 def path_relationships_updated! @path_relationships_updated = true end |
#path_relationships_updated? ⇒ Boolean
85 86 87 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 85 def path_relationships_updated? @path_relationships_updated.present? end |
#presence(key) ⇒ Object
120 121 122 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 120 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
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 37 def process_nil_relationship(name) attributes = {} method_name = :disassociate { meta: { jsonapi_type: name.to_sym, method: method_name }, attributes: attributes, relationships: {} } end |
#process_relationships(relationship_hash) ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 26 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 |
#update_params(params, rel_name, path_value) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 89 def update_params(params, rel_name, path_value) params[:data] ||= {} params[:data][:relationships] ||= {} params[:data][:relationships][rel_name] = { data: { type: derive_resource_type(rel_name), id: path_value } } end |
#update_realationships(rel_name, path_value) ⇒ Object
100 101 102 |
# File 'lib/graphiti/active_graph/deserializer.rb', line 100 def update_realationships(rel_name, path_value) relationships[rel_name] = { meta: {}, attributes: { id: path_value } } end |