reductor 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +4 -1
- data/lib/reductor.rb +36 -9
- data/lib/reductor/converted_field.rb +31 -0
- data/lib/reductor/reductions.rb +2 -1
- data/lib/reductor/reductions/count.rb +40 -0
- data/lib/reductor/reductions/sum.rb +40 -0
- data/lib/reductor/simple_field.rb +13 -0
- data/reductor.gemspec +1 -1
- metadata +7 -2
- data/lib/reductor/reductions/counts_by_day.rb +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6a5a238cb24cc1f8de7500be93a91693486367ec
|
4
|
+
data.tar.gz: ddeb998004dd0be173e73a358d9b11428431cd28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32bc32f45554932f300f2893c701edc7ef1029d5e56bc3d49193577f8819fac8761ff1602a769348117ce826c897af56adc285f7e6d22bfc66a5b5ecf138f948
|
7
|
+
data.tar.gz: eb5b6999b97240ad15f65212506cf125c4cd83ec65fd6d1292a783c740d8b5c3c554ade963ecd10dd11fd0a60c642103c5ddea33493e106da8889b5cfae08704
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/README.md
CHANGED
@@ -5,7 +5,10 @@ Collection of common map_reduce operations, implemented with mongoid.
|
|
5
5
|
## Basic sample:
|
6
6
|
|
7
7
|
```
|
8
|
-
pry(main)>
|
8
|
+
pry(main)>Project.all.reduct :sum do
|
9
|
+
pry(main)> by :project_id, { created_at: :by_day }
|
10
|
+
pry(main)> value :duration
|
11
|
+
pry(main)>end
|
9
12
|
=> {
|
10
13
|
nil => 12.0,
|
11
14
|
2012-05-10 22:00:00 UTC => 1.0,
|
data/lib/reductor.rb
CHANGED
@@ -1,27 +1,54 @@
|
|
1
1
|
module Mongoid
|
2
2
|
class Criteria
|
3
3
|
|
4
|
-
def
|
5
|
-
reductor = Reductor.new(self, reduction_type,
|
6
|
-
Hash[reductor.
|
4
|
+
def reduct(reduction_type, &block)
|
5
|
+
reductor = Reductor.new(self, reduction_type, &block)
|
6
|
+
Hash[reductor.reduct.map{|e| [e['_id'], e["value"]] }]
|
7
7
|
end
|
8
8
|
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
require 'reductor/reductions'
|
13
|
+
require 'reductor/simple_field'
|
14
|
+
require 'reductor/converted_field'
|
13
15
|
|
14
16
|
class Reductor
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
attr_accessor :collection, :type, :block, :fields
|
19
|
+
|
20
|
+
def initialize(collection, type, &block)
|
21
|
+
self.collection = collection
|
22
|
+
self.type = type
|
23
|
+
self.block = block
|
24
|
+
end
|
25
|
+
|
26
|
+
def keys(*keys)
|
27
|
+
self.fields = keys.map do |f|
|
28
|
+
if f.is_a?(Hash)
|
29
|
+
f.map{ |k,v| Reductor::ConvertedField.new(k, v) }
|
30
|
+
else
|
31
|
+
Reductor::SimpleField.new(f)
|
32
|
+
end
|
33
|
+
end.flatten
|
20
34
|
end
|
21
35
|
|
22
|
-
def
|
36
|
+
def map
|
37
|
+
%Q{
|
38
|
+
function() {
|
39
|
+
#{emit}
|
40
|
+
}
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def reduct
|
23
45
|
self.extend reduction_module
|
24
|
-
|
46
|
+
instance_eval &block
|
47
|
+
@collection.map_reduce(map, reduce).finalize(finalize).out(@out || { inline: true })
|
48
|
+
end
|
49
|
+
|
50
|
+
def out arg
|
51
|
+
@out = arg
|
25
52
|
end
|
26
53
|
|
27
54
|
def reduction_module
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Reductor
|
2
|
+
class ConvertedField < Struct.new(:field_name, :translation_type)
|
3
|
+
|
4
|
+
def to_s
|
5
|
+
"#{field_name}: (#{translated})"
|
6
|
+
end
|
7
|
+
|
8
|
+
def translated
|
9
|
+
send(translation_type, field_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def to_hour(field_name)
|
15
|
+
"this.#{field_name} ? new Date(this.#{field_name}.getFullYear(), this.#{field_name}.getMonth(), this.#{field_name}.getDate(), this.#{field_name}.getHours()) : this.#{field_name}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_day(field_name)
|
19
|
+
"this.#{field_name} ? new Date(this.#{field_name}.getFullYear(), this.#{field_name}.getMonth(), this.#{field_name}.getDate()) : this.#{field_name}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_month(field_name)
|
23
|
+
"this.#{field_name} ? new Date(this.#{field_name}.getFullYear(), this.#{field_name}.getMonth()) : this.#{field_name}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_year(field_name)
|
27
|
+
"this.#{field_name} ? new Date(this.#{field_name}.getFullYear()) : this.#{field_name}"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/lib/reductor/reductions.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
class Reductor
|
2
|
+
module Reductions
|
3
|
+
module Count
|
4
|
+
|
5
|
+
def emit
|
6
|
+
if fields.size > 1
|
7
|
+
"emit({#{fields.join(", ")}}, 1);"
|
8
|
+
else
|
9
|
+
"emit(#{fields.first.translated}, 1);"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def count options
|
14
|
+
@as = options[:as]
|
15
|
+
end
|
16
|
+
|
17
|
+
def value_field_name
|
18
|
+
@as || 'count';
|
19
|
+
end
|
20
|
+
|
21
|
+
def reduce
|
22
|
+
%Q{
|
23
|
+
function(key, values) {
|
24
|
+
return Array.sum(values);
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def finalize
|
31
|
+
%Q{
|
32
|
+
function(key, values) {
|
33
|
+
return { #{value_field_name}: values };
|
34
|
+
}
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
class Reductor
|
2
|
+
module Reductions
|
3
|
+
module Sum
|
4
|
+
|
5
|
+
def emit
|
6
|
+
if fields.size > 1
|
7
|
+
"emit({ #{fields.join(", ")} } , #{translated_sum_field});"
|
8
|
+
else
|
9
|
+
"emit( #{fields.first.translated} , #{translated_sum_field});"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def translated_sum_field
|
14
|
+
"this.#{@sum_field}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def sum field, options = {}
|
18
|
+
@sum_field = field
|
19
|
+
@sum_field_name = options[:as] || @sum_field
|
20
|
+
end
|
21
|
+
|
22
|
+
def reduce
|
23
|
+
%Q{
|
24
|
+
function(key, values) {
|
25
|
+
return Array.sum(values);
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def finalize
|
31
|
+
%Q{
|
32
|
+
function(key, values) {
|
33
|
+
return { #{@sum_field_name}: values };
|
34
|
+
}
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/reductor.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reductor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcin Stecki
|
@@ -16,10 +16,14 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- .gitignore
|
19
20
|
- README.md
|
20
21
|
- lib/reductor.rb
|
22
|
+
- lib/reductor/converted_field.rb
|
21
23
|
- lib/reductor/reductions.rb
|
22
|
-
- lib/reductor/reductions/
|
24
|
+
- lib/reductor/reductions/count.rb
|
25
|
+
- lib/reductor/reductions/sum.rb
|
26
|
+
- lib/reductor/simple_field.rb
|
23
27
|
- reductor.gemspec
|
24
28
|
homepage: http://rubygems.org/gems/hola
|
25
29
|
licenses: []
|
@@ -45,3 +49,4 @@ signing_key:
|
|
45
49
|
specification_version: 4
|
46
50
|
summary: Map-reduce with ease.
|
47
51
|
test_files: []
|
52
|
+
has_rdoc:
|
@@ -1,32 +0,0 @@
|
|
1
|
-
class Reductor
|
2
|
-
module Reductions
|
3
|
-
module CountsByDay
|
4
|
-
|
5
|
-
def datetime_field
|
6
|
-
@options[:field] || 'created_at'
|
7
|
-
end
|
8
|
-
|
9
|
-
def map
|
10
|
-
%Q{
|
11
|
-
function() {
|
12
|
-
if(this.#{datetime_field}){
|
13
|
-
date = new Date(this.#{datetime_field}.getFullYear(), this.#{datetime_field}.getMonth(), this.#{datetime_field}.getDate());
|
14
|
-
} else {
|
15
|
-
date = null;
|
16
|
-
}
|
17
|
-
emit(date , 1);
|
18
|
-
}
|
19
|
-
}
|
20
|
-
end
|
21
|
-
|
22
|
-
def reduce
|
23
|
-
%Q{
|
24
|
-
function(key, values) {
|
25
|
-
return Array.sum(values);
|
26
|
-
}
|
27
|
-
}
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|