graphoid 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +46 -32
  3. data/Rakefile +2 -6
  4. data/lib/graphoid/argument.rb +3 -0
  5. data/lib/graphoid/config.rb +2 -0
  6. data/lib/graphoid/definitions/filters.rb +7 -8
  7. data/lib/graphoid/definitions/inputs.rb +8 -6
  8. data/lib/graphoid/definitions/orders.rb +5 -5
  9. data/lib/graphoid/definitions/types.rb +46 -40
  10. data/lib/graphoid/drivers/active_record.rb +37 -37
  11. data/lib/graphoid/drivers/mongoid.rb +75 -53
  12. data/lib/graphoid/graphield.rb +6 -6
  13. data/lib/graphoid/grapho.rb +2 -0
  14. data/lib/graphoid/main.rb +6 -4
  15. data/lib/graphoid/mapper.rb +5 -2
  16. data/lib/graphoid/mutations/create.rb +12 -14
  17. data/lib/graphoid/mutations/delete.rb +12 -14
  18. data/lib/graphoid/mutations/processor.rb +3 -1
  19. data/lib/graphoid/mutations/structure.rb +2 -0
  20. data/lib/graphoid/mutations/update.rb +2 -0
  21. data/lib/graphoid/operators/attribute.rb +6 -5
  22. data/lib/graphoid/operators/inherited/belongs_to.rb +10 -8
  23. data/lib/graphoid/operators/inherited/embeds_many.rb +17 -3
  24. data/lib/graphoid/operators/inherited/embeds_one.rb +17 -3
  25. data/lib/graphoid/operators/inherited/has_many.rb +4 -2
  26. data/lib/graphoid/operators/inherited/has_one.rb +9 -7
  27. data/lib/graphoid/operators/inherited/many_to_many.rb +4 -2
  28. data/lib/graphoid/operators/relation.rb +8 -9
  29. data/lib/graphoid/queries/operation.rb +2 -0
  30. data/lib/graphoid/queries/processor.rb +7 -5
  31. data/lib/graphoid/queries/queries.rb +16 -17
  32. data/lib/graphoid/scalars.rb +28 -26
  33. data/lib/graphoid/utils.rb +6 -4
  34. data/lib/graphoid/version.rb +3 -1
  35. data/lib/graphoid.rb +2 -0
  36. metadata +15 -28
@@ -1,68 +1,95 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module MongoidDriver
3
5
  class << self
4
- def through?(type)
6
+ def through?(_type)
5
7
  false
6
8
  end
7
9
 
10
+ def mongo_constants
11
+ begin
12
+ {
13
+ many_to_many: Mongoid::Relations::Referenced::ManyToMany,
14
+ has_many: Mongoid::Relations::Referenced::Many,
15
+ belongs_to: Mongoid::Relations::Referenced::In,
16
+ has_one: Mongoid::Relations::Referenced::One,
17
+ embeds_one: Mongoid::Relations::Embedded::One,
18
+ embeds_many: Mongoid::Relations::Embedded::Many,
19
+ embedded_in: Mongoid::Relations::Embedded::In
20
+ }
21
+ rescue
22
+ {
23
+ many_to_many: Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy,
24
+ has_many: Mongoid::Association::Referenced::HasMany::Proxy,
25
+ belongs_to: Mongoid::Association::Referenced::BelongsTo::Proxy,
26
+ has_one: Mongoid::Association::Referenced::HasOne::Proxy,
27
+ embeds_one: Mongoid::Association::Embedded::EmbedsOne::Proxy,
28
+ embeds_many: Mongoid::Association::Embedded::EmbedsMany::Proxy,
29
+ embedded_in: Mongoid::Association::Embedded::EmbeddedIn::Proxy
30
+ }
31
+ end
32
+ end
33
+
8
34
  def has_and_belongs_to_many?(type)
9
- type == Mongoid::Association::Referenced::HasAndBelongsToMany
35
+ type == mongo_constants[:many_to_many]
10
36
  end
11
37
 
12
38
  def has_many?(type)
13
- type == Mongoid::Association::Referenced::HasMany
39
+ type == mongo_constants[:has_many]
14
40
  end
15
41
 
16
42
  def belongs_to?(type)
17
- type == Mongoid::Association::Referenced::BelongsTo
43
+ type == mongo_constants[:belongs_to]
44
+
18
45
  end
