isomorfeus-data 1.0.0.zeta12 → 1.0.0.zeta13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +6 -0
  3. data/lib/isomorfeus-data.rb +2 -0
  4. data/lib/isomorfeus/data/attribute_support.rb +2 -2
  5. data/lib/isomorfeus/data/config.rb +1 -1
  6. data/lib/isomorfeus/data/element_validator.rb +16 -16
  7. data/lib/isomorfeus/data/generic_class_api.rb +34 -52
  8. data/lib/isomorfeus/data/generic_instance_api.rb +35 -8
  9. data/lib/isomorfeus/data/handler/arango.rb +2 -3
  10. data/lib/isomorfeus/data/handler/generic.rb +44 -55
  11. data/lib/isomorfeus/data/handler/object_call.rb +2 -3
  12. data/lib/isomorfeus/data/handler/object_store.rb +2 -3
  13. data/lib/isomorfeus/data/version.rb +1 -1
  14. data/lib/isomorfeus_data/lucid_data/array/base.rb +0 -2
  15. data/lib/isomorfeus_data/lucid_data/array/mixin.rb +18 -22
  16. data/lib/isomorfeus_data/lucid_data/collection/base.rb +0 -2
  17. data/lib/isomorfeus_data/lucid_data/collection/mixin.rb +33 -36
  18. data/lib/isomorfeus_data/lucid_data/composition/base.rb +0 -2
  19. data/lib/isomorfeus_data/lucid_data/composition/mixin.rb +35 -29
  20. data/lib/isomorfeus_data/lucid_data/document/base.rb +0 -2
  21. data/lib/isomorfeus_data/lucid_data/edge/base.rb +0 -2
  22. data/lib/isomorfeus_data/lucid_data/edge/mixin.rb +39 -31
  23. data/lib/isomorfeus_data/lucid_data/edge_collection/base.rb +0 -2
  24. data/lib/isomorfeus_data/lucid_data/edge_collection/mixin.rb +33 -29
  25. data/lib/isomorfeus_data/lucid_data/graph/base.rb +0 -2
  26. data/lib/isomorfeus_data/lucid_data/graph/mixin.rb +41 -40
  27. data/lib/isomorfeus_data/lucid_data/hash/base.rb +0 -2
  28. data/lib/isomorfeus_data/lucid_data/hash/mixin.rb +20 -25
  29. data/lib/isomorfeus_data/lucid_data/link/base.rb +0 -2
  30. data/lib/isomorfeus_data/lucid_data/link_collection/base.rb +0 -2
  31. data/lib/isomorfeus_data/lucid_data/node/base.rb +0 -2
  32. data/lib/isomorfeus_data/lucid_data/node/mixin.rb +18 -23
  33. data/lib/isomorfeus_data/lucid_data/query/base.rb +13 -0
  34. data/lib/isomorfeus_data/lucid_data/query/mixin.rb +88 -0
  35. data/lib/isomorfeus_data/lucid_data/query_result.rb +12 -10
  36. data/lib/isomorfeus_data/lucid_data/vertex/base.rb +0 -2
  37. data/lib/lucid_arango/collection/base.rb +0 -2
  38. data/lib/lucid_arango/collection/mixin.rb +0 -2
  39. data/lib/lucid_arango/document/base.rb +0 -2
  40. data/lib/lucid_arango/edge/base.rb +0 -3
  41. data/lib/lucid_arango/edge/mixin.rb +0 -2
  42. data/lib/lucid_arango/edge_collection/base.rb +0 -2
  43. data/lib/lucid_arango/edge_collection/mixin.rb +0 -2
  44. data/lib/lucid_arango/graph/base.rb +0 -2
  45. data/lib/lucid_arango/graph/mixin.rb +0 -2
  46. data/lib/lucid_arango/node/base.rb +0 -3
  47. data/lib/lucid_arango/node/mixin.rb +0 -2
  48. data/lib/lucid_arango/object/base.rb +0 -2
  49. data/lib/lucid_arango/object/mixin.rb +1 -3
  50. data/lib/lucid_arango/remote_object/base.rb +0 -2
  51. data/lib/lucid_arango/remote_object/mixin.rb +0 -2
  52. data/lib/lucid_arango/vertex/base.rb +0 -2
  53. metadata +10 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e5ee58b1f8f38aef39dbcdf8a8d19449a6dc2935355568f5e09bd2cc8e667be
