mr_mongo 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog.md CHANGED
@@ -1,6 +1,12 @@
1
1
  ChangeLog of Mr. Mongo
2
2
  ======================
3
3
 
4
+ 0.1.0 released 2013-04-08
5
+ -------------------------
6
+
7
+ - [Add] Dynamic Parameters feature
8
+ - [Add] Default Parameter feature
9
+
4
10
  0.0.1 released 2013-04-08
5
11
  -------------------------
6
12
 
data/README.md CHANGED
@@ -64,6 +64,37 @@ function (key, values) {
64
64
  }
65
65
  ```
66
66
 
67
+ ### Dynamic Parameters
68
+
69
+ sub-commands `exec` and `exec_on_memory` accepts *Dynamic Parameters* by `--params` option.
70
+
71
+ `--params` can be specified as JSON hash.
72
+
73
+ ```
74
+ $ bundle exec mr_mongo exec some_map_reduce.rb --params '{"date":"2013-02-01"}'
75
+ ```
76
+
77
+ In DSL, Dynamic Parameters can be get with `#params` method.
78
+
79
+ ```ruby
80
+ set :collection, 'accesses'
81
+ set :query, {date: params['date']} if params['date']
82
+
83
+ __END__
84
+
85
+ ...
86
+ ```
87
+
88
+ ### Default Parameter
89
+
90
+ Default Parameter can be set with `#default_param` method
91
+
92
+ ```ruby
93
+ default_param 'date', '2013-02-01'
94
+
95
+ # => params will be {"date" => "2013-02-01"} by default.
96
+ ```
97
+
67
98
  ## Contributing
68
99
 
69
100
  1. Fork it
@@ -1,5 +1,6 @@
1
1
  require 'thor'
2
2
  require 'mongo'
3
+ require 'multi_json'
3
4
  require 'pp'
4
5
 
5
6
  module MrMongo
@@ -8,6 +9,7 @@ module MrMongo
8
9
 
9
10
  desc 'exec', 'executes MapReduce'
10
11
  method_option :db, default: DEFAULT_URI
12
+ method_option :params
11
13
  def exec(*files)
12
14
  files.each do |file|
13
15
  puts "Executing #{file}..."
@@ -20,6 +22,7 @@ module MrMongo
20
22
 
21
23
  desc 'exec_on_memory', 'executes MapReduce on memory'
22
24
  method_option :db, default: DEFAULT_URI
25
+ method_option :params
23
26
  def exec_on_memory(*files)
24
27
  files.each do |file|
25
28
  puts "Executing #{file} on memory..."
@@ -36,7 +39,10 @@ module MrMongo
36
39
  end
37
40
 
38
41
  def context
39
- Context.new(db: database)
42
+ context_params = {db: database}
43
+ context_params[:params] = ::MultiJson.load(options[:params]) if options[:params]
44
+
45
+ Context.new(context_params)
40
46
  end
41
47
 
42
48
  def database
@@ -1,9 +1,14 @@
1
1
  module MrMongo
2
2
  class Context
3
- attr_reader :db
3
+ attr_reader :db, :params
4
4
 
5
5
  def initialize(params = {})
6
- @db = params[:db] if params[:db]
6
+ @db = params[:db] if params[:db]
7
+ @params = params[:params] if params[:params]
8
+ end
9
+
10
+ def params?
11
+ not @params.nil?
7
12
  end
8
13
  end
9
14
  end
data/lib/mr_mongo/dsl.rb CHANGED
@@ -3,7 +3,9 @@ module MrMongo
3
3
  attr_reader :map_reduce
4
4
 
5
5
  def initialize(context)
6
- @map_reduce = MapReduce.new(context)
6
+ @context = context
7
+ @map_reduce = MapReduce.new(context)
8
+ @default_params = {}
7
9
  end
8
10
 
9
11
  def parse(&block)
@@ -15,5 +17,17 @@ module MrMongo
15
17
  def set(key, value = true)
16
18
  @map_reduce.send("#{key}=", value)
17
19
  end
20
+
21
+ def params
22
+ if @context.params?
23
+ @default_params.merge(@context.params)
24
+ else
25
+ @default_params
26
+ end
27
+ end
28
+
29
+ def default_param(key, value)
30
+ @default_params[key] = value
31
+ end
18
32
  end
19
33
  end
@@ -1,3 +1,3 @@
1
1
  module MrMongo
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/mr_mongo.gemspec CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_dependency "mongo", "~> 1.8.4"
22
22
  spec.add_dependency "bson_ext", "~> 1.8.4"
23
23
  spec.add_dependency "thor", "~> 0.18.1"
24
+ spec.add_dependency "multi_json", "~> 1.7.2"
24
25
  spec.add_dependency "inline_template_loader", "~> 0.3.0"
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.3"
@@ -20,5 +20,44 @@ module MrMongo
20
20
  it { should be_true }
21
21
  end
22
22
  end
23
+
24
+ describe '#params' do
25
+ subject { dsl.params }
26
+
27
+ context 'when context has no params' do
28
+ context 'without default params' do
29
+ it { should == {} }
30
+ end
31
+
32
+ context 'with default params' do
33
+ before { dsl.default_param 'foo', 'bar' }
34
+
35
+ it { should eq({'foo' => 'bar'}) }
36
+ end
37
+ end
38
+
39
+ context 'when context has params' do
40
+ let(:context) { Context.new(params: input_params) }
41
+ let(:input_params) { {'hoge' => 'fuga'} }
42
+
43
+ context 'without default params' do
44
+ it { should == input_params }
45
+ end
46
+
47
+ context 'with default params' do
48
+ context 'no override' do
49
+ before { dsl.default_param 'foo', 'bar' }
50
+
51
+ it { should eq({'foo' => 'bar', 'hoge' => 'fuga'}) }
52
+ end
53
+
54
+ context 'override' do
55
+ before { dsl.default_param 'hoge', 'piyo' }
56
+
57
+ it { should eq({'hoge' => 'fuga'}) }
58
+ end
59
+ end
60
+ end
61
+ end
23
62
  end
24
63
  end
@@ -1,6 +1,6 @@
1
1
  module MrMongo
2
2
  describe MapReduce do
3
- let(:context) { Context.new(db: testing_database) }
3
+ let(:context) { Context.new }
4
4
  let(:map_reduce) { MapReduce.new(context) }
5
5
 
6
6
  describe '#to_options' do
@@ -26,6 +26,8 @@ module MrMongo
26
26
  end
27
27
 
28
28
  describe 'MapReduce excution', mongo: true do
29
+ let(:context) { Context.new(db: testing_database) }
30
+
29
31
  let(:expected) do
30
32
  [
31
33
  {'_id' => 'be', 'value' => 2.0},
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mr_mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-07 00:00:00.000000000 Z
12
+ date: 2013-04-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongo
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.18.1
62
+ - !ruby/object:Gem::Dependency
63
+ name: multi_json
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 1.7.2
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 1.7.2
62
78
  - !ruby/object:Gem::Dependency
63
79
  name: inline_template_loader
64
80
  requirement: !ruby/object:Gem::Requirement
@@ -218,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
234
  version: '0'
219
235
  segments:
220
236
  - 0
221
- hash: 1664865836217424745
237
+ hash: 1026442737
222
238
  required_rubygems_version: !ruby/object:Gem::Requirement
223
239
  none: false
224
240
  requirements:
@@ -227,7 +243,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
243
  version: '0'
228
244
  segments:
229
245
  - 0
230
- hash: 1664865836217424745
246
+ hash: 1026442737
231
247
  requirements: []
232
248
  rubyforge_project:
233
249
  rubygems_version: 1.8.23