19
46
 
20
47
  def has_one?(type)
21
- type == Mongoid::Association::Referenced::HasOne
48
+ type == mongo_constants[:has_one]
22
49
  end
23
50
 
24
51
  def embeds_one?(type)
25
- type == Mongoid::Association::Embedded::EmbedsOne
52
+ type == mongo_constants[:embeds_one]
26
53
  end
27
54
 
28
55
  def embeds_many?(type)
29
- type == Mongoid::Association::Embedded::EmbedsMany
56
+ type == mongo_constants[:embeds_many]
30
57
  end
31
58
 
32
59
  def embedded_in?(type)
33
- type == Mongoid::Association::Embedded::EmbeddedIn
60
+ type == mongo_constants[:embedded_in]
34
61
  end
35
62
 
36
63
  def types_map
37
64
  {
38
- BSON::ObjectId => GraphQL::Types::ID,
65
+ BSON::ObjectId => GraphQL::Types::ID,
39
66
  Mongoid::Boolean => GraphQL::Types::Boolean,
40
- #Graphoid::Upload => ApolloUploadServer::Upload,
67
+ # Graphoid::Upload => ApolloUploadServer::Upload,
41
68
 
42
- Boolean => GraphQL::Types::Boolean,
43
- Float => GraphQL::Types::Float,
44
- Integer => GraphQL::Types::Int,
45
- String => GraphQL::Types::String,
46
- Object => GraphQL::Types::String,
47
- Symbol => GraphQL::Types::String,
69
+ Boolean => GraphQL::Types::Boolean,
70
+ Float => GraphQL::Types::Float,
71
+ Integer => GraphQL::Types::Int,
72
+ String => GraphQL::Types::String,
73
+ Object => GraphQL::Types::String,
74
+ Symbol => GraphQL::Types::String,
48
75
 
49
76
  DateTime => Graphoid::Scalars::DateTime,
50
- Time => Graphoid::Scalars::DateTime,
51
- Date => Graphoid::Scalars::DateTime,
52
- Array => Graphoid::Scalars::Array,
53
- Hash => Graphoid::Scalars::Hash
77
+ Time => Graphoid::Scalars::DateTime,
78
+ Date => Graphoid::Scalars::DateTime,
79
+ Array => Graphoid::Scalars::Array,
80
+ Hash => Graphoid::Scalars::Hash
54
81
  }
55
82
  end
56
83
 
57
84
  def class_of(relation)
58
85
  {
59
- Mongoid::Association::Referenced::HasAndBelongsToMany => ManyToMany,
60
- Mongoid::Association::Referenced::HasMany => HasMany,
61
- Mongoid::Association::Referenced::HasOne => HasOne,
62
- Mongoid::Association::Referenced::BelongsTo => BelongsTo,
63
- Mongoid::Association::Embedded::EmbedsMany => EmbedsMany,
64
- Mongoid::Association::Embedded::EmbedsOne => EmbedsOne,
65
- Mongoid::Association::Embedded::EmbeddedIn => Relation
86
+ mongo_constants[:many_to_many] => ManyToMany,
87
+ mongo_constants[:has_many] => HasMany,
88
+ mongo_constants[:has_one] => HasOne,
89
+ mongo_constants[:belongs_to] => BelongsTo,
90
+ mongo_constants[:embeds_many] => EmbedsMany,
91
+ mongo_constants[:embeds_one] => EmbedsOne,
92
+ mongo_constants[:embedded_in] => Relation
66
93
  }[relation.relation] || Relation
67
94
  end
68
95
 
@@ -88,10 +115,10 @@ module Graphoid
88
115
 
89
116
  def eager_load(selection, model)
90
117
  referenced_relations = [
91
- Mongoid::Association::Referenced::HasAndBelongsToMany,
92
- Mongoid::Association::Referenced::HasMany,
93
- Mongoid::Association::Referenced::HasOne,
94
- Mongoid::Association::Referenced::BelongsTo
118
+ mongo_constants[:many_to_many],
119
+ mongo_constants[:has_many],
120
+ mongo_constants[:has_one],
121
+ mongo_constants[:belongs_to]
95
122
  ]
96
123
 
97
124
  properties = Graphoid::Queries::Processor.children_of(selection)
@@ -108,7 +135,7 @@ module Graphoid
108
135
  if (relations & children).empty?
109
136
  model = model.includes(name)
