isomorfeus-data 1.0.0.zeta25 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +47 -21
  3. data/README.md +38 -46
  4. data/lib/isomorfeus/data/attribute_support.rb +176 -176
  5. data/lib/isomorfeus/data/config.rb +79 -110
  6. data/lib/isomorfeus/data/element_validator.rb +147 -147
  7. data/lib/isomorfeus/data/ferret_accelerator.rb +66 -0
  8. data/lib/isomorfeus/data/field_support.rb +110 -0
  9. data/lib/isomorfeus/data/generic_class_api.rb +136 -116
  10. data/lib/isomorfeus/data/generic_instance_api.rb +150 -145
  11. data/lib/isomorfeus/data/hamster_accelerator.rb +87 -0
  12. data/lib/isomorfeus/data/hamster_storage_expander.rb +94 -0
  13. data/lib/isomorfeus/data/handler/generic.rb +134 -138
  14. data/lib/isomorfeus/data/reducer.rb +30 -30
  15. data/lib/isomorfeus/data/version.rb +4 -4
  16. data/lib/isomorfeus-data.rb +56 -64
  17. data/lib/isomorfeus_data/lucid_document/base.rb +10 -0
  18. data/lib/isomorfeus_data/lucid_document/mixin.rb +148 -0
  19. data/lib/isomorfeus_data/lucid_file/base.rb +10 -0
  20. data/lib/isomorfeus_data/lucid_file/mixin.rb +217 -0
  21. data/lib/isomorfeus_data/lucid_object/base.rb +10 -0
  22. data/lib/isomorfeus_data/lucid_object/mixin.rb +216 -0
  23. data/lib/isomorfeus_data/lucid_query/base.rb +10 -0
  24. data/lib/isomorfeus_data/lucid_query/mixin.rb +81 -0
  25. data/lib/isomorfeus_data/lucid_query_result.rb +97 -0
  26. data/opal/uri/common.rb +18 -0
  27. data/opal/uri/generic.rb +47 -0
  28. data/opal/uri.rb +29 -0
  29. metadata +85 -96
  30. data/lib/isomorfeus/data/handler/arango.rb +0 -56
  31. data/lib/isomorfeus/data/handler/object_call.rb +0 -40
  32. data/lib/isomorfeus/data/handler/object_store.rb +0 -40
  33. data/lib/isomorfeus_data/lucid_data/array/base.rb +0 -13
  34. data/lib/isomorfeus_data/lucid_data/array/mixin.rb +0 -580
  35. data/lib/isomorfeus_data/lucid_data/collection/base.rb +0 -13
  36. data/lib/isomorfeus_data/lucid_data/collection/finders.rb +0 -83
  37. data/lib/isomorfeus_data/lucid_data/collection/mixin.rb +0 -737
  38. data/lib/isomorfeus_data/lucid_data/composition/base.rb +0 -13
  39. data/lib/isomorfeus_data/lucid_data/composition/mixin.rb +0 -234
  40. data/lib/isomorfeus_data/lucid_data/document/base.rb +0 -13
  41. data/lib/isomorfeus_data/lucid_data/document/mixin.rb +0 -9
  42. data/lib/isomorfeus_data/lucid_data/edge/base.rb +0 -13
  43. data/lib/isomorfeus_data/lucid_data/edge/mixin.rb +0 -286
  44. data/lib/isomorfeus_data/lucid_data/edge_collection/base.rb +0 -13
  45. data/lib/isomorfeus_data/lucid_data/edge_collection/finders.rb +0 -134
  46. data/lib/isomorfeus_data/lucid_data/edge_collection/mixin.rb +0 -747
  47. data/lib/isomorfeus_data/lucid_data/generic_collection.rb +0 -4
  48. data/lib/isomorfeus_data/lucid_data/generic_edge.rb +0 -4
  49. data/lib/isomorfeus_data/lucid_data/generic_edge_collection.rb +0 -4
  50. data/lib/isomorfeus_data/lucid_data/generic_node.rb +0 -4
  51. data/lib/isomorfeus_data/lucid_data/graph/base.rb +0 -13
  52. data/lib/isomorfeus_data/lucid_data/graph/finders.rb +0 -141
  53. data/lib/isomorfeus_data/lucid_data/graph/mixin.rb +0 -489
  54. data/lib/isomorfeus_data/lucid_data/hash/base.rb +0 -13
  55. data/lib/isomorfeus_data/lucid_data/hash/mixin.rb +0 -427
  56. data/lib/isomorfeus_data/lucid_data/link/base.rb +0 -13
  57. data/lib/isomorfeus_data/lucid_data/link/mixin.rb +0 -9
  58. data/lib/isomorfeus_data/lucid_data/link_collection/base.rb +0 -13
  59. data/lib/isomorfeus_data/lucid_data/link_collection/mixin.rb +0 -9
  60. data/lib/isomorfeus_data/lucid_data/node/base.rb +0 -13
  61. data/lib/isomorfeus_data/lucid_data/node/mixin.rb +0 -232
  62. data/lib/isomorfeus_data/lucid_data/query/base.rb +0 -13
  63. data/lib/isomorfeus_data/lucid_data/query/mixin.rb +0 -83
  64. data/lib/isomorfeus_data/lucid_data/query_result.rb +0 -95
  65. data/lib/isomorfeus_data/lucid_data/vertex/base.rb +0 -13
  66. data/lib/isomorfeus_data/lucid_data/vertex/mixin.rb +0 -9
  67. data/lib/lucid_arango/collection/base.rb +0 -13
  68. data/lib/lucid_arango/collection/mixin.rb +0 -18
  69. data/lib/lucid_arango/document/base.rb +0 -13
  70. data/lib/lucid_arango/document/mixin.rb +0 -9
  71. data/lib/lucid_arango/edge/base.rb +0 -13
  72. data/lib/lucid_arango/edge/mixin.rb +0 -18
  73. data/lib/lucid_arango/edge_collection/base.rb +0 -13
  74. data/lib/lucid_arango/edge_collection/mixin.rb +0 -18
  75. data/lib/lucid_arango/graph/base.rb +0 -13
  76. data/lib/lucid_arango/graph/mixin.rb +0 -18
  77. data/lib/lucid_arango/node/base.rb +0 -13
  78. data/lib/lucid_arango/node/mixin.rb +0 -79
  79. data/lib/lucid_arango/object/base.rb +0 -11
  80. data/lib/lucid_arango/object/mixin.rb +0 -158
  81. data/lib/lucid_arango/remote_object/base.rb +0 -11
  82. data/lib/lucid_arango/remote_object/mixin.rb +0 -17
  83. data/lib/lucid_arango/vertex/base.rb +0 -13
  84. data/lib/lucid_arango/vertex/mixin.rb +0 -9