4
- data.tar.gz: 3eaa2d3558298f3b82e0dec47076a0662353e8fb679f6fd56979f6db91ff24c2
3
+ metadata.gz: 5cee3d71649477f132cee03fd570b5fc5c4ef9430ccad0d40daab90e05525eff
4
+ data.tar.gz: 6839dfeb7ceebc8f7311073c31d69f11d73265ab0689d798d2c60754f4e65b72
5
5
  SHA512:
6
- metadata.gz: bc4f6bcce18fcaf860603bdc6d1e454883a681d8db6b1a543ec36de56b797a6c658ab42c607e06ae67643b4b6544abc226488de56cdd1f42ac6ee6b68ea0af4a
7
- data.tar.gz: d295afbea8c6223cd4c38a62f3f19630a869e02165eb6e1ee26dee38b3535609db3e938b4d5d036bad7eded951e47a9d59c9c57175ee37803d546f5793115231
6
+ metadata.gz: 189425d916a46bda087d17a9d49afe58bf6334446605a0d631032aff08b44f0b651acad2dea727ca6499e170d602bfe6cc1dccd31eaf5172ad3b9e737bf6399f
7
+ data.tar.gz: bb087ac1acdf8887b28bea4b5c997c2fac9263c500c7e88d18a3ef66115bde41a93bdf2955ac1546bc85e947405720f4e22d36d19abf26dd9388c3956e518dbf
data/README.md CHANGED
@@ -36,4 +36,10 @@ All classes follow the common principles and the common API above.
36
36
  - [LucidData::Graph](https://github.com/isomorfeus/isomorfeus-project/blob/master/ruby/isomorfeus-data/docs/data_graph.md) - A graph, which can be build from several LucidData::Collection objects and LucidData::EdgeCollection objects
37
37
  - [LucidData::Hash](https://github.com/isomorfeus/isomorfeus-project/blob/master/ruby/isomorfeus-data/docs/data_hash.md) - A hash, easily accessible on client and server
38
38
 
39
+ For convenience the following generic classes are provided. They can be used when creating a separate class wouldn't be useful:
40
+ - LucidData::GenericCollection
41
+ - LucidData::GenericEdgeCollection
42
+ - LucidData::GenericNode
43
+ - LucidData::GenericEdge
44
+
39
45
  (more to come soon)
@@ -44,6 +44,8 @@ else
44
44
  require 'isomorfeus_data/lucid_data/graph/base'
45
45
  require 'isomorfeus_data/lucid_data/composition/mixin'
46
46
  require 'isomorfeus_data/lucid_data/composition/base'
47
+ require 'isomorfeus_data/lucid_data/query/mixin'
48
+ require 'isomorfeus_data/lucid_data/query/base'
47
49
 
48
50
  require 'isomorfeus_data/lucid_data/generic_collection'
49
51
  require 'isomorfeus_data/lucid_data/generic_edge'
@@ -18,14 +18,14 @@ module Isomorfeus
18
18
  end
19
19
 
20
20
  def _validate_attribute(attr_name, attr_val)
21
- raise "#{self.name}: No such attribute declared: '#{attr_name}'!" unless attribute_conditions.key?(attr_name)
21
+ Isomorfeus.raise_error(message: "#{self.name}: No such attribute declared: '#{attr_name}'!") unless attribute_conditions.key?(attr_name)
22
22
  Isomorfeus::Props::Validator.new(self.name, attr_name, attr_val, attribute_conditions[attr_name]).validate!
23
23
  end
24
24
 
25
25
  def _validate_attributes(attrs)
26
26
  attribute_conditions.each_key do |attr|
27
27
  if attribute_conditions[attr].key?(:required) && attribute_conditions[attr][:required] && !attrs.key?(attr)
28
- raise "Required attribute #{attr} not given!"
28
+ Isomorfeus.raise_error(message: "Required attribute #{attr} not given!")
29
29
  end
30
30
  end
31
31
  attrs.each { |attr, val| _validate_attribute(attr, val) }
@@ -50,7 +50,7 @@ module Isomorfeus
50
50
  begin
51
51
  Arango.current_server.get_database(database)
52
52
  rescue Exception => e
53
- raise "Can't connect to database '#{database}' (#{e.message})."
53
+ Isomorfeus.raise_error(message: "Can't connect to database '#{database}' (#{e.message}).")
54
54
  end
55
55
  end
56
56
 
@@ -33,7 +33,7 @@ module Isomorfeus
33
33
  end
34
34
  @e = !!@e if @o[:type] == :boolean
35
35
  rescue
36
- raise "#{@c}: #{@p} cast failed" unless @e.class == @o[:class]
36
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} cast failed") unless @e.class == @o[:class]
37
37
  end
38
38
  end
39
39
  end
@@ -53,13 +53,13 @@ module Isomorfeus
53
53
  def type!
54
54
  return if @o[:allow_nil] && @e.nil?
55
55
  if @o.key?(:class)
56
- raise "#{@c}: #{@p} class not #{@o[:class]}" unless @e.class == @o[:class]
56
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} class not #{@o[:class]}") unless @e.class == @o[:class]
57
57
  elsif @o.key?(:is_a)
