isomorfeus-data 1.0.0.zeta5 → 1.0.0.zeta6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +37 -1
  3. data/lib/isomorfeus/data/attribute_support.rb +122 -0
  4. data/lib/isomorfeus/data/config.rb +79 -89
  5. data/lib/isomorfeus/data/element_validator.rb +133 -0
  6. data/lib/isomorfeus/data/generic_class_api.rb +155 -0
  7. data/lib/isomorfeus/data/generic_instance_api.rb +72 -0
  8. data/lib/isomorfeus/data/handler/arango.rb +61 -0
  9. data/lib/isomorfeus/data/handler/generic.rb +92 -0
  10. data/lib/isomorfeus/data/handler/object_call.rb +45 -0
  11. data/lib/isomorfeus/data/handler/object_store.rb +45 -0
  12. data/lib/isomorfeus/data/version.rb +1 -1
  13. data/lib/isomorfeus-data.rb +61 -30
  14. data/lib/isomorfeus_data/lucid_data/array/base.rb +15 -0
  15. data/lib/isomorfeus_data/lucid_data/array/mixin.rb +570 -0
  16. data/lib/isomorfeus_data/lucid_data/collection/base.rb +15 -0
  17. data/lib/isomorfeus_data/lucid_data/collection/finders.rb +83 -0
  18. data/lib/isomorfeus_data/lucid_data/collection/mixin.rb +710 -0
  19. data/lib/isomorfeus_data/lucid_data/composition/base.rb +15 -0
  20. data/lib/isomorfeus_data/lucid_data/composition/mixin.rb +185 -0
  21. data/lib/isomorfeus_data/lucid_data/document/base.rb +15 -0
  22. data/lib/isomorfeus_data/lucid_data/document/mixin.rb +9 -0
  23. data/lib/isomorfeus_data/lucid_data/edge/base.rb +15 -0
  24. data/lib/isomorfeus_data/lucid_data/edge/mixin.rb +281 -0
  25. data/lib/isomorfeus_data/lucid_data/edge_collection/base.rb +15 -0
  26. data/lib/isomorfeus_data/lucid_data/edge_collection/finders.rb +134 -0
  27. data/lib/isomorfeus_data/lucid_data/edge_collection/mixin.rb +705 -0
  28. data/lib/isomorfeus_data/lucid_data/graph/base.rb +15 -0
  29. data/lib/isomorfeus_data/lucid_data/graph/finders.rb +141 -0
  30. data/lib/isomorfeus_data/lucid_data/graph/mixin.rb +401 -0
  31. data/lib/isomorfeus_data/lucid_data/hash/base.rb +15 -0
  32. data/lib/isomorfeus_data/lucid_data/hash/mixin.rb +413 -0
  33. data/lib/isomorfeus_data/lucid_data/link/base.rb +15 -0
  34. data/lib/isomorfeus_data/lucid_data/link/mixin.rb +9 -0
  35. data/lib/isomorfeus_data/lucid_data/link_collection/base.rb +15 -0
  36. data/lib/isomorfeus_data/lucid_data/link_collection/mixin.rb +9 -0
  37. data/lib/isomorfeus_data/lucid_data/node/base.rb +15 -0
  38. data/lib/isomorfeus_data/lucid_data/node/mixin.rb +192 -0
  39. data/lib/isomorfeus_data/lucid_data/object/base.rb +13 -0
  40. data/lib/{lucid_array → isomorfeus_data/lucid_data/object}/mixin.rb +15 -16
  41. data/lib/isomorfeus_data/lucid_data/remote_object/base.rb +13 -0
  42. data/lib/isomorfeus_data/lucid_data/remote_object/mixin.rb +19 -0
  43. data/lib/isomorfeus_data/lucid_data/vertex/base.rb +15 -0
  44. data/lib/isomorfeus_data/lucid_data/vertex/mixin.rb +9 -0
  45. data/lib/lucid_arango/collection/base.rb +15 -0
  46. data/lib/lucid_arango/collection/mixin.rb +20 -0
  47. data/lib/lucid_arango/document/base.rb +15 -0
  48. data/lib/lucid_arango/document/mixin.rb +9 -0
  49. data/lib/lucid_arango/edge/base.rb +16 -0
  50. data/lib/lucid_arango/edge/mixin.rb +20 -0
  51. data/lib/lucid_arango/edge_collection/base.rb +15 -0
  52. data/lib/lucid_arango/edge_collection/mixin.rb +20 -0
  53. data/lib/lucid_arango/graph/base.rb +15 -0
  54. data/lib/lucid_arango/graph/mixin.rb +20 -0
  55. data/lib/lucid_arango/node/base.rb +16 -0
  56. data/lib/lucid_arango/node/mixin.rb +81 -0
  57. data/lib/lucid_arango/vertex/base.rb +15 -0
  58. data/lib/lucid_arango/vertex/mixin.rb +9 -0
  59. metadata +84 -47
  60. data/lib/isomorfeus/data/handler/array_load_handler.rb +0 -47
  61. data/lib/isomorfeus/data/handler/collection_load_handler.rb +0 -48
  62. data/lib/isomorfeus/data/handler/graph_load_handler.rb +0 -48
  63. data/lib/isomorfeus/data/handler/hash_load_handler.rb +0 -47
  64. data/lib/lucid_array/base.rb +0 -14
  65. data/lib/lucid_collection/base.rb +0 -14
  66. data/lib/lucid_collection/mixin.rb +0 -241
  67. data/lib/lucid_edge/base.rb +0 -5
  68. data/lib/lucid_edge/mixin.rb +0 -238
  69. data/lib/lucid_graph/base.rb +0 -14
  70. data/lib/lucid_graph/mixin.rb +0 -670
  71. data/lib/lucid_hash/base.rb +0 -14
  72. data/lib/lucid_hash/mixin.rb +0 -168
  73. data/lib/lucid_node/base.rb +0 -5
  74. data/lib/lucid_node/mixin.rb +0 -198
