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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/aggregate/pipeline.rb +8 -9
- data/lib/aggregate/stages/base.rb +9 -1
- data/lib/aggregate/stages/facet.rb +2 -6
- data/lib/aggregate/stages/group.rb +2 -6
- data/lib/aggregate/stages/hash_base.rb +1 -1
- data/lib/aggregate/stages/lookup.rb +2 -6
- data/lib/aggregate/stages/match.rb +2 -6
- data/lib/aggregate/stages/project.rb +2 -6
- data/lib/aggregate/stages/replace_root.rb +2 -6
- data/lib/aggregate/stages/unwind.rb +2 -6
- data/lib/aggregate/values/array.rb +2 -2
- data/lib/aggregate/values/base.rb +5 -1
- data/lib/aggregate/values/document_class.rb +2 -6
- data/lib/aggregate/values/hash.rb +2 -6
- data/lib/aggregate/values/{boolean.rb → pipeline.rb} +5 -8
- data/lib/aggregate/values/symbol.rb +2 -6
- data/mongo_aggregation_dsl.gemspec +1 -1
- metadata +2 -5
- data/lib/aggregate/values/nil.rb +0 -25
- data/lib/aggregate/values/object_id.rb +0 -24
- data/lib/aggregate/values/string.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0bfb829de9382240792a0899fc54f54dad1bfcc
|
4
|
+
data.tar.gz: ded7e2f31ee34100a8e0eaf6ea7acb8ec04d6b00
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9347a316bf8cbe67e159f501018d5147093ebd4bccd778680bcd7b49a65836de17ddb31100e76238b54cef4934eb0672217e3ac0220fff3cd55ea82633745d4a
|
7
|
+
data.tar.gz: f90f48e5e50f24d890b0d45a76f5a92071fccd7d47e06ebf82f1e6d8fb3f33c1f4b5c084fbcb4b9d58e096348446e8af11799e1c143309f1bc9dfb58c282def7
|
data/Gemfile.lock
CHANGED
data/lib/aggregate/pipeline.rb
CHANGED
@@ -7,28 +7,27 @@ module Aggregate
|
|
7
7
|
attr_reader :stages
|
8
8
|
|
9
9
|
def initialize(klass = nil)
|
10
|
-
@klass
|
10
|
+
@klass = klass
|
11
11
|
@stages = []
|
12
12
|
end
|
13
13
|
|
14
14
|
def to_s
|
15
|
-
|
15
|
+
transpose
|
16
16
|
end
|
17
17
|
|
18
18
|
def inspect
|
19
|
-
|
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
|
-
|
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
|
@@ -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
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
def inspect
|
13
|
-
"{ $project: #{options} }"
|
8
|
+
def transpose
|
9
|
+
{ '$project': options }
|
14
10
|
end
|
15
11
|
end
|
16
12
|
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
|
7
|
+
def transpose
|
8
8
|
raise ArgumentError, "Array cannot be a hash key" if is_key
|
9
9
|
|
10
|
-
|
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
|
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
|
-
|
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
|
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
|
-
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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? ::
|
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
|
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
|
-
|
38
|
+
is_key ? retval : retval.to_s
|
43
39
|
end
|
44
40
|
|
45
41
|
class << self
|
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.
|
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/
|
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
|
data/lib/aggregate/values/nil.rb
DELETED
@@ -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
|