110
137
  else
111
- model = model.includes(name, with: -> (instance) { Graphoid::Queries::Processor.eager_load(subselection, instance) })
138
+ model = model.includes(name, with: ->(instance) { Graphoid::Queries::Processor.eager_load(subselection, instance) })
112
139
  end
113
140
  end
114
141
 
@@ -126,17 +153,18 @@ module Graphoid
126
153
  scope.any_of(list)
127
154
  end
128
155
 
129
- def parse(attribute, value, operator)
156
+ def parse(attribute, value, operator, prefix = nil)
130
157
  field = attribute.name
158
+ field = "#{prefix}.#{field}" if prefix
131
159
  parsed = {}
132
160
  case operator
133
- when "gt", "gte", "lt", "lte", "in", "nin"
161
+ when 'gt', 'gte', 'lt', 'lte', 'in', 'nin'
134
162
  parsed[field.to_sym.send(operator)] = value
135
- when "regex"
163
+ when 'regex'
136
164
  parsed[field.to_sym] = Regexp.new(value.to_s, Regexp::IGNORECASE)
137
- when "contains"
165
+ when 'contains'
138
166
  parsed[field.to_sym] = Regexp.new(Regexp.quote(value.to_s), Regexp::IGNORECASE)
139
- when "not"
167
+ when 'not'
140
168
  if value.present? && !value.is_a?(Numeric)
141
169
  parsed[field.to_sym.send(operator)] = Regexp.new(Regexp.quote(value.to_s), Regexp::IGNORECASE)
142
170
  else
@@ -167,17 +195,11 @@ module Graphoid
167
195
  field = relation.name
168
196
  parsed = {}
169
197
 
170
- if relation.embeds_one?
171
- parsed = relate_embedded(scope, relation, value)
172
- end
198
+ parsed = relate_embedded(scope, relation, value) if relation.embeds_one?
173
199
 
174
- if relation.belongs_to?
175
- parsed = relation.exec(scope, value)
176
- end
200
+ parsed = relation.exec(scope, value) if relation.belongs_to?
177
201
 
178
- if relation.has_one?
179
- parsed = relation.exec(scope, value)
180
- end
202
+ parsed = relation.exec(scope, value) if relation.has_one?
181
203
 
182
204
  parsed
183
205
  end
@@ -191,20 +213,20 @@ module Graphoid
191
213
  end
192
214
 
193
215
  if relation.many_to_many?
194
- field_name = field_name.to_s.singularize + "_ids"
195
- ids = target.map(&(field_name.to_sym))
216
+ field_name = field_name.to_s.singularize + '_ids'
217
+ ids = target.map(&field_name.to_sym)
196
218
  ids.flatten!.uniq!
197
219
  else
198
- field_name = field_name.to_s + "_id"
199
- ids = target.map(&(field_name.to_sym))
220
+ field_name = field_name.to_s + '_id'
221
+ ids = target.map(&field_name.to_sym)
200
222
  end
201
223
 
202
224
  parsed = {}
203
- if operator == "none"
225
+ if operator == 'none'
204
226
  parsed[:id.nin] = ids
205
- elsif operator == "some"
227
+ elsif operator == 'some'
206
228
  parsed[:id.in] = ids
207
- elsif operator == "every"
229
+ elsif operator == 'every'
208
230
  # missing implementation
209
231
  end
210
232
  parsed
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module Graphield
3
5
  extend ActiveSupport::Concern
@@ -7,20 +9,18 @@ module Graphoid
7
9
  @forbidden = {}
8
10
 
9
11
  class << self
10
- def graphield name, type
12
+ def graphield(name, type)
11
13
  @graphields << Graphoid::Attribute.new(name: name.to_s, type: type)
12
14
  end
13
15
 
14
- def graphorbid field, *actions
16
+ def graphorbid(field, *actions)
15
17
  @forbidden[field] = actions
16
18
  end
17
19
 
18
- def graphields
19
- @graphields
20
- end
20
+ attr_reader :graphields
21
21
 
22
22
  def graphfiles
23
- @graphields.select{ |field| field.type == Graphoid::Upload }
23
+ @graphields.select { |field| field.type == Graphoid::Upload }
24
24
  end
25
25
 
26
26
  def forbidden_fields
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class Grapho
3
5
  attr_reader :name, :plural, :camel_name
