neo4j_legacy 7.2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +1357 -0
  3. data/CONTRIBUTORS +8 -0
  4. data/Gemfile +38 -0
  5. data/README.md +103 -0
  6. data/bin/neo4j-jars +33 -0
  7. data/bin/rake +17 -0
  8. data/config/locales/en.yml +5 -0
  9. data/config/neo4j/add_classnames.yml +1 -0
  10. data/config/neo4j/config.yml +35 -0
  11. data/lib/active_support/per_thread_registry.rb +1 -0
  12. data/lib/backports/action_controller/metal/strong_parameters.rb +672 -0
  13. data/lib/backports/active_model/forbidden_attributes_protection.rb +30 -0
  14. data/lib/backports/active_support/concern.rb +13 -0
  15. data/lib/backports/active_support/core_ext/module/attribute_accessors.rb +10 -0
  16. data/lib/backports/active_support/logger.rb +99 -0
  17. data/lib/backports/active_support/logger_silence.rb +27 -0
  18. data/lib/backports/active_support/logger_thread_safe_level.rb +32 -0
  19. data/lib/backports/active_support/per_thread_registry.rb +60 -0
  20. data/lib/backports.rb +4 -0
  21. data/lib/neo4j/active_node/callbacks.rb +8 -0
  22. data/lib/neo4j/active_node/dependent/association_methods.rb +48 -0
  23. data/lib/neo4j/active_node/dependent/query_proxy_methods.rb +50 -0
  24. data/lib/neo4j/active_node/dependent.rb +11 -0
  25. data/lib/neo4j/active_node/enum.rb +29 -0
  26. data/lib/neo4j/active_node/has_n/association/rel_factory.rb +61 -0
  27. data/lib/neo4j/active_node/has_n/association/rel_wrapper.rb +23 -0
  28. data/lib/neo4j/active_node/has_n/association.rb +280 -0
  29. data/lib/neo4j/active_node/has_n/association_cypher_methods.rb +108 -0
  30. data/lib/neo4j/active_node/has_n.rb +532 -0
  31. data/lib/neo4j/active_node/id_property/accessor.rb +62 -0
  32. data/lib/neo4j/active_node/id_property.rb +187 -0
  33. data/lib/neo4j/active_node/initialize.rb +21 -0
  34. data/lib/neo4j/active_node/labels/index.rb +87 -0
  35. data/lib/neo4j/active_node/labels/reloading.rb +21 -0
  36. data/lib/neo4j/active_node/labels.rb +198 -0
  37. data/lib/neo4j/active_node/node_wrapper.rb +52 -0
  38. data/lib/neo4j/active_node/orm_adapter.rb +82 -0
  39. data/lib/neo4j/active_node/persistence.rb +175 -0
  40. data/lib/neo4j/active_node/property.rb +60 -0
  41. data/lib/neo4j/active_node/query/query_proxy.rb +361 -0
  42. data/lib/neo4j/active_node/query/query_proxy_eager_loading.rb +61 -0
  43. data/lib/neo4j/active_node/query/query_proxy_enumerable.rb +90 -0
  44. data/lib/neo4j/active_node/query/query_proxy_find_in_batches.rb +19 -0
  45. data/lib/neo4j/active_node/query/query_proxy_link.rb +117 -0
  46. data/lib/neo4j/active_node/query/query_proxy_methods.rb +210 -0
  47. data/lib/neo4j/active_node/query/query_proxy_methods_of_mass_updating.rb +83 -0
  48. data/lib/neo4j/active_node/query.rb +76 -0
  49. data/lib/neo4j/active_node/query_methods.rb +65 -0
  50. data/lib/neo4j/active_node/reflection.rb +86 -0
  51. data/lib/neo4j/active_node/rels.rb +11 -0
  52. data/lib/neo4j/active_node/scope.rb +146 -0
  53. data/lib/neo4j/active_node/unpersisted.rb +48 -0
  54. data/lib/neo4j/active_node/validations.rb +59 -0
  55. data/lib/neo4j/active_node.rb +105 -0
  56. data/lib/neo4j/active_rel/callbacks.rb +15 -0
  57. data/lib/neo4j/active_rel/initialize.rb +28 -0
  58. data/lib/neo4j/active_rel/persistence/query_factory.rb +95 -0
  59. data/lib/neo4j/active_rel/persistence.rb +114 -0
  60. data/lib/neo4j/active_rel/property.rb +95 -0
  61. data/lib/neo4j/active_rel/query.rb +95 -0
  62. data/lib/neo4j/active_rel/rel_wrapper.rb +22 -0
  63. data/lib/neo4j/active_rel/related_node.rb +83 -0
  64. data/lib/neo4j/active_rel/types.rb +82 -0
  65. data/lib/neo4j/active_rel/validations.rb +8 -0
  66. data/lib/neo4j/active_rel.rb +67 -0
  67. data/lib/neo4j/class_arguments.rb +39 -0
  68. data/lib/neo4j/config.rb +124 -0
  69. data/lib/neo4j/core/query.rb +22 -0
  70. data/lib/neo4j/errors.rb +28 -0
  71. data/lib/neo4j/migration.rb +127 -0
  72. data/lib/neo4j/paginated.rb +27 -0
  73. data/lib/neo4j/railtie.rb +169 -0
  74. data/lib/neo4j/schema/operation.rb +91 -0
  75. data/lib/neo4j/shared/attributes.rb +220 -0
  76. data/lib/neo4j/shared/callbacks.rb +64 -0
  77. data/lib/neo4j/shared/cypher.rb +37 -0
  78. data/lib/neo4j/shared/declared_properties.rb +204 -0
  79. data/lib/neo4j/shared/declared_property/index.rb +37 -0
  80. data/lib/neo4j/shared/declared_property.rb +118 -0
  81. data/lib/neo4j/shared/enum.rb +148 -0
  82. data/lib/neo4j/shared/filtered_hash.rb +79 -0
  83. data/lib/neo4j/shared/identity.rb +28 -0
  84. data/lib/neo4j/shared/initialize.rb +28 -0
  85. data/lib/neo4j/shared/marshal.rb +23 -0
  86. data/lib/neo4j/shared/mass_assignment.rb +58 -0
  87. data/lib/neo4j/shared/permitted_attributes.rb +28 -0
  88. data/lib/neo4j/shared/persistence.rb +231 -0
  89. data/lib/neo4j/shared/property.rb +220 -0
  90. data/lib/neo4j/shared/query_factory.rb +101 -0
  91. data/lib/neo4j/shared/rel_type_converters.rb +43 -0
  92. data/lib/neo4j/shared/serialized_properties.rb +30 -0
  93. data/lib/neo4j/shared/type_converters.rb +418 -0
  94. data/lib/neo4j/shared/typecasted_attributes.rb +98 -0
  95. data/lib/neo4j/shared/typecaster.rb +53 -0
  96. data/lib/neo4j/shared/validations.rb +48 -0
  97. data/lib/neo4j/shared.rb +51 -0
  98. data/lib/neo4j/tasks/migration.rake +24 -0
  99. data/lib/neo4j/timestamps/created.rb +9 -0
  100. data/lib/neo4j/timestamps/updated.rb +9 -0
  101. data/lib/neo4j/timestamps.rb +11 -0
  102. data/lib/neo4j/type_converters.rb +7 -0
  103. data/lib/neo4j/version.rb +3 -0
  104. data/lib/neo4j/wrapper.rb +4 -0
  105. data/lib/neo4j.rb +96 -0
  106. data/lib/rails/generators/neo4j/model/model_generator.rb +86 -0
  107. data/lib/rails/generators/neo4j/model/templates/model.erb +15 -0
  108. data/lib/rails/generators/neo4j_generator.rb +67 -0
  109. data/neo4j.gemspec +43 -0
  110. metadata +389 -0