@@ -1,241 +0,0 @@
1
- module LucidCollection
2
- module Mixin
3
- def self.included(base)
4
- if RUBY_ENGINE != 'opal'
5
- Isomorfeus.add_valid_collection_class(base) unless base == LucidCollection::Base
6
- end
7
-
8
- base.extend(LucidPropDeclaration::Mixin)
9
-
10
- def find_node(attribute_hash = nil, &block)
11
- if block_given?
12
- nodes.each do |node|
13
- return node if block.call(node)
14
- end
15
- else
16
- node_class = attribute_hash.delete(:class)
17
- is_a_module = attribute_hash.delete(:is_a)
18
- nodes.each do |node|
19
- if node_class
20
- next unless node.class == node_class
21
- end
22
- if is_a_module
23
- next unless node.is_a?(is_a_module)
24
- end
25
- found = true
26
- attribute_hash.each do |k,v|
27
- found &&= (node[k] == v)
28
- break unless found
29
- end
30
- return node if found
31
- end
32
- end
33
- nil
34
- end
35
-
36
- def find_nodes(attribute_hash = nil, &block)
37
- found_nodes = Set.new
38
- if block_given?
39
- nodes.each do |node|
40
- found_nodes << node if block.call(node)
41
- end
42
- else
43
- node_class = attribute_hash.delete(:class)
44
- is_a_module = attribute_hash.delete(:is_a)
45
- nodes.each do |node|
46
- if node_class
47
- next unless node.class == node_class
48
- end
49
- if is_a_module
50
- next unless node.is_a?(is_a_module)
51
- end
52
- found = true
53
- attribute_hash.each do |k,v|
54
- found &&= (node[k] == v)
55
- break unless found
56
- end
57
- found_nodes << node if found
58
- end
59
- end
60
- found_nodes
61
- end
62
-
63
- def to_gid
64
- [@class_name, @props_json]
65
- end
66
-
67
- def to_transport(inline: false)
68
- if inline
69
- { '_inline' => { @props_json => nodes_as_cids }}
70
- else
71
- { 'collections' => { @class_name => { @props_json => nodes_as_cids }}}
72
- end
73
- end
74
-
75
- def included_items_to_transport
76
- nodes_hash = {}
77
- nodes.each do |node|
78
- nodes_hash.deep_merge!(node.to_transport)
79
- end
80
- nodes_hash
81
- end
82
-
83
- base.instance_exec do
84
- def on_load_block
85
- @on_load_block
86
- end
87
-
88
- def query_block
89
- @query_block
90
- end
91
- end
92
-
93
- if RUBY_ENGINE == 'opal'
94
- def initialize(store_path: nil, validated_props: nil)
95
- @props = validated_props
96
- @props_json = @props.to_json if @props
97
- @store_path = store_path
98
- @class_name = self.class.name
99
- @class_name = @class_name.split('>::').last if @class_name.start_with?('#<')
100
- @store_path = store_path ? store_path : [:data_state, :collections, @class_name, @props_json]
101
- end
102
-
103
- def loaded?
104
- Redux.fetch_by_path(*@store_path) ? true : false
105
- end
106
-
107
- def find_node_by_id(node_id)
108
- nodes_as_cids.each do |node_cid|
109
- return LucidNode::Base.node_from_cid(node_cid) if node_cid[1] == node_id
110
- end
111
- nil
112
- end
113
-
114
- def nodes
115
- # maybe use a node cache, maybe not:
116
- # - pro node cache: maybe faster
117
- # - contra node cache: js garbage collection fails because references are kept forever, memory usage just grows and grows
118
- nodes_as_cids.map { |node_cid| LucidNode::Base.node_from_cid(node_cid) }
119
- end
120
-
121
- def nodes_as_cids
122
- node_cids = Redux.fetch_by_path(*@store_path)
123
- node_cids ? node_cids : []
124
- end
125
-
126
- def method_missing(method_name, *args, &block)
127
- if method_name.JS.startsWith('find_node_by_')
128
- attribute = method_name[13..-1] # remove 'find_node_by_'
129
- value = args[0]
130
- attribute_hash = { attribute => value }
131
- attribute_hash.merge!(args[1]) if args[1]
132
- find_node(attribute_hash)
133
- else
134
- super
135
- end
136
- end
137
-
138
- base.instance_exec do
139
- def load(props_hash = {})
140
- validate_props(props_hash)
141
- instance = self.new(validated_props: Isomorfeus::Data::Props.new(props_hash))
142
- self.promise_load(props_hash, instance) unless instance.loaded?
143
- instance
144
- end
145
-
146
- def on_load(&block)
147
- end
148
-
149
- def promise_load(props_hash = {}, instance = nil)
150
- unless instance
151
- validate_props(props_hash)
152
- instance = self.new(validated_props: Isomorfeus::Data::Props.new(props_hash))
153
- end
154
-
155
- props_json = instance.instance_variable_get(:@props_json)
156
-
157
- Isomorfeus::Transport.promise_send_path('Isomorfeus::Data::Handler::CollectionLoadHandler', self.name, props_json).then do |agent|
158
- if agent.processed
159
- agent.result
160
- else
161
- agent.processed = true
162
- if agent.response.key?(:error)
163
- `console.error(#{agent.response[:error].to_n})`
164
- raise agent.response[:error]
165
- end
166
- Isomorfeus.store.dispatch(type: 'DATA_LOAD', data: agent.full_response[:data])
167
- agent.result = instance
168
- end
169
- end
170
- end
171
-
172
- def query
173
- nil
174
- end
175
- end
176
- else # RUBY_ENGINE
177
- unless base == LucidCollection::Base
178
- base.prop :pub_sub_client, default: nil
179
- base.prop :current_user, default: nil
180
- end
181
-
182
- def initialize(store_path: nil, validated_props: nil)
183
- @props = validated_props
184
- @props_json = @props.to_json if @props
185
- @loaded = false
186
- @class_name = self.class.name
187
- @class_name = @class_name.split('>::').last if @class_name.start_with?('#<')
188
- end
189
-
190
- def loaded?
191
- @loaded
192
- end
193
-
194
- def method_missing(method_name, *args, &block)
195
- if method_name.start_with?('find_node_by_')
196
- attribute = method_name[13..-1] # remove 'find_node_by_'
197
- value = args[0]
198
- attribute_hash = { attribute => value }
199
- attribute_hash.merge!(args[1]) if args[1]
200
- find_node(attribute_hash)
201
- else
202
- super
203
- end
204
- end
205
-
206
- def nodes_as_cids
207
- nodes.map { |node| node.to_cid }
208
- end
209
-
210
- base.instance_exec do
211
- attr_reader :nodes
212
-
213
- def load(props_hash = {})
214
- validate_props(props_hash)
215
- instance = self.new(validated_props: Isomorfeus::Data::Props.new(props_hash))
216
- instance.instance_exec do
217
- @nodes = self.class.query_block.call(props_hash)
218
- @loaded = true
219
- end
220
- instance
221
- end
222
-
223
- def on_load(&block)
224
- @on_load_block = block
225
- end
226
-
227
- def promise_load(props_hash = {}, instance = nil)
228
- instance = self.load(props_hash)
229
- result_promise = Promise.new
230
- result_promise.resolve(instance)
231
- result_promise
232
- end
233
-
234
- def query(&block)
235
- @query_block = block
236
- end
237
- end
238
- end # RUBY_ENGINE
239
- end
240
- end
241
- end
@@ -1,5 +0,0 @@
1
- module LucidEdge
2
- class Base
3
- include LucidEdge::Mixin
4
- end
5
- end
@@ -1,238 +0,0 @@
1
- module LucidEdge
2
- module Mixin
3
- def self.included(base)
4
- attr_reader :id
5
-
6
- def ==(other_node)
7
- eql?(other_node)
8
- end
9
-
10
- def eql?(other_node)
11
- @id == other_node.id && @class_name == other_node.instance_variable_get(:@class_name)
12
- end
13
-
14
- def id=(new_id)
15
- new_id = new_id.to_s
16
- changed_attributes[:id] = new_id
17
- @id = new_id
18
- end
19
-
20
- def changed_attributes
21
- @changed_attributes ||= Isomorfeus::Data::Props.new({})
22
- end
23
-
24
- def changed?
25
- changed_attributes.any?
26
- end
27
-
28
- def to=(node)
29
- @changed_to_cid = node.to_cid
30
- node
31
- end
32
-
33
- def to_cid
34
- [@class_name, @id]
35
- end
36
-
37
- def valid_attribute?(attr, value)
38
- begin
39
- validate_attribute!(attr, value)
40
- true
41
- rescue
42
- false
43
- end
44
- end
45
-
46
- def validate_attribute!(attr, value)
47
- attr_options = self.class.attribute_options[attr]
48
-
49
- if attr_options.key?(:class)
50
- raise "#{attr}: value class is not #{attr_options[:class]}!" unless value.class == attr_options[:class]
51
- end
52
-
53
- if attr_options.key?(:is_a)
54
- raise "#{attr}: value is not a #{attr_options[:class]}!" unless value.is_a?(attr_options[:is_a])
55
- end
56
-
57
- if attr_options.key?(:validate)
58
- raise "#{attr}: value failed validation!" unless attr_options[:validate].call(value)
59
- end
60
- end
61
-
62
- base.instance_exec do
63
- def attributes
64
- attribute_options.keys
65
- end
66
-
67
- def attribute_options
68
- @attribute_options ||= { id: {} }
69
- end
70
-
71
- def edge_from_cid(cid)
72
- Isomorfeus.cached_edge_class(cid[0]).new({id: cid[1]})
73
- end
74
- end
75
-
76
- if RUBY_ENGINE == 'opal'
77
- def initialize(attributes_hash = nil)
78
- attributes_hash = {} unless attributes_hash
79
- self.class.attributes.each do |attr|
80
- next if attr == :to || attr == :from
81
- if attributes_hash.key?(attr)
82
- validate_attribute!(attr, attributes_hash[attr])
83
- changed_attributes[attr] = attributes_hash[attr]
84
- elsif self.class.attribute_options[attr].key?(:default)
85
- changed_attributes[attr] = self.class.attribute_options[attr][:default]
86
- end
87
- end
88
- @id = attributes_hash[:id].to_s
89
- @id = "new_#{object_id}" if @id.empty?
90
- @changed_from_cid = attributes_hash[:from]&.to_cid
91
- @changed_to_cid = attributes_hash[:to]&.to_cid
92
- @class_name = self.class.name
93
- @class_name = @class_name.split('>::').last if @class_name.start_with?('#<')
94
- end
95
-
96
- def loaded?
97
- Redux.fetch_by_path(:data_state, :edges, @class_name, @id) ? true : false
98
- end
99
-
100
- def from
101
- cid = from_as_cid
102
- cid ? LucidNode::Base.node_from_cid(cid) : nil
103
- end
104
-
105
- def from_as_cid
106
- return @changed_from_cid if @changed_from_cid
107
- cid = Redux.fetch_by_path(:data_state, :edges, @class_name, @id, :from)
108
- cid ? cid : nil
109
- end
110
-
111
- def from=(node)
112
- @changed_from_cid = node.to_cid
113
- node
114
- end
115
-
116
- def to
117
- cid = to_as_cid
118
- cid ? LucidNode::Base.node_from_cid(cid) : nil
119
- end
120
-
121
- def to_as_cid
122
- return @changed_to_cid if @changed_to_cid
123
- cid = Redux.fetch_by_path(:data_state, :edges, @class_name, @id, :to)
124
- cid ? cid : nil
125
- end
126
-
127
- def to_transport(*args)
128
- final_attributes = {}
129
- self.class.attributes.each do |attr|
130
- next if attr == :id
131
- final_attributes[attr] = send(attr)
132
- end
133
- { 'edges' => { @class_name => { @id => { from: from_as_cid, to: to_as_cid, attributes: final_attributes }}}}
134
- end
135
-
136
- base.instance_exec do
137
- def attribute(name, options = {})
138
- attribute_options[name] = options
139
-
140
- define_method(name) do
141
- if changed_attributes.key?(name)
142
- changed_attributes[name]
143
- else
144
- Redux.fetch_by_path(:data_state, :edges, @class_name, @id, :attributes, name)
145
- end
146
- end
147
-
148
- define_method("#{name}=") do |arg|
149
- validate_attribute!(name, arg)
150
- changed_attributes.set(name, arg)
151
- end
152
- end
153
- end
154
- else # RUBY_ENGINE
155
- def initialize(attributes_hash = nil)
156
- attributes_hash = {} unless attributes_hash
157
- given_attributes = Isomorfeus::Data::Props.new(attributes_hash)
158
- valid_attributes_hash = {}
159
- self.class.attributes.each do |attr|
160
- next if attr == :to || attr == :from
161
- if given_attributes.key?(attr)
162
- validate_attribute!(attr, given_attributes[attr])
163
- valid_attributes_hash[attr] = given_attributes[attr]
164
- elsif self.class.attribute_options[attr].key?(:default)
165
- valid_attributes_hash[attr] = self.class.attribute_options[attr][:default]
166
- end
167
- end
168
- @attributes = Isomorfeus::Data::Props.new(valid_attributes_hash)
169
- @id = @attributes[:id].to_s
170
- @id = "new_#{object_id}" if @id.empty?
171
- @from_cid = given_attributes[:from]&.to_cid
172
- @changed_from_cid = nil
173
- @to_cid = given_attributes[:to]&.to_cid
174
- @changed_to_cid = nil
175
- @class_name = self.class.name
176
- @class_name = @class_name.split('>::').last if @class_name.start_with?('#<')
177
- end
178
-
179
- def loaded?
180
- true
181
- end
182
-
183
- def from
184
- from_cid = from_as_cid
185
- from_cid ? LucidNode::Base.node_from_cid(from_cid) : nil
186
- end
187
-
188
- def from_as_cid
189
- @changed_from_cid ? @changed_from_cid : @from_cid
190
- end
191
-
192
- def from=(node)
193
- @changed_from_cid = node.to_cid
194
- node
195
- end
196
-
197
- def to
198
- to_cid = to_as_cid
199
- to_cid ? LucidNode::Base.node_from_cid(to_cid) : nil
200
- end
201
-
202
- def to_as_cid
203
- @changed_to_cid ? @changed_to_cid : @to_cid
204
- end
205
-
206
- def to_transport(*args)
207
- final_attributes = {}
208
- self.class.attributes.each do |attr|
209
- next if attr == :id
210
- include_attribute = @attributes.key?(attr)
211
- include_attribute = !self.class.attribute_options[attr][:server_only] if self.class.attribute_options[attr].key?(:server_only)
212
- final_attributes[attr.to_s] = @attributes[attr] if include_attribute
213
- end
214
- { 'edges' => { @class_name => { @id => { 'from' => from_as_cid, 'to' => to_as_cid, 'attributes' => final_attributes }}}}
215
- end
216
-
217
- base.instance_exec do
218
- def attribute(name, options = {})
219
- attribute_options[name] = options
220
-
221
- define_method(name) do
222
- if changed_attributes.key?(name)
223
- changed_attributes[name]
224
- else
225
- @attributes[name]
226
- end
227
- end
228
-
229
- define_method("#{name}=") do |arg|
230
- validate_attribute!(name, arg)
231
- changed_attributes.set(name, arg)
232
- end
233
- end
234
- end
235
- end # RUBY_ENGINE
236
- end
237
- end
238
- end
@@ -1,14 +0,0 @@
1
- module LucidGraph
2
- class Base
3
- include LucidGraph::Mixin
4
-
5
- if RUBY_ENGINE != 'opal'
6
- def self.inherited(base)
7
- Isomorfeus.add_valid_graph_class(base)
8
-
9
- base.prop :pub_sub_client, default: nil
10
- base.prop :current_user, default: nil
11
- end
12
- end
13
- end
14
- end