data/lib/graphoid/main.rb CHANGED
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
- @@graphs = {}
4
+ @graphs = {}
3
5
 
4
6
  class << self
5
- attr_accessor :driver
7
+ attr_reader :driver
6
8
 
7
9
  def initialize
8
10
  Graphoid.driver = configuration&.driver
@@ -10,8 +12,8 @@ module Graphoid
10
12
  Graphoid::Scalars.generate
11
13
  end
12
14
 
13
- def build(model, action = nil)
14
- @@graphs[model] ||= Graphoid::Grapho.new(model)
15
+ def build(model, _action = nil)
16
+ @graphs[model] ||= Graphoid::Grapho.new(model)
15
17
  end
16
18
 
17
19
  def driver=(driver)
@@ -1,8 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module Mapper
3
5
  class << self
4
- def convert field
5
- return GraphQL::Types::ID if field.name.end_with?("id")
6
+ def convert(field)
7
+ return GraphQL::Types::ID if field.name.end_with?('id')
8
+
6
9
  Graphoid.driver.types_map[field.type] || GraphQL::Types::String
7
10
  end
8
11
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module Mutations
3
5
  module Create
@@ -22,25 +24,21 @@ module Graphoid
22
24
 
23
25
  type.class_eval do
24
26
  define_method :"#{name}" do |data: {}|
25
- begin
26
- user = context[:current_user]
27
- Graphoid::Mutations::Processor.execute(model, grapho, data, user)
28
- rescue Exception => ex
29
- GraphQL::ExecutionError.new(ex.message)
30
- end
27
+ user = context[:current_user]
28
+ Graphoid::Mutations::Processor.execute(model, grapho, data, user)
29
+ rescue Exception => ex
30
+ GraphQL::ExecutionError.new(ex.message)
31
31
  end
32
32
  end
33
33
 
34
34
  type.class_eval do
35
35
  define_method :"#{plural_name}" do |data: []|
36
- begin
37
- user = context[:current_user]
38
- result = []
39
- data.each { |d| result << Graphoid::Mutations::Processor.execute(model, grapho, d, user) }
40
- result
41
- rescue Exception => ex
42
- GraphQL::ExecutionError.new(ex.message)
43
- end
36
+ user = context[:current_user]
37
+ result = []
38
+ data.each { |d| result << Graphoid::Mutations::Processor.execute(model, grapho, d, user) }
39
+ result
40
+ rescue Exception => ex
41
+ GraphQL::ExecutionError.new(ex.message)
44
42
  end
45
43
  end
46
44
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module Mutations
3
5
  module Delete
@@ -22,25 +24,21 @@ module Graphoid
22
24
 
23
25
  type.class_eval do
24
26
  define_method :"#{name}" do |id:|
25
- begin
26
- result = model.find(id)
27
- result.destroy!
28
- result
29
- rescue Exception => ex
30
- GraphQL::ExecutionError.new(ex.message)
31
- end
27
+ result = model.find(id)
28
+ result.destroy!
29
+ result
30
+ rescue Exception => ex
31
+ GraphQL::ExecutionError.new(ex.message)
32
32
  end
33
33
  end
34
34
 
35
35
  type.class_eval do
36
36
  define_method :"#{plural}" do |where: {}|
37
- begin
38
- objects = Graphoid::Queries::Processor.execute(model, where.to_h)
39
- objects.destroy_all
40
- objects.all.to_a
41
- rescue Exception => ex
42
- GraphQL::ExecutionError.new(ex.message)
43
- end
37
+ objects = Graphoid::Queries::Processor.execute(model, where.to_h)
38
+ objects.destroy_all
39
+ objects.all.to_a
40
+ rescue Exception => ex
41
+ GraphQL::ExecutionError.new(ex.message)
44
42
  end
45
43
  end
46
44
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module Mutations
3
5
  module Processor
@@ -11,7 +13,7 @@ module Graphoid
11
13
  root_object << item if item.present?
12
14
  end
13
15
 
14
- root_object = root_object.reduce(Hash.new, :merge)
16
+ root_object = root_object.reduce({}, :merge)
15
17
 
16
18
  fieldnames = Attribute.fieldnames_of(model)
17
19
  root_object['created_by_id'] = user.id if fieldnames.include?('created_by_id')
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module Mutations
3
5
  extend ActiveSupport::Concern
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module Mutations
3
5
  module Update
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class Attribute
3
5
  attr_reader :name, :type