58
- raise "#{@c}: #{@p} is not a #{@o[:is_a]}" unless @e.is_a?(@o[:is_a])
58
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a #{@o[:is_a]}") unless @e.is_a?(@o[:is_a])
59
59
  elsif @o.key?(:type)
60
60
  case @o[:type]
61
61
  when :boolean
62
- raise "#{@c}: #{@p} is not a boolean" unless @e.class == TrueClass || @e.class == FalseClass
62
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a boolean") unless @e.class == TrueClass || @e.class == FalseClass
63
63
  end
64
64
  end
65
65
  end
@@ -76,48 +76,48 @@ module Isomorfeus
76
76
 
77
77
  # specific validations
78
78
  def c_gt(v)
79
- raise "#{@c}: #{@p} not greater than #{v}!" unless @e > v
79
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} not greater than #{v}!") unless @e > v
80
80
  end
81
81
 
82
82
  def c_lt(v)
83
- raise "#{@c}: #{@p} not less than #{v}!" unless @e < v
83
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} not less than #{v}!") unless @e < v
84
84
  end
85
85
 
86
86
  def c_keys(v)
87
- raise "#{@c}: #{@p} keys dont fit!" unless @e.keys.sort == v.sort
87
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} keys dont fit!") unless @e.keys.sort == v.sort
88
88
  end
89
89
 
90
90
  def c_size(v)
91
- raise "#{@c}: #{@p} length/size is not #{v}" unless @e.size == v
91
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} length/size is not #{v}") unless @e.size == v
92
92
  end
93
93
 
94
94
  def c_matches(v)
95
- raise "#{@c}: #{@p} does not match #{v}" unless v.match?(@e)
95
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} does not match #{v}") unless v.match?(@e)
96
96
  end
97
97
 
98
98
  def c_max(v)
99
- raise "#{@c}: #{@p} is larger than #{v}" unless @e <= v
99
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is larger than #{v}") unless @e <= v
100
100
  end
101
101
 
102
102
  def c_min(v)
103
- raise "#{@c}: #{@p} is smaller than #{v}" unless @e >= v
103
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is smaller than #{v}") unless @e >= v
104
104
  end
105
105
 
106
106
  def c_max_size(v)