@@ -1,110 +1,79 @@
1
- module Isomorfeus
2
- # available settings
3
- class << self
4
- def instance_from_sid(sid)
5
- data_class = cached_data_class(sid[0])
6
- data_class.new(key: sid[1])
7
- end
8
-
9
- if RUBY_ENGINE == 'opal'
10
- def cached_data_classes
11
- @cached_data_classes ||= `{}`
12
- end
13
-
14
- def cached_data_class(class_name)
15
- return "::#{class_name}".constantize if Isomorfeus.development?
16
- return cached_data_classes.JS[class_name] if cached_data_classes.JS[class_name]
17
- cached_data_classes.JS[class_name] = "::#{class_name}".constantize
18
- end
19
- else
20
- def cached_data_classes
21
- @cached_data_classes ||= {}
22
- end
23
-
24
- def cached_data_class(class_name)
25
- return "::#{class_name}".constantize if Isomorfeus.development?
26
- return cached_data_classes[class_name] if cached_data_classes.key?(class_name)
27
- cached_data_classes[class_name] = "::#{class_name}".constantize
28
- end
29
-
30
- def valid_data_classes
31
- @valid_data_classes ||= {}
32
- end
33
-
34
- def valid_data_class_name?(class_name)
35
- valid_data_classes.key?(class_name)
36
- end
37
-
38
- def add_valid_data_class(klass)
39
- valid_data_classes[raw_class_name(klass)] = true
40
- end
41
-
42
- def connect_to_arango
43
- arango_options = if Isomorfeus.production? then Isomorfeus.arango_production
44
- elsif Isomorfeus.development? then Isomorfeus.arango_development
45
- elsif Isomorfeus.test? then Isomorfeus.arango_test
46
- end
47
- arango_options = {}.merge(arango_options)
48
- database = arango_options.delete(:database)
49
- Arango.connect_to(**arango_options)
50
- begin
51
- Arango.current_server.get_database(database)
52
- rescue Exception => e
53
- Isomorfeus.raise_error(message: "Can't connect to database '#{database}' (#{e.message}).")
54
- end
55
- end
56
-
57
- def prepare_arango_database
58
- unless arango_configured?
59
- STDOUT.puts "ArangoDB not configured, not preparing things, not using ArangoDB"
60
- return
61
- end
62
- arango_options = if Isomorfeus.production? then Isomorfeus.arango_production
63
- elsif Isomorfeus.development? then Isomorfeus.arango_development
64
- elsif Isomorfeus.test? then Isomorfeus.arango_test
65
- end
66
- arango_options = {}.merge(arango_options)
67
- database = arango_options.delete(:database)
68
- Arango.connect_to(**arango_options)
69
- db = nil
70
- begin
71
- opened_db = Arango.current_server.get_database(database)
72
- db = opened_db.name
73
- rescue Exception => e
74
- db = nil
75
- unless e.message.include?('database not found')
76
- Isomorfeus.raise_error(message: "Can't check if database '#{database}' exists.")
77
- end
78
- end
79
- unless db
80
- begin
81
- Arango.current_server.create_database(database)
82
- rescue Exception => e
83
- Isomorfeus.raise_error(message: "Can't create database '#{database}' (#{e.message}).\nPlease make sure database '#{database}' exists.")
84
- end
85
- begin
86
- Arango.current_server.get_database(database)
87
- rescue Exception => e
88
- Isomorfeus.raise_error(message: "Can't connect to database '#{database}' (#{e.message}).")
89
- end
90
- end
91
-
92
- Arango.current_server.install_opal_module(database)
93
- unless Arango.current_database.collection_exist?('IsomorfeusSessions')
94
- Arango.current_database.create_collection('IsomorfeusSessions')
95
- end
96
- unless Arango.current_database.collection_exist?('IsomorfeusObjectStore')
97
- Arango.current_database.create_collection('IsomorfeusObjectStore')
98
- end
99
- end
100
-
101
- def arango_configured?
102
- (defined? ::Arango) && !!(Isomorfeus.arango_production && Isomorfeus.arango_development && Isomorfeus.arango_test)
103
- end
104
-
105
- attr_accessor :arango_production
106
- attr_accessor :arango_development
107
- attr_accessor :arango_test
108
- end
109
- end
110
- end
1
+ module Isomorfeus
2
+ # available settings
3
+ class << self
4
+ def instance_from_sid(sid)
5
+ data_class = cached_data_class(sid[0])
6
+ data_class.new(key: sid[1])
7
+ end
8
+
9
+ if RUBY_ENGINE == 'opal'
10
+ def cached_data_classes
11
+ @cached_data_classes ||= `{}`
12
+ end
13
+
14
+ def cached_data_class(class_name)
15
+ return "::#{class_name}".constantize if Isomorfeus.development?
16
+ return cached_data_classes.JS[class_name] if cached_data_classes.JS[class_name]
17
+ cached_data_classes.JS[class_name] = "::#{class_name}".constantize
18
+ end
19
+ else
20
+ def cached_data_classes
21
+ @cached_data_classes ||= {}
22
+ end
23
+
24
+ def cached_data_class(class_name)
25
+ return "::#{class_name}".constantize if Isomorfeus.development?
26
+ return cached_data_classes[class_name] if cached_data_classes.key?(class_name)
27
+ cached_data_classes[class_name] = "::#{class_name}".constantize
28
+ end
29
+
30
+ def valid_data_classes
31
+ @valid_data_classes ||= {}
32
+ end
33
+
34
+ def valid_data_class_name?(class_name)
35
+ valid_data_classes.key?(class_name)
36
+ end
37
+
38
+ def add_valid_data_class(klass)
39
+ valid_data_classes[raw_class_name(klass)] = true
40
+ end
41
+
42
+ def valid_file_classes
43
+ @valid_file_classes ||= {}
44
+ end
45
+
46
+ def valid_file_class_name?(class_name)
47
+ valid_file_classes.key?(class_name)
48
+ end
49
+
50
+ def add_valid_file_class(klass)
51
+ valid_file_classes[raw_class_name(klass)] = true
52
+ end
53
+
54
+ attr_accessor :data_path
55
+ attr_accessor :files_path
56
+
57
+ attr_accessor :ferret_path
58
+ attr_accessor :data_documents_path
59
+
60
+ attr_accessor :hamster_path
61
+ attr_accessor :data_object_env_path
62
+ attr_accessor :data_object_idx_path
63
+ end
64
+ end
65
+
66
+ if RUBY_ENGINE != 'opal'
67
+ self.data_path = File.expand_path(File.join(Isomorfeus.root, 'data', Isomorfeus.env))
68
+ self.files_path = File.expand_path(File.join(self.data_path, 'files'))
69
+
70
+ # documents and indices
71
+ self.ferret_path = File.expand_path(File.join(self.data_path, 'ferret'))
72
+ self.data_documents_path = File.expand_path(File.join(self.ferret_path, 'documents'))
73
+
74
+ # objects, nodes and edges
75
+ self.hamster_path = File.expand_path(File.join(self.data_path, 'hamster'))
76
+ self.data_object_env_path = File.expand_path(File.join(self.hamster_path, 'object_env'))
77
+ self.data_object_idx_path = File.expand_path(File.join(self.hamster_path, 'object_idx'))
78
+ end
79
+ end
@@ -1,147 +1,147 @@
1
- module Isomorfeus
2
- module Data
3
- class ElementValidator
4
- def initialize(source_class, element, options)
5
- @c = source_class
6
- @e = element
7
- @o = options
8
- end
9
-
10
- def validate!
11
- ensured = ensure!
12
- unless ensured
13
- cast!
14
- type!
15
- end
16
- run_checks!
17
- true
18
- end
19
-
20
- private
21
-
22
- # basic tests
23
-
24
- def cast!
25
- if @o.key?(:cast)
26
- begin
27
- @e = case @o[:class]
28
- when Integer then @e.to_i
29
- when String then @e.to_s
30
- when Float then @e.to_f
31
- when Array then @e.to_a
32
- when Hash then @e.to_h
33
- end
34
- @e = !!@e if @o[:type] == :boolean
35
- rescue
36
- Isomorfeus.raise_error(message: "#{@c}: #{@p} cast failed") unless @e.class == @o[:class]
37
- end
38
- end
39
- end
40
-
41
- def ensure!
42
- if @o.key?(:ensure)
43
- @e = @o[:ensure] unless @e
44
- true
45
- elsif @o.key?(:ensure_block)
46
- @e = @o[:ensure_block].call(@e)
47
- true
48
- else
49
- false
50
- end
51
- end
52
-
53
- def type!
54
- return if @o[:allow_nil] && @e.nil?
55
- if @o.key?(:class)
56
- Isomorfeus.raise_error(message: "#{@c}: #{@p} class not #{@o[:class]}") unless @e.class == @o[:class]
57
- elsif @o.key?(:is_a)
58
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a #{@o[:is_a]}") unless @e.is_a?(@o[:is_a])
59
- elsif @o.key?(:type)
60
- case @o[:type]
61
- when :boolean
62
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a boolean") unless @e.class == TrueClass || @e.class == FalseClass
63
- else
64
- c_string_sub_types
65
- end
66
- end
67
- end
68
-
69
- # all other checks
70
-
71
- def run_checks!
72
- if @o.key?(:validate)
73
- @o[:validate].each do |m, l|
74
- send('c_' + m, l)
75
- end
76
- end
77
- @o[:validate_block].call(@e) if @o.key?(:validate_block)
78
- end
79
-
80
- # specific validations
81
- def c_gt(v)
82
- Isomorfeus.raise_error(message: "#{@c}: #{@p} not greater than #{v}!") unless @e > v
83
- end
84
-
85
- def c_lt(v)
86
- Isomorfeus.raise_error(message: "#{@c}: #{@p} not less than #{v}!") unless @e < v
87
- end
88
-
89
- def c_keys(v)
90
- Isomorfeus.raise_error(message: "#{@c}: #{@p} keys dont fit!") unless @e.keys.sort == v.sort
91
- end
92
-
93
- def c_size(v)
94
- Isomorfeus.raise_error(message: "#{@c}: #{@p} length/size is not #{v}") unless @e.size == v
95
- end
96
-
97
- def c_matches(v)
98
- Isomorfeus.raise_error(message: "#{@c}: #{@p} does not match #{v}") unless v.match?(@e)
99
- end
100
-
101
- def c_max(v)
102
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is larger than #{v}") unless @e <= v
103
- end
104
-
105
- def c_min(v)
106
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is smaller than #{v}") unless @e >= v
107
- end
108
-
109
- def c_max_size(v)
110
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is larger than #{v}") unless @e.size <= v
111
- end
112
-
113
- def c_min_size(v)
114
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is smaller than #{v}") unless @e.size >= v
115
- end
116
-
117
- def c_direction(v)
118
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is positive") if v == :negative && @e >= 0
119
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is negative") if v == :positive && @e < 0
120
- end
121
-
122
- def c_test
123
- Isomorfeus.raise_error(message: "#{@c}: #{@p} test condition check failed") unless @o[:test].call(@e)
124
- end
125
-
126
- def c_string_sub_types
127
- Isomorfeus.raise_error(message: "#{@c}: #{@p} must be a String") unless @e.class == String
128
- case @o[:type]
129
- when :email
130
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a valid email address") unless @e.match? /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/
131
- when :uri
132
- if RUBY_ENGINE == 'opal'
133
- %x{
134
- try {
135
- new URL(#@e);
136
- } catch {
137
- #{Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a valid uri")}
138
- }
139
- }
140
- else
141
- Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a valid uri") unless @e.match? /\A#{URI.regexp}\z/
142
- end
143
- end
144
- end
145
- end
146
- end
147
- end
1
+ module Isomorfeus
2
+ module Data
3
+ class ElementValidator
4
+ def initialize(source_class, element, options)
5
+ @c = source_class
6
+ @e = element
7
+ @o = options
8
+ end
9
+
10
+ def validate!
11
+ ensured = ensure!
12
+ unless ensured
13
+ cast!
14
+ type!
15
+ end
16
+ run_checks!
17
+ true
18
+ end
19
+
20
+ private
21
+
22
+ # basic tests
23
+
24
+ def cast!
25
+ if @o.key?(:cast)
26
+ begin
27
+ @e = case @o[:class]
28
+ when Integer then @e.to_i
29
+ when String then @e.to_s
30
+ when Float then @e.to_f
31
+ when Array then @e.to_a
32
+ when Hash then @e.to_h
33
+ end
34
+ @e = !!@e if @o[:type] == :boolean
35
+ rescue
36
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} cast failed") unless @e.class == @o[:class]
37
+ end
38
+ end
39
+ end
40
+
41
+ def ensure!
42
+ if @o.key?(:ensure)
43
+ @e = @o[:ensure] unless @e
44
+ true
45
+ elsif @o.key?(:ensure_block)
46
+ @e = @o[:ensure_block].call(@e)
47
+ true
48
+ else
49
+ false
50
+ end
51
+ end
52
+
53
+ def type!
54
+ return if @o[:allow_nil] && @e.nil?
55
+ if @o.key?(:class)
56
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} class not #{@o[:class]}") unless @e.class == @o[:class]
57
+ elsif @o.key?(:is_a)
58
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a #{@o[:is_a]}") unless @e.is_a?(@o[:is_a])
59
+ elsif @o.key?(:type)
60
+ case @o[:type]
61
+ when :boolean
62
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a boolean") unless @e.class == TrueClass || @e.class == FalseClass
63
+ else
64
+ c_string_sub_types
65
+ end
66
+ end
67
+ end
68
+
69
+ # all other checks
70
+
71
+ def run_checks!
72
+ if @o.key?(:validate)
73
+ @o[:validate].each do |m, l|
74
+ send('c_' + m, l)
75
+ end
76
+ end
77
+ @o[:validate_block].call(@e) if @o.key?(:validate_block)
78
+ end
79
+
80
+ # specific validations
81
+ def c_gt(v)
82
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} not greater than #{v}!") unless @e > v
83
+ end
84
+
85
+ def c_lt(v)
86
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} not less than #{v}!") unless @e < v
87
+ end
88
+
89
+ def c_keys(v)
90
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} keys dont fit!") unless @e.keys.sort == v.sort
91
+ end
92
+
93
+ def c_size(v)
94
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} length/size is not #{v}") unless @e.size == v
95
+ end
96
+
97
+ def c_matches(v)
98
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} does not match #{v}") unless v.match?(@e)
99
+ end
100
+
101
+ def c_max(v)
102
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is larger than #{v}") unless @e <= v
103
+ end
104
+
105
+ def c_min(v)
106
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is smaller than #{v}") unless @e >= v
107
+ end
108
+
109
+ def c_max_size(v)
110
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is larger than #{v}") unless @e.size <= v
111
+ end
112
+
113
+ def c_min_size(v)
114
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is smaller than #{v}") unless @e.size >= v
115
+ end
116
+
117
+ def c_direction(v)
118
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is positive") if v == :negative && @e >= 0
119
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is negative") if v == :positive && @e < 0
120
+ end
121
+
122
+ def c_test
123
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} test condition check failed") unless @o[:test].call(@e)
124
+ end
125
+
126
+ def c_string_sub_types
127
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} must be a String") unless @e.class == String
128
+ case @o[:type]
129
+ when :email
130
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a valid email address") unless @e.match?(/\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/)
131
+ when :uri
132
+ if RUBY_ENGINE == 'opal'
133
+ %x{
134
+ try {
135
+ new URL(#@e);
136
+ } catch {
137
+ #{Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a valid uri")}
138
+ }
139
+ }
140
+ else
141
+ Isomorfeus.raise_error(message: "#{@c}: #{@p} is not a valid uri") unless @e.match?(/\A#{URI.regexp}\z/)
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,66 @@
1
+ module Isomorfeus
2
+ module Data
3
+ class FerretAccelerator
4
+ def self.finalize(fer_acc)
5
+ proc { fer_acc.close_index }
6
+ end
7
+
8
+ attr_reader :doc_class, :doc_class_name, :doc_class_name_u
9
+ attr_accessor :index
10
+
11
+ def initialize(doc_class, &block)
12
+ @doc_class = doc_class
13
+ @doc_class_name = doc_class.name
14
+ @doc_class_name_u = @doc_class_name.underscore
15
+ if block_given?
16
+ res = block.call(self)
17
+ @index = res unless @index
18
+ else
19
+ open_index
20
+ end
21
+ ObjectSpace.define_finalizer(self, self.class.finalize(self))
22
+ end
23
+
24
+ def destroy_index
25
+ close_index
26
+ FileUtils.rm_rf(index_path(doc_class_name))
27
+ end
28
+
29
+ def close_index
30
+ @index.close
31
+ end
32
+
33
+ def create_doc(document)
34
+ @index.add_document(document)
35
+ end
36
+
37
+ def destroy_doc(id)
38
+ @index.delete(id)
39
+ true
40
+ end
41
+
42
+ def load_doc(id)
43
+ @index.doc(id)&.load
44
+ end
45
+
46
+ def save_doc(id, document)
47
+ @index.update(id, document)
48
+ end
49
+
50
+ def search_each(query, options, &block)
51
+ @index.search_each(query, options, &block)
52
+ end
53
+
54
+ private
55
+
56
+ def index_path
57
+ File.expand_path(File.join(Isomorfeus.data_documents_path, @doc_class_name_u))
58
+ end
59
+
60
+ def open_index
61
+ FileUtils.mkdir_p(Isomorfeus.data_documents_path) unless Dir.exist?(Isomorfeus.data_documents_path)
62
+ @index = Isomorfeus::Ferret::Index::Index.new(path: index_path, id_field: :key)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,110 @@
1
+ module Isomorfeus
2
+ module Data
3
+ module FieldSupport
4
+ def self.included(base)
5
+ base.instance_exec do
6
+ def field_options
7
+ @field_options ||= {}
8
+ end
9
+
10
+ def valid_field?(field_name, field_value)
11
+ field_options.key?(field_name)
12
+ end
13
+ end
14
+
15
+ def exclude_fields(*flds)
16
+ @_excluded_fields = flds
17
+ end
18
+
19
+ def select_fields(*flds)
20
+ @_selected_fields = flds
21
+ end
22
+
23
+ if RUBY_ENGINE == 'opal'
24
+ base.instance_exec do
25
+ def field(name, options = nil)
26
+ field_options[name] = options
27
+
28
+ define_method(name) do
29
+ _get_field(name)
30
+ end
31
+
32
+ define_method("#{name}=") do |val|
33
+ val = val.to_s unless val.class == String
34
+ changed!
35
+ @_changed_fields[name] = val
36
+ end
37
+ end
38
+ end
39
+
40
+ def _get_field(name)
41
+ return @_changed_fields[name] if @_changed_fields.key?(name)
42
+ path = @_store_path + [name]
43
+ result = Redux.fetch_by_path(*path)
44
+ %x{
45
+ if (result === null) { return nil; }
46
+ else { return result; }
47
+ }
48
+ end
49
+
50
+ def fields
51
+ raw_fields = Redux.fetch_by_path(*@_store_path)
52
+ hash = Hash.new(raw_fields)
53
+ hash.merge!(@_changed_fields) if @_changed_fields
54
+ hash
55
+ end
56
+
57
+ def _get_selected_fields
58
+ sel_fields = fields.dup
59
+ if @_selected_fields && !@_selected_fields.empty?
60
+ sel_fields.each_key do |fld|
61
+ unless @_selected_fields.include?(fld) || @_selected_fields.include?(fld)
62
+ sel_fields.delete(fld)
63
+ end
64
+ end
65
+ end
66
+ if @_excluded_fields && !@_excluded_fields.empty?
67
+ @_excluded_fields.each { |fld| sel_fields.delete(fld) }
68
+ end
69
+ sel_fields
70
+ end
71
+ else
72
+ base.instance_exec do
73
+ def field(name, options = {})
74
+ field_options[name] = options
75
+
76
+ define_method(name) do
77
+ @_raw_fields[name]
78
+ end
79
+
80
+ define_method("#{name}=") do |val|
81
+ val = val.to_s unless val.class == String
82
+ changed!
83
+ @_raw_fields[name] = val
84
+ end
85
+ end
86
+ end
87
+
88
+ def fields
89
+ @_raw_fields
90
+ end
91
+
92
+ def _get_selected_fields
93
+ sel_fields = fields.transform_keys(&:to_s)
94
+ if @_selected_fields && !@_selected_fields.empty?
95
+ sel_fields.each_key do |fld|
96
+ unless @_selected_fields.include?(fld.to_sym) || @_selected_fields.include?(fld)
97
+ sel_fields.delete(fld)
98
+ end
99
+ end
100
+ end
101
+ if @_excluded_fields && !@_excluded_fields.empty?
102
+ @_excluded_fields.each { |fld| sel_fields.delete(fld.to_s) }
103
+ end
104
+ sel_fields
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end