mongo_aggregation_dsl 0.0.2.alpha → 0.0.3alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf04296272e988d1f33eb80b9c529c2d0c1debdc
4
- data.tar.gz: 309b6f158ed999d4f1c87d269102cbb1151bb800
3
+ metadata.gz: b0bfb829de9382240792a0899fc54f54dad1bfcc
4
+ data.tar.gz: ded7e2f31ee34100a8e0eaf6ea7acb8ec04d6b00
5
5
  SHA512:
6
- metadata.gz: 69bf0d84c9036935cebe0d1d1582c071bc8accd4b686f2f0ec8c44e120f8a89195d734653e9d427226326ecb488dce9ed660690b592108f1cdceebee05c40d5d
7
- data.tar.gz: c947143db1ac1d8cc94062df4029d03b01801417227463d89bd654134c4740c1c2bcb79da2810d7f0dcd41ae8d881fd13b20b7a9c7f2e654475078ff5a66a17c
6
+ metadata.gz: 9347a316bf8cbe67e159f501018d5147093ebd4bccd778680bcd7b49a65836de17ddb31100e76238b54cef4934eb0672217e3ac0220fff3cd55ea82633745d4a
7
+ data.tar.gz: f90f48e5e50f24d890b0d45a76f5a92071fccd7d47e06ebf82f1e6d8fb3f33c1f4b5c084fbcb4b9d58e096348446e8af11799e1c143309f1bc9dfb58c282def7
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongo_aggregation_dsl (0.0.2.alpha)
4
+ mongo_aggregation_dsl (0.0.3alpha)
5
5
  autoloaded (~> 2)
6
6
  contracts-lite
7
7
  mongo (~> 2.6.2)
@@ -7,28 +7,27 @@ module Aggregate
7
7
  attr_reader :stages
8
8
 
9
9
  def initialize(klass = nil)
10
- @klass = klass
10
+ @klass = klass
11
11
  @stages = []
12
12
  end
13
13
 
14
14
  def to_s
15
- inspect
15
+ transpose
16
16
  end
17
17
 
18
18
  def inspect
19
- @stages.to_s
19
+ transpose.to_s
20
+ end
21
+
22
+ def transpose
23
+ @stages.map(&:transpose)
20
24
  end
21
25
 
22
26
  # :reek:NilCheck
23
27
  def execute
24
28
  raise "Pipeline initializer must specify a class in order to be executable" if @klass.nil?
25
29
 
26
- db = Mongoid.default_client.database
27
- command = "function(){return db.#{@klass.collection_name}.aggregate(#{self}).toArray()}"
28
-
29
- # https://github.com/mongodb/mongo-ruby-driver/blob/master/lib/mongo/operation/result.rb
30
- result = db.command("$eval": command, nolock: true, await_data: true)
31
- result.documents
30
+ @klass.collection.aggregate(transpose)
32
31
  end
33
32
 
34
33
  Aggregate::Stages.constants.each do |klass|
@@ -13,7 +13,15 @@ module Aggregate
13
13
  # :reek:NilCheck
14
14
  def initialize(options)
15
15
  value_handler = self.class.value_handlers.detect { |handler| handler.handles?(options) }
16
- @options = value_handler.nil? ? options : value_handler.new(options, false)
16
+ @options = value_handler.nil? ? options : value_handler.new(options, false).transpose
17
+ end
18
+
19
+ def to_s
20
+ transpose.to_s
21
+ end
22
+
23
+ def inspect
24
+ transpose.to_s
17
25
  end
18
26
  end
19
27
  end
@@ -12,12 +12,8 @@ module Aggregate
12
12
  super(options)
13
13
  end
14
14
 
15
- def to_s
16
- inspect
17
- end
18
-
19
- def inspect
20
- "{ $facet: #{options} }"
15
+ def transpose
16
+ { '$facet': options }
21
17
  end
22
18
  end
23
19
  end
@@ -13,12 +13,8 @@ module Aggregate
13
13
  super(options)
14
14
  end
15
15
 
16
- def to_s
17
- inspect
18
- end
19
-
20
- def inspect
21
- "{ $group: #{options} }"
16
+ def transpose
17
+ { '$group': options }
22
18
  end
23
19
  end
24
20
  end
@@ -6,7 +6,7 @@ module Aggregate
6
6
  class HashBase < Base