@@ -0,0 +1,280 @@
1
+ require 'active_support/inflector/inflections'
2
+ require 'neo4j/class_arguments'
3
+
4
+ module Neo4j
5
+ module ActiveNode
6
+ module HasN
7
+ class Association
8
+ include Neo4j::Shared::RelTypeConverters
9
+ include Neo4j::ActiveNode::Dependent::AssociationMethods
10
+ include Neo4j::ActiveNode::HasN::AssociationCypherMethods
11
+
12
+ attr_reader :type, :name, :relationship, :direction, :dependent, :model_class
13
+
14
+ def initialize(type, direction, name, options = {type: nil})
15
+ validate_init_arguments(type, direction, name, options)
16
+ @type = type.to_sym
17
+ @name = name
18
+ @direction = direction.to_sym
19
+ @target_class_name_from_name = name.to_s.pluralize.classify
20
+ apply_vars_from_options(options)
21
+ end
22
+
23
+ def derive_model_class
24
+ refresh_model_class! if pending_model_refresh?
25
+ return @model_class unless @model_class.nil?
26
+ return nil if relationship_class.nil?
27
+ dir_class = direction == :in ? :from_class : :to_class
28
+ return false if relationship_class.send(dir_class).to_s.to_sym == :any
29
+ relationship_class.send(dir_class)
30
+ end
31
+
32
+ def refresh_model_class!
33
+ @pending_model_refresh = @target_classes_or_nil = nil
34
+
35
+ # Using #to_s on purpose here to take care of classes/strings/symbols
36
+ @model_class = ClassArguments.constantize_argument(@model_class.to_s) if @model_class
37
+ end
38
+
39
+ def queue_model_refresh!
40
+ @pending_model_refresh = true
41
+ end
42
+
43
+ def target_class_option(model_class)
44
+ case model_class
45
+ when nil
46
+ @target_class_name_from_name ? "#{association_model_namespace}::#{@target_class_name_from_name}" : @target_class_name_from_name
47
+ when Array
48
+ model_class.map { |sub_model_class| target_class_option(sub_model_class) }
49
+ when false
50
+ false
51
+ else
52
+ model_class.to_s[0, 2] == '::' ? model_class.to_s : "::#{model_class}"
53
+ end
54
+ end
55
+
56
+ def pending_model_refresh?
57
+ !!@pending_model_refresh
58
+ end
59
+
60
+ def target_class_names
61
+ option = target_class_option(derive_model_class)
62
+
63
+ @target_class_names ||= if option.is_a?(Array)
64
+ option.map(&:to_s)
65
+ elsif option
66
+ [option.to_s]
67
+ end
68
+ end
69
+
70
+ def target_classes
71
+ ClassArguments.constantize_argument(target_class_names)
72
+ end
73
+
74
+ def target_classes_or_nil
75
+ @target_classes_or_nil ||= discovered_model if target_class_names
76
+ end
77
+
78
+ def target_where_clause
79
+ return if model_class == false
80
+
81
+ Array.new(target_classes).map do |target_class|
82
+ "#{name}:`#{target_class.mapped_label_name}`"
83
+ end.join(' OR ')
84
+ end
85
+
86
+ def discovered_model
87
+ target_classes.select do |constant|
88
+ constant.ancestors.include?(::Neo4j::ActiveNode)
89
+ end
90
+ end
91
+
92
+ def target_class
93
+ return @target_class if @target_class
94
+
95
+ return if !(target_class_names && target_class_names.size == 1)
96
+
97
+ class_const = ClassArguments.constantize_argument(target_class_names[0])
98
+
99
+ @target_class = class_const
100
+ end
101
+
102
+ def callback(type)
103
+ @callbacks[type]
104
+ end
105
+
106
+ def perform_callback(caller, other_node, type)
107
+ return if callback(type).nil?
108
+ caller.send(callback(type), other_node)
109
+ end
110
+
111
+ def relationship_type(create = false)
112
+ case
113
+ when relationship_class
114
+ relationship_class_type
115
+ when !@relationship_type.nil?
116
+ @relationship_type
117
+ when @origin
118
+ origin_type
119
+ else
120
+ (create || exceptional_target_class?) && decorated_rel_type(@name)
121
+ end
122
+ end
123
+
124
+ attr_reader :relationship_class_name
125
+
126
+ def relationship_class_type
127
+ relationship_class._type.to_sym
128
+ end
129
+
130
+ def relationship_class
131
+ @relationship_class ||= @relationship_class_name && @relationship_class_name.constantize
132
+ end
133
+
134
+ def unique?
135
+ return relationship_class.unique? if rel_class?
136
+ @origin ? origin_association.unique? : !!@unique
137
+ end
138
+
139
+ def creates_unique_option
140
+ @unique || :none
141
+ end
142
+
143
+ def create_method
144
+ unique? ? :create_unique : :create
145
+ end
146
+
147
+ def _create_relationship(start_object, node_or_nodes, properties)
148
+ RelFactory.create(start_object, node_or_nodes, properties, self)
149
+ end
150
+
151
+ def relationship_class?
152
+ !!relationship_class
153
+ end
154
+ alias_method :rel_class?, :relationship_class?
155
+
156
+ private
157
+
158
+ def association_model_namespace
159
+ Neo4j::Config.association_model_namespace_string
160
+ end
161
+
162
+ def get_direction(create, reverse = false)
163
+ dir = (create && @direction == :both) ? :out : @direction
164
+ if reverse
165
+ case dir
166
+ when :in then :out
167
+ when :out then :in
168
+ else :both
169
+ end
170
+ else
171
+ dir
172
+ end
173
+ end
174
+
175
+ def origin_association
176
+ target_class.associations[@origin]
177
+ end
178
+
179
+ def origin_type
180
+ origin_association.relationship_type
181
+ end
182
+
183
+ private
184
+
185
+ def apply_vars_from_options(options)
186
+ @relationship_class_name = options[:rel_class] && options[:rel_class].to_s
187
+ @relationship_type = options[:type] && options[:type].to_sym
188
+
189
+ @model_class = options[:model_class]
190
+ @callbacks = {before: options[:before], after: options[:after]}
191
+ @origin = options[:origin] && options[:origin].to_sym
192
+ @dependent = options[:dependent].try(:to_sym)
193
+ @unique = options[:unique]
194
+ end
195
+
196
+ # Return basic details about association as declared in the model
197
+ # @example
198
+ # has_many :in, :bands, type: :has_band
199
+ def base_declaration
200
+ "#{type} #{direction.inspect}, #{name.inspect}"
201
+ end
202
+
203
+ def validate_init_arguments(type, direction, name, options)
204
+ validate_association_options!(name, options)
205
+ validate_option_combinations(options)
206
+ validate_dependent(options[:dependent].try(:to_sym))
207
+ check_valid_type_and_dir(type, direction)
208
+ end
209
+
210
+ VALID_ASSOCIATION_OPTION_KEYS = [:type, :origin, :model_class, :rel_class, :dependent, :before, :after, :unique]
211
+
212
+ def validate_association_options!(_association_name, options)
213
+ ClassArguments.validate_argument!(options[:model_class], 'model_class')
214
+ ClassArguments.validate_argument!(options[:rel_class], 'rel_class')
215
+
216
+ message = case
217
+ when (message = type_keys_error_message(options.keys))
218
+ message
219
+ when (unknown_keys = options.keys - VALID_ASSOCIATION_OPTION_KEYS).size > 0
220
+ "Unknown option(s) specified: #{unknown_keys.join(', ')}"
221
+ end
222
+
223
+ fail ArgumentError, message if message
224
+ end
225
+
226
+ def type_keys_error_message(keys)
227
+ type_keys = (keys & [:type, :origin, :rel_class])
228
+ if type_keys.size > 1
229
+ "Only one of 'type', 'origin', or 'rel_class' options are allowed for associations"
230
+ elsif type_keys.empty?
231
+ "The 'type' option must be specified( even if it is `nil`) or `origin`/`rel_class` must be specified"
232
+ end
233
+ end
234
+
235
+ def check_valid_type_and_dir(type, direction)
236
+ fail ArgumentError, "Invalid association type: #{type.inspect} (valid value: :has_many and :has_one)" if ![:has_many, :has_one].include?(type.to_sym)
237
+ fail ArgumentError, "Invalid direction: #{direction.inspect} (valid value: :out, :in, and :both)" if ![:out, :in, :both].include?(direction.to_sym)
238
+ end
239
+
240
+ def validate_option_combinations(options)
241
+ [[:type, :origin],
242
+ [:type, :rel_class],
243
+ [:origin, :rel_class]].each do |key1, key2|
244
+ if options[key1] && options[key2]
245
+ fail ArgumentError, "Cannot specify both :#{key1} and :#{key2} (#{base_declaration})"
246
+ end
247
+ end
248
+ end
249
+
250
+ # Determine if model class as derived from the association name would be different than the one specified via the model_class key
251
+ # @example
252
+ # has_many :friends # Would return false
253
+ # has_many :friends, model_class: Friend # Would return false
254
+ # has_many :friends, model_class: Person # Would return true
255
+ def exceptional_target_class?
256
+ # TODO: Exceptional if target_class.nil?? (when model_class false)
257
+
258
+ target_class && target_class.name != @target_class_name_from_name
259
+ end
260
+
261
+ def validate_origin!
262
+ return if not @origin
263
+
264
+ association = origin_association
265
+
266
+ message = case
267
+ when !target_class
268
+ 'Cannot use :origin without a model_class (implied or explicit)'
269
+ when !association
270
+ "Origin `#{@origin.inspect}` association not found for #{target_class} (specified in #{base_declaration})"
271
+ when @direction == association.direction
272
+ "Origin `#{@origin.inspect}` (specified in #{base_declaration}) has same direction `#{@direction}`)"
273
+ end
274
+
275
+ fail ArgumentError, message if message
276
+ end
277
+ end
278
+ end
279
+ end
280
+ end
@@ -0,0 +1,108 @@
1
+ module Neo4j
2
+ module ActiveNode
3
+ module HasN
4
+ module AssociationCypherMethods
5
+ # Return cypher partial query string for the relationship part of a MATCH (arrow / relationship definition)
6
+ def arrow_cypher(var = nil, properties = {}, create = false, reverse = false, length = nil)
7
+ validate_origin!
8
+
9
+ if create && length.present?
10
+ fail(ArgumentError, 'rel_length option cannot be specified when creating a relationship')
11
+ end
12
+
13
+ direction_cypher(get_relationship_cypher(var, properties, create, length), create, reverse)
14
+ end
15
+
16
+ private
17
+
18
+ def direction_cypher(relationship_cypher, create, reverse = false)
19
+ case get_direction(create, reverse)
20
+ when :out
21
+ "-#{relationship_cypher}->"
22
+ when :in
23
+ "<-#{relationship_cypher}-"
24
+ when :both
25
+ "-#{relationship_cypher}-"
26
+ end
27
+ end
28
+
29
+ def get_relationship_cypher(var, properties, create, length)
30
+ relationship_type = relationship_type(create)
31
+ relationship_name_cypher = ":`#{relationship_type}`" if relationship_type
32
+ rel_length_cypher = cypher_for_rel_length(length)
33
+ properties_string = get_properties_string(properties)
34
+
35
+ "[#{var}#{relationship_name_cypher}#{rel_length_cypher}#{properties_string}]"
36
+ end
37
+
38
+ def get_properties_string(properties)
39
+ p = properties.map do |key, value|
40
+ "#{key}: #{value.inspect}"
41
+ end.join(', ')
42
+ p.size == 0 ? '' : " {#{p}}"
43
+ end
44
+
45
+ VALID_REL_LENGTH_SYMBOLS = {
46
+ any: '*'
47
+ }
48
+
49
+ def cypher_for_rel_length(length)
50
+ return nil if length.blank?
51
+
52
+ validate_rel_length!(length)
53
+
54
+ case length
55
+ when Symbol then VALID_REL_LENGTH_SYMBOLS[length]
56
+ when Fixnum then "*#{length}"
57
+ when Range then cypher_for_range_rel_length(length)
58
+ when Hash then cypher_for_hash_rel_length(length)
59
+ end
60
+ end
61
+
62
+ def cypher_for_range_rel_length(length)
63
+ range_end = length.end
64
+ range_end = nil if range_end == Float::INFINITY
65
+ "*#{length.begin}..#{range_end}"
66
+ end
67
+
68
+ def cypher_for_hash_rel_length(length)
69
+ range_end = length[:max]
70
+ range_end = nil if range_end == Float::INFINITY
71
+ "*#{length[:min]}..#{range_end}"
72
+ end
73
+
74
+ def validate_rel_length!(length)
75
+ message = rel_length_error_message(length)
76
+ fail(ArgumentError, "Invalid value for rel_length (#{length.inspect}): #{message}") if message
77
+ true
78
+ end
79
+
80
+ def rel_length_error_message(length)
81
+ case length
82
+ when Fixnum then 'cannot be negative' if length < 0
83
+ when Symbol then rel_length_symbol_error_message(length)
84
+ when Range then rel_length_range_error_message(length)
85
+ when Hash then rel_length_hash_error_message(length)
86
+ else 'should be a Symbol, Fixnum, Range or Hash'
87
+ end
88
+ end
89
+
90
+ def rel_length_symbol_error_message(length)
91
+ "expecting one of #{VALID_REL_LENGTH_SYMBOLS.keys.inspect}" if !VALID_REL_LENGTH_SYMBOLS.key?(length)
92
+ end
93
+
94
+ def rel_length_range_error_message(length)
95
+ if length.begin > length.end
96
+ 'cannot be a decreasing Range'
97
+ elsif length.begin < 0
98
+ 'cannot include negative values'
99
+ end
100
+ end
101
+
102
+ def rel_length_hash_error_message(length)
103
+ 'Hash keys should be a subset of [:min, :max]' if (length.keys & [:min, :max]) != length.keys
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end