reductor 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +23 -0
- data/lib/reductor.rb +32 -0
- data/lib/reductor/reductions.rb +6 -0
- data/lib/reductor/reductions/counts_by_day.rb +32 -0
- data/reductor.gemspec +14 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 276380fee6ca82990688b8563dded3e1a3917344
|
4
|
+
data.tar.gz: e3a96aa7bafa4c87f30326a66188e48bb75a3be8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0c3d0d2d0424d3ed3a10ddb2c17dcd0b1cda06e5185b0a3cbed2c1824eff3f90e0d0af41b1e865d8feeb690ffda7c20943dc53e40bc0d57196d0f74b9b61bac1
|
7
|
+
data.tar.gz: 9745420bc9af25e64f96647120f9ea27a4b791dd98b16eeb46137e3ede8dd71b9d4e1625cbb27f493a1165f9b7801c9e34c3f5964cf56ff6e5f92a4a99d34aba
|
data/README.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Reductor
|
2
|
+
========
|
3
|
+
|
4
|
+
Collection of common map_reduce operations, implemented with mongoid.
|
5
|
+
## Basic sample:
|
6
|
+
|
7
|
+
```
|
8
|
+
pry(main)> Project.all.reduction(:counts_by_day)
|
9
|
+
=> {
|
10
|
+
nil => 12.0,
|
11
|
+
2012-05-10 22:00:00 UTC => 1.0,
|
12
|
+
2012-05-14 22:00:00 UTC => 1.0,
|
13
|
+
2012-05-30 22:00:00 UTC => 1.0,
|
14
|
+
2012-06-02 22:00:00 UTC => 2.0,
|
15
|
+
2012-06-07 22:00:00 UTC => 2.0,
|
16
|
+
2012-07-01 22:00:00 UTC => 1.0,
|
17
|
+
2012-07-11 22:00:00 UTC => 1.0,
|
18
|
+
2012-07-15 22:00:00 UTC => 1.0,
|
19
|
+
2012-08-05 22:00:00 UTC => 1.0,
|
20
|
+
2012-08-10 22:00:00 UTC => 1.0
|
21
|
+
}
|
22
|
+
|
23
|
+
```
|
data/lib/reductor.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Mongoid
|
2
|
+
class Criteria
|
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"]] }]
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'reductor/reductions'
|
13
|
+
|
14
|
+
class Reductor
|
15
|
+
|
16
|
+
def initialize(collection, type, options)
|
17
|
+
@collection = collection
|
18
|
+
@type = type
|
19
|
+
@options = options
|
20
|
+
end
|
21
|
+
|
22
|
+
def reduce
|
23
|
+
self.extend reduction_module
|
24
|
+
@collection.map_reduce(map, reduce)
|
25
|
+
end
|
26
|
+
|
27
|
+
def reduction_module
|
28
|
+
"Reductor::Reductions::#{@type.to_s.classify}".constantize
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,32 @@
|
|
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
|
data/reductor.gemspec
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'reductor'
|
3
|
+
s.version = '0.0.1'
|
4
|
+
s.date = '2013-07-12'
|
5
|
+
s.summary = "Map-reduce with ease."
|
6
|
+
s.description = "Collection of common map_reduce operations, implemented with mongoid."
|
7
|
+
s.authors = ["Marcin Stecki"]
|
8
|
+
s.email = 'marcin@netguru.co'
|
9
|
+
s.files = `git ls-files`.split("\n")
|
10
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
11
|
+
s.require_paths = ["lib"]
|
12
|
+
s.homepage =
|
13
|
+
'http://rubygems.org/gems/hola'
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reductor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcin Stecki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-12 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Collection of common map_reduce operations, implemented with mongoid.
|
14
|
+
email: marcin@netguru.co
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- README.md
|
20
|
+
- lib/reductor.rb
|
21
|
+
- lib/reductor/reductions.rb
|
22
|
+
- lib/reductor/reductions/counts_by_day.rb
|
23
|
+
- reductor.gemspec
|
24
|
+
homepage: http://rubygems.org/gems/hola
|
25
|
+
licenses: []
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.0.3
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: Map-reduce with ease.
|
47
|
+
test_files: []
|