7
7
  Contract C::HashMinLength[1] => Any
8
8
  def initialize(options)
9
- super(Aggregate::Values::Hash.new(options, false))
9
+ super(options)
10
10
  end
11
11
  end
12
12
  end
@@ -21,12 +21,8 @@ module Aggregate
21
21
  super(options)
22
22
  end
23
23
 
24
- def to_s
25
- inspect
26
- end
27
-
28
- def inspect
29
- "{ $lookup: #{options} }"
24
+ def transpose
25
+ { '$lookup': options }
30
26
  end
31
27
  end
32
28
  end
@@ -13,12 +13,8 @@ module Aggregate
13
13
  super(options)
14
14
  end
15
15
 
16
- def to_s
17
- inspect
18
- end
19
-
20
- def inspect
21
- "{ $match: #{options} }"
16
+ def transpose
17
+ { '$match': options }
22
18
  end
23
19
  end
24
20
  end
@@ -5,12 +5,8 @@ module Aggregate
5
5
  # Represents an aggregation project
6
6
  # https://docs.mongodb.com/manual/reference/operator/aggregation/project/#pipe._S_project
7
7
  class Project < HashBase
8
- def to_s
9
- inspect
10
- end
11
-
12
- def inspect
13
- "{ $project: #{options} }"
8
+ def transpose
9
+ { '$project': options }
14
10
  end
15
11
  end
16
12
  end
@@ -10,12 +10,8 @@ module Aggregate
10
10
  super(new_root)
11
11
  end
12
12
 
13
- def to_s
14
- inspect
15
- end
16
-
17
- def inspect
18
- "{ $replaceRoot: {newRoot: #{options}} }"
13
+ def transpose
14
+ { '$replaceRoot': { 'newRoot': options } }
19
15
  end
20
16
  end
21
17
  end
@@ -17,12 +17,8 @@ module Aggregate
17
17
  super(options)
18
18
  end
19
19
 
20
- def to_s
21
- inspect
22
- end
23
-
24
- def inspect
25
- "{ $unwind: #{options} }"
20
+ def transpose
21
+ { '$unwind': options }
26
22
  end
27
23
  end
28
24
  end
@@ -4,10 +4,10 @@ module Aggregate
4
4
  module Values
5
5
  # Takes an array and converts each to the appropriate value handler if one is available.
6
6
  class Array < Base
7
- def to_s
7
+ def transpose
8
8
  raise ArgumentError, "Array cannot be a hash key" if is_key
9
9
 
10
- "[#{value.map { |value| get_value(value, false) }.join(', ')}]"
10
+ value.map { |value| get_value(value, false) }
11
11
  end
12
12
 
13
13
  class << self
@@ -25,6 +25,10 @@ module Aggregate
25
25
  @is_key = is_key
26
26
  end
27
27
 
28
+ def to_s
29
+ transpose.to_s
30
+ end
31
+
28
32
  class << self
29
33
  def value_handlers
30
34
  @value_handlers ||= (Aggregate::Values.constants - [:Base]).map do |klass|
@@ -39,7 +43,7 @@ module Aggregate
39
43
 
40
44
  def get_value(original_value, is_hash_key)
41
45
  handler = self.class.value_handlers.detect { |handler| handler.handles?(original_value) }
42
- handler.nil? ? original_value : handler.new(original_value, is_hash_key)
46
+ handler.nil? ? original_value : handler.new(original_value, is_hash_key).transpose
43
47
  end
44
48
  end
45
49
  end
@@ -4,14 +4,10 @@ module Aggregate
4
4
  module Values
5
5
  # Takes a mongoid document and returns a string containing the document collection name
6
6
  class DocumentClass < Base
7
- def to_s
8
- inspect
9
- end
10
-
11
- def inspect
7
+ def transpose
12
8
  raise ArgumentError("Document cannot be a hash key") if is_key
13
9
 
14
- "'#{value.collection_name}'"
10
+ value.collection_name.to_s
15
11
  end
16
12
 
17
13
  class << self
@@ -4,14 +4,10 @@ module Aggregate
4
4
  module Values
5
5
  # Takes hash and converts each key and pair to the appropriate value handler if one is available.
6
6
  class Hash < Base
7
- def to_s
8
- inspect
9
- end
10
-
11
- def inspect
7
+ def transpose
12
8
  raise ArgumentError("Hash cannot be a hash key") if is_key