107
- raise "#{@c}: #{@p} is larger than #{v}" unless @e.size <= v
107
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is larger than #{v}") unless @e.size <= v
108
108
  end
109
109
 
110
110
  def c_min_size(v)
111
- raise "#{@c}: #{@p} is smaller than #{v}" unless @e.size >= v
111
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is smaller than #{v}") unless @e.size >= v
112
112
  end
113
113
 
114
114
  def c_direction(v)
115
- raise "#{@c}: #{@p} is positive" if v == :negative && @e >= 0
116
- raise "#{@c}: #{@p} is negative" if v == :positive && @e < 0
115
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is positive") if v == :negative && @e >= 0
116
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is negative") if v == :positive && @e < 0
117
117
  end
118
118
 
119
119
  def c_test
120
- raise "#{@c}: #{@p} test condition check failed" unless @o[:test].call(@e)
120
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} test condition check failed") unless @o[:test].call(@e)
121
121
  end
122
122
 
123
123
  def c_sub_type(v)
@@ -25,9 +25,9 @@ module Isomorfeus
25
25
  agent.processed = true
26
26
  if agent.response.key?(:error)
27
27
  `console.error(#{agent.response[:error].to_n})`
28
- raise agent.response[:error]
28
+ Isomorfeus.raise_error(message: agent.response[:error])
29
29
  end
30
- Isomorfeus.store.dispatch(type: 'DATA_DESTROY', data: agent.full_response[:data])
30
+ Isomorfeus.store.dispatch(type: 'DATA_DESTROY', data: [self.name, key])
31
31
  agent.result = true
32
32
  end
33
33
  end
@@ -48,7 +48,7 @@ module Isomorfeus
48
48
  agent.processed = true
49
49
  if agent.response.key?(:error)
50
50
  `console.error(#{agent.response[:error].to_n})`
51
- raise agent.response[:error]
51
+ Isomorfeus.raise_error(message: agent.response[:error])
52
52
  end
53
53
  instance._load_from_store!
54
54
  Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: agent.full_response[:data])
@@ -57,40 +57,18 @@ module Isomorfeus
57
57
  end
58
58
  end
59
59
 
60
- def query(props:)
61
- query_result_instance = LucidData::QueryResult.new
62
- promise_query(props: props, query_result_instance: query_result_instance) unless query_result_instance.loaded?
63
- query_result_instance
60
+ def save(instance:)
61
+ instance.promise_save
62
+ instance
64
63
  end
65
64
 
66
- def promise_query(props:, query_result_instance: nil)
67
- query_result_instance = LucidData::QueryResult.new unless query_result_instance
68
- props.each_key do |prop_name|
69
- raise "#{self.to_s} No such query prop declared: '#{prop_name}'!" unless declared_props.key?(prop_name)
70
- end
71
- validate_props(props)
72
- data_props = { props: props, query_result_instance_key: query_result_instance.key }
73
- props_json = data_props.to_json
74
- Isomorfeus::Transport.promise_send_path( 'Isomorfeus::Data::Handler::Generic', self.name, :query, props_json).then do |agent|
75
- if agent.processed
76
- agent.result
77
- else
78
- agent.processed = true
79
- if agent.response.key?(:error)
80
- `console.error(#{agent.response[:error].to_n})`
81
- raise agent.response[:error]
82
- end
83
- query_result_instance._load_from_store!
84
- Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: agent.full_response[:data])
85
- agent.result = query_result_instance
86
- end
87
- end
65
+ def promise_save(instance:)
66
+ instance.promise_save
88
67
  end
89
68
 
90
69
  # execute
91
70
  def execute_destroy(_); end
92
71
  def execute_load(_); end
93
- def execute_query(_); end
94
72
  def execute_save(_); end
95
73
  else
96
74
  def promise_create(key:, **things)
@@ -101,14 +79,14 @@ module Isomorfeus
101
79
  end
102
80
 
103
81
  def promise_destroy(key:)