4
6
 
5
- PERMS = [:read, :create, :update, :delete]
7
+ PERMS = %i[read create update delete]
6
8
 
7
9
  def initialize(name:, type:)
8
10
  @name = name.to_s
@@ -21,13 +23,12 @@ module Graphoid
21
23
  def relation?
22
24
  false
23
25
  end
24
-
26
+
25
27
  def precreate(value)
26
- { self.name.to_sym => value }
28
+ { name.to_sym => value }
27
29
  end
28
30
 
29
- def create(_,_,_)
30
- end
31
+ def create(_, _, _); end
31
32
 
32
33
  class << self
33
34
  def fields_of(model, action = :read)
@@ -1,15 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class BelongsTo < Relation
3
5
  def precreate(value)
4
- sanitized = Attribute.correct(self.klass, value)
5
- foreign_id = self.klass.create!(sanitized).id
6
- { :"#{self.name}_id" => foreign_id }
6
+ sanitized = Attribute.correct(klass, value)
7
+ foreign_id = klass.create!(sanitized).id
8
+ { :"#{name}_id" => foreign_id }
7
9
  end
8
-
10
+
9
11
  def exec(_, value)
10
- ids = Graphoid::Queries::Processor.execute(self.klass, value).to_a.map(&:id)
11
- attribute = Attribute.new(name: "#{self.name.underscore}_id", type: nil)
12
- Graphoid.driver.parse(attribute, ids, "in")
12
+ ids = Graphoid::Queries::Processor.execute(klass, value).to_a.map(&:id)
13
+ attribute = Attribute.new(name: "#{name.underscore}_id", type: nil)
14
+ Graphoid.driver.parse(attribute, ids, 'in')
13
15
  end
14
16
  end
15
- end
17
+ end
@@ -1,10 +1,24 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class EmbedsMany < Relation
3
5
  def create(parent, values, _)
4
6
  values.each do |value|
5
- attributes = Attribute.correct(self.klass, value)
6
- parent.send(:"#{self.name}").create!(attributes)
7
+ attributes = Attribute.correct(klass, value)
8
+ parent.send(:"#{name}").create!(attributes)
7
9
  end
8
10
  end
11
+
12
+ def exec(_scope, value)
13
+ _hash = {}
14
+
15
+ value.each do |key, _value|
16
+ operation = Operation.new(klass, key, _value)
17
+ parsed = Graphoid.driver.parse(operation.operand, operation.value, operation.operator, klass.to_s.underscore.pluralize)
18
+ _hash.merge!(parsed)
19
+ end
20
+
21
+ _hash
22
+ end
9
23
  end
10
- end
24
+ end
@@ -1,8 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class EmbedsOne < Relation
3
5
  def create(parent, value, _)
4
- attrs = Attribute.correct(self.klass, value)
5
- parent.send(:"#{self.name}=", attrs)
6
+ attrs = Attribute.correct(klass, value)
7
+ parent.send(:"#{name}=", attrs)
8
+ end
9
+
10
+ def exec(_scope, value)
11
+ _hash = {}
12
+
13
+ value.each do |key, _value|
14
+ operation = Operation.new(klass, key, _value)
15
+ parsed = Graphoid.driver.parse(operation.operand, operation.value, operation.operator, klass.to_s.underscore)
16
+ _hash.merge!(parsed)
17
+ end
18
+
19
+ _hash
6
20
  end
7
21
  end
8
- end
22
+ end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class HasMany < Relation
3
5
  def create(parent, values, grapho)
4
6
  values.each do |value|
5
- attributes = Attribute.correct(self.klass, value)
7
+ attributes = Attribute.correct(klass, value)
6
8
  attributes[:"#{grapho.name}_id"] = parent.id
7
- self.klass.create!(attributes)
9
+ klass.create!(attributes)
8
10
  end
9
11
  end
10
12
  end
@@ -1,16 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class HasOne < Relation
3
5
  def create(parent, value, grapho)
4
- attributes = Attribute.correct(self.klass, value)
6
+ attributes = Attribute.correct(klass, value)
5
7
  attributes[:"#{grapho.name}_id"] = parent.id
6
- self.klass.create!(attributes)
8
+ klass.create!(attributes)
7
9
  end
8
-
10
+
9
11
  def exec(scope, value)