13
9
 
14
- "{ #{transpose_options.map { |hash_key, hash_value| "#{hash_key}: #{hash_value}" }.join(', ')} }"
10
+ transpose_options
15
11
  end
16
12
 
17
13
  private
@@ -3,18 +3,15 @@
3
3
  module Aggregate
4
4
  module Values
5
5
  # Returns a string wrapped in single quotes if the value or string if a key
6
- class Boolean < Base
7
- def to_s
8
- inspect
9
- end
10
-
11
- def inspect
12
- value.to_s
6
+ # Additionally adds $ to the reserved aggregation operations.
7
+ class Pipeline < Base
8
+ def transpose
9
+ @value.transpose
13
10
  end
14
11
 
15
12
  class << self
16
13
  def handles?(value)
17
- value.is_a? ::Boolean
14
+ value.is_a? ::Aggregate::Pipeline
18
15
  end
19
16
  end
20
17
  end
@@ -29,17 +29,13 @@ module Aggregate
29
29
  GROUP_ACCUMULATORS = %i[avg first last max min push addToSet].freeze
30
30
 
31
31
  PROJECT_ACCUMULATORS = %i[avg max min push stdDevPop stdDevSamp sum].freeze
32
- def to_s
33
- inspect
34
- end
35
-
36
- def inspect
32
+ def transpose
37
33
  is_operator = (%i[expr] + EXPRESSION_OPERATORS + GROUP_ACCUMULATORS + PROJECT_ACCUMULATORS).include?(value)
38
34
  retval = is_operator ? "$#{value}" : value
39
35
 
40
36
  retval = :_id if value == :id
41
37
 
42
- "'#{retval}'"
38
+ is_key ? retval : retval.to_s
43
39
  end
44
40
 
45
41
  class << self
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "mongo_aggregation_dsl"
5
- spec.version = "0.0.2.alpha"
5
+ spec.version = "0.0.3alpha"
6
6
  spec.authors = ["KrimsonKla"]
7
7
  spec.email = ["admin@cardtapp.com"]
8
8
  spec.date = "2018-09-10"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_aggregation_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.alpha
4
+ version: 0.0.3alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - KrimsonKla
@@ -350,12 +350,9 @@ files:
350
350
  - lib/aggregate/values.rb
351
351
  - lib/aggregate/values/array.rb
352
352
  - lib/aggregate/values/base.rb
353
- - lib/aggregate/values/boolean.rb
354
353
  - lib/aggregate/values/document_class.rb
355
354
  - lib/aggregate/values/hash.rb
356
- - lib/aggregate/values/nil.rb
357
- - lib/aggregate/values/object_id.rb
358
- - lib/aggregate/values/string.rb
355
+ - lib/aggregate/values/pipeline.rb
359
356
  - lib/aggregate/values/symbol.rb
360
357
  - lib/mongo_aggregation_dsl.rb
361
358
  - mongo_aggregation_dsl.gemspec
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Aggregate
4
- module Values
5
- # Converts nil to null
6
- # :reek:NilCheck
7
- class Nil < Base
8
- def to_s
9
- inspect
10
- end
11
-
12
- def inspect
13
- raise ArgumentError, "Nil cannot be a hash key" if is_key
14
-
15
- "null"
16
- end
17
-
18
- class << self
19
- def handles?(value)
20
- value.nil?
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,24 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Aggregate
4
- module Values
5
- # Converts a BSON::Object id to a string `ObjectId('123')`
6
- class ObjectId < Base
7
- def to_s
8
- inspect
9
- end
10
-
11
- def inspect
12
- raise ArgumentError, "ObjectId cannot be a hash key" if is_key
13
-
14
- "ObjectId('#{value}')"
15
- end
16
-
17
- class << self
18
- def handles?(value)
19
- value.is_a? BSON::ObjectId
20
- end
21
- end
22
- end
23
- end
24
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Aggregate
4
- module Values
5
- # Returns a string wrapped in single quotes if the value or string if a key
6
- class String < Base
7
- def to_s
8
- inspect
9
- end
10
-
11
- def inspect
12
- "'#{value}'"
13
- end
14
-
15
- class << self
16
- def handles?(value)
17
- value.is_a? ::String
18
- end
19
- end
20
- end
21
- end
22
- end