104
- sid = self.destroy(key: key)
82
+ result = self.destroy(key: key)
105
83
  result_promise = Promise.new
106
- result_promise.resolve(sid)
84
+ result_promise.resolve(result)
107
85
  result_promise
108
86
  end
109
87
 
110
- def destroy(key:, pub_sub_client: nil, current_user: nil)
111
- instance_exec(key: key, pub_sub_client: pub_sub_client, current_user: current_user, &@_destroy_block)
88
+ def destroy(key:)
89
+ !!instance_exec(key: key, &@_destroy_block)
112
90
  end
113
91
 
114
92
  def promise_load(key:)
@@ -118,22 +96,22 @@ module Isomorfeus
118
96
  result_promise
119
97
  end
120
98
 
121
- def promise_query(props:)
122
- instance = self.query(props: props)
123
- result_promise = Promise.new
124
- result_promise.resolve(instance)
125
- result_promise
99
+ def load(key:)
100
+ data = instance_exec(key: key, &@_load_block)
101
+ return nil unless data
102
+ return data if data.class == self
103
+ Isomorfeus.raise_error "#{self.to_s}: execute_load must return a instance of #{self.to_s} or nil. Returned was: #{data.class}." if data.class != self
104
+ data
126
105
  end
127
106
 
128
- def query(props:, query_result_instance_key: nil, pub_sub_client: nil, current_user: nil)
129
- props.each_key do |prop_name|
130
- raise "#{self.to_s} No such query prop declared: '#{prop_name}'!" unless declared_props.key?(prop_name)
131
- end
132
- validate_props(props)
133
- query_result = LucidData::QueryResult.new(key: query_result_instance_key)
134
- query_result.result_set = instance_exec(props: Isomorfeus::Transport::PropsProxy.new(props),
135
- pub_sub_client: pub_sub_client, current_user: current_user, &@_query_block)
136
- query_result
107
+ def save(instance:)
108
+ previous_key = instance.key
109
+ data = instance_exec(instance: instance, &@_save_block)
110
+ return nil unless data
111
+ Isomorfeus.raise_error "#{self.to_s}: execute_save must return a instance of #{self.to_s} or nil. Returned was: #{data.class}." if data.class != self
112
+ data.instance_variable_set(:@previous_key, previous_key) if data.key != previous_key
113
+ data._unchange!
114
+ data
137
115
  end
138
116
 
139
117
  # execute
@@ -145,14 +123,18 @@ module Isomorfeus
145
123
  @_load_block = block
146
124
  end
147
125
 
148
- def execute_query(&block)
149
- @_query_block = block
150
- end
151
-
152
126
  def execute_save(&block)
153
127
  @_save_block = block
154
128
  end
155
129
  end
130
+
131
+ def current_user
132
+ Isomorfeus.current_user
133
+ end
134
+
135
+ def pub_sub_client
136
+ Isomorfeus.pub_sub_client
137
+ end
156
138
  end
157
139
  end
158
140
  end
@@ -47,30 +47,57 @@ module Isomorfeus
47
47
  alias create save
48
48
 
49
49
  def promise_save
50
- data_hash = to_transport
51
- if self.respond_to?(:included_changed_items)
52
- data_hash.deep_merge!(self.included_changed_items)
53
- end
54
- Isomorfeus::Transport.promise_send_path( 'Isomorfeus::Data::Handler::Generic', self.name, 'save', data_hash).then do |agent|
50
+ data_hash = { instance: to_transport }
51
+ data_hash.deep_merge!(included_items: included_items_to_transport) if respond_to?(:included_items_to_transport)
52
+ class_name = self.class.name
53
+ Isomorfeus::Transport.promise_send_path( 'Isomorfeus::Data::Handler::Generic', class_name, :save, data_hash).then do |agent|
55
54
  if agent.processed
56
55
  agent.result
57
56
  else
58
57
  agent.processed = true
59
58
  if agent.response.key?(:error)