10
- field_name = self.inverse_name || scope.name.underscore
11
- ids = Graphoid::Queries::Processor.execute(self.klass, value).to_a.map(&("#{field_name}_id".to_sym))
12
- attribute = Attribute.new(name: "id", type: nil)
13
- Graphoid.driver.parse(attribute, ids, "in")
12
+ field_name = inverse_name || scope.name.underscore
13
+ ids = Graphoid::Queries::Processor.execute(klass, value).to_a.map(&"#{field_name}_id".to_sym)
14
+ attribute = Attribute.new(name: 'id', type: nil)
15
+ Graphoid.driver.parse(attribute, ids, 'in')
14
16
  end
15
17
  end
16
18
  end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class ManyToMany < Relation
3
5
  def create(parent, values, grapho)
4
6
  values.each do |value|
5
- attributes = Attribute.correct(self.klass, value)
7
+ attributes = Attribute.correct(klass, value)
6
8
  attributes[:"#{grapho.name}_ids"] = [parent.id]
7
- self.klass.create!(attributes)
9
+ klass.create!(attributes)
8
10
  end
9
11
  end
10
12
  end
@@ -1,5 +1,6 @@
1
- module Graphoid
1
+ # frozen_string_literal: true
2
2
 
3
+ module Graphoid
3
4
  class Relation
4
5
  attr_reader :name, :klass, :type, :inverse_name
5
6
 
@@ -11,7 +12,7 @@ module Graphoid
11
12
  @type = Graphoid.driver.relation_type(relation)
12
13
  end
13
14
 
14
- [:has_and_belongs_to_many, :through, :has_many, :belongs_to, :has_one, :embeds_one, :embeds_many, :embedded_in].each do |type|
15
+ %i[has_and_belongs_to_many through has_many belongs_to has_one embeds_one embeds_many embedded_in].each do |type|
15
16
  type = :"#{type}?"
16
17
  define_method type do
17
18
  Graphoid.driver.send(type, @type)
@@ -43,19 +44,17 @@ module Graphoid
43
44
  embeds_one? || embeds_many? || embedded_in?
44
45
  end
45
46
 
46
- def precreate(_)
47
- end
48
-
49
- def create(_,_,_)
50
- end
47
+ def precreate(_); end
48
+
49
+ def create(_, _, _); end
51
50
 
52
51
  def resolve(operation)
53
- if one?
52
+ if one? || operation.operand.embedded?
54
53
  return operation.operand.exec(operation.scope, operation.value)
55
54
  end
56
55
 
57
56
  if many?
58
- return Graphoid.driver.relate_many(o.scope, o.operand, o.value, o.operator)
57
+ return Graphoid.driver.relate_many(operation.scope, operation.operand, operation.value, operation.operator)
59
58
  end
60
59
  end
61
60
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  class Operation
3
5
  attr_reader :scope, :operand, :operator, :value
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Graphoid
2
4
  module Queries
3
5
  module Processor
@@ -8,7 +10,7 @@ module Graphoid
8
10
  end
9
11
 
10
12
  def execute_array(scope, list, action)
11
- if action == "OR"
13
+ if action == 'OR'
12
14
  scope = Graphoid.driver.execute_or(scope, list)
13
15
  else
14
16
  list.each { |object| scope = execute(scope, object) }
@@ -17,7 +19,7 @@ module Graphoid
17
19
  end
18
20
 
19
21
  def process(scope, value, key = nil)
20
- if key && ["OR", "AND"].exclude?(key)
22
+ if key && %w[OR AND].exclude?(key)
21
23
  operation = Operation.new(scope, key, value)
22
24
  filter = operation.resolve
23
25
  return Graphoid.driver.execute_and(scope, filter)
@@ -25,12 +27,12 @@ module Graphoid
25
27
 
26
28
  if operation.nil? || operation.type == :attribute
27
29
  return execute(scope, value) if value.is_a?(Hash)
28
- if value.is_a?(Array) && ["in", "nin"].exclude?(operation&.operator)
29
- return execute_array(scope, value, key)
30
+ if value.is_a?(Array) && %w[in nin].exclude?(operation&.operator)
31
+ return execute_array(scope, value, key)
30
32
  end
31
33
  end
32
34
  end
33
-
35
+
34
36
  def children_of(selection)
35
37
  selection.scoped_children.values[0]
36
38
  end