reductor 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 276380fee6ca82990688b8563dded3e1a3917344
4
- data.tar.gz: e3a96aa7bafa4c87f30326a66188e48bb75a3be8
3
+ metadata.gz: 6a5a238cb24cc1f8de7500be93a91693486367ec
4
+ data.tar.gz: ddeb998004dd0be173e73a358d9b11428431cd28
5
5
  SHA512:
6
- metadata.gz: 0c3d0d2d0424d3ed3a10ddb2c17dcd0b1cda06e5185b0a3cbed2c1824eff3f90e0d0af41b1e865d8feeb690ffda7c20943dc53e40bc0d57196d0f74b9b61bac1
7
- data.tar.gz: 9745420bc9af25e64f96647120f9ea27a4b791dd98b16eeb46137e3ede8dd71b9d4e1625cbb27f493a1165f9b7801c9e34c3f5964cf56ff6e5f92a4a99d34aba
6
+ metadata.gz: 32bc32f45554932f300f2893c701edc7ef1029d5e56bc3d49193577f8819fac8761ff1602a769348117ce826c897af56adc285f7e6d22bfc66a5b5ecf138f948
7
+ data.tar.gz: eb5b6999b97240ad15f65212506cf125c4cd83ec65fd6d1292a783c740d8b5c3c554ade963ecd10dd11fd0a60c642103c5ddea33493e106da8889b5cfae08704
@@ -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)> Project.all.reduction(:counts_by_day)
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,
@@ -1,27 +1,54 @@
1
1
  module Mongoid
2
2
  class Criteria
3
3
 
4
- def reduction(reduction_type, out: :inline, options: {})
5
- reductor = Reductor.new(self, reduction_type, options)
6
- Hash[reductor.reduce.out(inline: true).map{|e| [e['_id'], e["value"]] }]
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
- def initialize(collection, type, options)
17
- @collection = collection
18
- @type = type
19
- @options = options
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 reduce
36
+ def map
37
+ %Q{
38
+ function() {
39
+ #{emit}
40
+ }
41
+ }
42
+ end
43
+
44
+ def reduct
23
45
  self.extend reduction_module
24
- @collection.map_reduce(map, reduce)
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
@@ -1,4 +1,5 @@
1
- require 'reductor/reductions/counts_by_day'
1
+ require 'reductor/reductions/count'
2
+ require 'reductor/reductions/sum'
2
3
  class Reductor
3
4
  module Reductions
4
5
  # cats
@@ -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
@@ -0,0 +1,13 @@
1
+ class Reductor
2
+ class SimpleField < Struct.new(:field_name)
3
+
4
+ def to_s
5
+ "#{field_name}: #{translated}"
6
+ end
7
+
8
+ def translated
9
+ "this.#{field_name}"
10
+ end
11
+
12
+ end
13
+ end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'reductor'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.date = '2013-07-12'
5
5
  s.summary = "Map-reduce with ease."
6
6
  s.description = "Collection of common map_reduce operations, implemented with mongoid."
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.1
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/counts_by_day.rb
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