60
59
  `console.error(#{agent.response[:error].to_n})`
61
- raise agent.response[:error]
60
+ Isomorfeus.raise_error(message: agent.response[:error])
61
+ end
62
+ data = agent.full_response[:data]
63
+ if data.key?(class_name) && data[class_name].key?(@key) && data[class_name][@key].key?('new_key')
64
+ @key = data[class_name][@key]['new_key']
65
+ @revision = data[class_name][@key]['revision'] if data[class_name][@key].key?('revision')
66
+ _update_paths
62
67
  end
63
- Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: agent.full_response[:data])
64
- agent.result = true
68
+ _load_from_store!
69
+ Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: data)
70
+ agent.result = self
65
71
  end
66
72
  end
67
73
  end
68
74
  alias promise_create promise_save
75
+
76
+ # TODO update -> only send partial change
77
+ # included_changed_items
69
78
  else # RUBY_ENGINE
70
79
  def loaded?
71
80
  true
72
81
  end
82
+
83
+ def save
84
+ self.class.save(instance: self)
85
+ self
86
+ end
87
+
88
+ def promise_save
89
+ promise = Promise.new
90
+ promise.resolve(save)
91
+ end
73
92
  end # RUBY_ENGINE
93
+
94
+ def current_user
95
+ Isomorfeus.current_user
96
+ end
97
+
98
+ def pub_sub_client
99
+ Isomorfeus.pub_sub_client
100
+ end
74
101
  end
75
102
  end
76
103
  end
@@ -4,7 +4,7 @@ module Isomorfeus
4
4
  module Data
5
5
  module Handler
6
6
  class Arango < LucidHandler::Base
7
- on_request do |pub_sub_client, current_user, response_agent|
7
+ on_request do |response_agent|
8
8
  # promise_send_path('Isomorfeus::Data::Handler::Generic', action, type, self.to_s, props_hash)
9
9
  response_agent.request.each_key do |action|
10
10
  if action == 'load'
@@ -17,11 +17,10 @@ module Isomorfeus
17
17
  props_json = response_agent.request[type_class_name]
18
18
  begin
19
19
  props = Oj.load(props_json, mode: :strict)
20
- props.merge!({pub_sub_client: pub_sub_client, current_user: current_user})
21
20
  if current_user.authorized?(type_class, :load, *props)
22
21
  loaded_type = type_class.load(props)
23
22
  loaded_type.instance_exec do
24
- type_class.on_load_block.call(pub_sub_client, current_user) if type_class.on_load_block
23
+ type_class.on_load_block.call() if type_class.on_load_block
25
24
  end
26
25
  response_agent.outer_result = { data: loaded_type.to_transport }
27
26
  if %w[collection graph].include?(type)
@@ -14,7 +14,7 @@ module Isomorfeus
14
14
  # LucidData::Graph
15
15
  # LucidData::Composition
16
16
 
17
- def process_request(pub_sub_client, current_user, response_agent)
17
+ def process_request(response_agent)
18
18
  # promise_send_path('Isomorfeus::Data::Handler::Generic', self.to_s, action, props_hash)
19
19
  response_agent.request.each_key do |type_class_name|
20
20
  if Isomorfeus.valid_data_class_name?(type_class_name)
@@ -22,103 +22,92 @@ module Isomorfeus
22
22
  if type_class
23
23
  response_agent.request[type_class_name].each_key do |action|
24
24
  case action
25
- when 'load' then process_load(pub_sub_client, current_user, response_agent, type_class, type_class_name)
26
- when 'query' then process_query(pub_sub_client, current_user, response_agent, type_class, type_class_name)
27
- when 'save' then process_save(pub_sub_client, current_user, response_agent, type_class, type_class_name)
28
- when 'destroy' then process_destroy(pub_sub_client, current_user, response_agent, type_class, type_class_name)
25
+ when 'load' then process_load(response_agent, type_class, type_class_name)
26
+ when 'execute' then process_execute(response_agent, type_class, type_class_name)
27
+ when 'save' then process_save(response_agent, type_class, type_class_name)
28
+ when 'destroy' then process_destroy(response_agent, type_class, type_class_name)
29
29
  else response_agent.error = { error: { action => 'No such thing!' }}
30
30
  end
31
31
  end
32
- else response_agent.error = { error: { type_class_name => 'No such thing!' }}
32
+ else response_agent.error = { error: { type_class_name => 'No such class!' }}
33
33
  end
34
- else response_agent.error = { error: { type_class_name => 'No such thing!' }}
34
+ else response_agent.error = { error: { type_class_name => 'Not a valid LucidData class!' }}
35
35
  end
36
36
  end
37
37
  rescue Exception => e
38
38
  response_agent.error = { error: "Isomorfeus::Data::Handler::Generic: #{e.message}\n#{e.backtrace.join("\n")}" }
39
39
  end
40
40
 
41
- def process_load(pub_sub_client, current_user, response_agent, type_class, type_class_name)
41
+ def process_load(response_agent, type_class, type_class_name)
42
+ # 'Isomorfeus::Data::Handler::Generic', self.name, :load, key: key
42
43
  props = response_agent.request[type_class_name]['load']
43
44
  props.transform_keys!(&:to_sym)
44
- props.merge!(pub_sub_client: pub_sub_client, current_user: current_user)
45
- if current_user.authorized?(type_class, :load, props)
45
+ if Isomorfeus.current_user.authorized?(type_class, :load, props)
46
46
  loaded_type = type_class.load(**props)
47
47
  if loaded_type
48
- response_agent.outer_result = { data: loaded_type.to_transport }
48
+ response_agent.outer_result = {} unless response_agent.outer_result
49
+ response_agent.outer_result.deep_merge!(data: loaded_type.to_transport)
49
50
  if loaded_type.respond_to?(:included_items_to_transport)
50
51
  response_agent.outer_result.deep_merge!(data: loaded_type.included_items_to_transport)
51
52
  end
52
53
  response_agent.agent_result = { success: 'ok' }
53
- else response_agent.error = { error: { type_class_name => 'No such thing!' }}
54
+ else response_agent.error = { error: { type_class_name => 'Load returned nothing!' }}
54
55
  end
55
56
  else response_agent.error = { error: 'Access denied!' }
56
57
  end
57
58
  end
58
59
 
59
- def process_query(pub_sub_client, current_user, response_agent, type_class, type_class_name)
60
- props_json = response_agent.request[type_class_name]['query']
60
+ def process_execute(response_agent, type_class, type_class_name)
61
+ # 'Isomorfeus::Data::Handler::Generic', self.name, :execute, props_json
62
+ props_json = response_agent.request[type_class_name]['execute']
61
63
  props = Oj.load(props_json, mode: :strict)
62
64
  props.transform_keys!(&:to_sym)
63
65
  props[:props].transform_keys!(&:to_sym)
64
- props.deep_merge!({ pub_sub_client: pub_sub_client, current_user: current_user })
65
- if current_user.authorized?(type_class, :query, props[:props])
66
- queried_type = type_class.query(**props)
66
+ if Isomorfeus.current_user.authorized?(type_class, :execute, props[:props])
67
+ queried_type = type_class.execute(**props)
67
68
  if queried_type
68
- response_agent.outer_result = { data: queried_type.to_transport }
69
+ response_agent.outer_result = {} unless response_agent.outer_result
70
+ response_agent.outer_result.deep_merge!(data: queried_type.to_transport)
69
71
  if queried_type.respond_to?(:included_items_to_transport)
70
72
  response_agent.outer_result.deep_merge!(data: queried_type.included_items_to_transport)
71
73
  end
72
74
  response_agent.agent_result = { success: 'ok' }
73
- else response_agent.error = { error: { type_class_name => 'No such thing!' }}
75
+ else response_agent.error = { error: { type_class_name => 'Query returned nothing!' }}
74
76
  end
75
77
  else response_agent.error = { error: 'Access denied!' }
76
78
  end
77
79
  end
78
80
 
79
- def process_save(pub_sub_client, current_user, response_agent, type_class, type_class_name)
80
- data_hash_json = response_agent.request[type_class_name]['save']
81
- data_hash = Oj.load(data_hash_json, mode: :strict)
82
- response_agent.outer_result = {}
83
- if current_user.authorized?(type_class, :save, { pub_sub_client: pub_sub_client, current_user: current_user })
84
- data_hash.each_key do |data_class_name|
85
- data_hash[data_class_name].each_key do |key|
86
- if Isomorfeus.valid_data_class_name?(type_class_name)
87
- data_class = Isomorfeus.cached_data_class(data_class_name)
88
- if data_class
89
- item_data_hash = data_hash[data_class_name][key]
90
- item_data_hash = item_data_hash.merge({ pub_sub_client: pub_sub_client, current_user: current_user })
91
- if current_user.authorized?(data_class, :save, item_data_hash)
92
- saved_type = data_class.save(**item_data_hash)
93
- if saved_type
94
- response_agent.outer_result.deep_merge!({ data: saved_type.to_transport })
95
- end
96
- else
97
- response_agent.error = { error: 'Access denied!' }
98
- break
99
- end
100
- else
101
- response_agent.error = { error: 'Access denied!' }
102
- break
103
- end
104
- end
81
+ def process_save(response_agent, type_class, type_class_name)
82
+ # 'Isomorfeus::Data::Handler::Generic', self.name, :save, data_hash
83
+ data = response_agent.request[type_class_name]['save']
84
+ instance_data = data['instance']
85
+ included_items_data = data.key?('included_items') ? data['included_items'] : nil
86
+ if Isomorfeus.current_user.authorized?(type_class, :save, data)
87
+ instance = type_class.instance_from_transport(instance_data, included_items_data)
88
+ saved_type = type_class.save(instance: instance)
89
+ if saved_type
90
+ response_agent.outer_result = {} unless response_agent.outer_result
91
+ response_agent.outer_result.deep_merge!(data: saved_type.to_transport)
92
+ if saved_type.respond_to?(:included_items_to_transport)
93
+ response_agent.outer_result.deep_merge!(data: saved_type.included_items_to_transport)
105
94
  end
95
+ response_agent.agent_result = { success: 'ok' }
96
+ else response_agent.error = { error: { type_class_name => 'Save returned nothing!' }}
106
97
  end
107
- response_agent.agent_result = { success: 'ok' }
108
98
  else response_agent.error = { error: 'Access denied!' }
109
99
  end
110
100
  end
111
101
 
112
- def process_destroy(pub_sub_client, current_user, response_agent, type_class, type_class_name)
113
- props_json = response_agent.request[type_class_name]['destroy']
114
- props = Oj.load(props_json, mode: :strict)
115
- props.merge!(pub_sub_client: pub_sub_client, current_user: current_user)
116
- if current_user.authorized?(type_class, :destroy, props)
117
- destroyed_type = type_class.destroy(**props)
118
- if destroyed_type
119
- response_agent.outer_result = { data: destroyed_type }
102
+ def process_destroy(response_agent, type_class, type_class_name)
103
+ props = response_agent.request[type_class_name]['destroy']
104
+ props.transform_keys!(&:to_sym)
105
+ if Isomorfeus.current_user.authorized?(type_class, :destroy, props)
106
+ result = type_class.destroy(**props)
107
+ if result
120
108
  response_agent.agent_result = { success: 'ok' }
121
- else response_agent.error = { error: { type_class_name => 'No such thing!' }}
109
+ else
110
+ response_agent.error = { error: { type_class_name => 'Destroy failed!' }}
122
111
  end
123
112
  else response_agent.error = { error: 'Access denied!' }
124
113
  end