rchoice 0.2.0 → 0.3.0

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.
@@ -1,20 +1,20 @@
1
- module RChoice
2
- class CommandLineChooser
3
- def call(choice)
4
- print_line "Choose"
5
- choice.options.each_with_index do |op,i|
6
- print_line "#{i}. #{choice.option_presenter[op]}"
7
- end
8
- get_choice(choice)
9
- end
10
- def print_line(ln)
11
- puts ln
12
- end
13
- def get_choice(choice)
14
- choice.options[get_choice_num]
15
- end
16
- def get_choice_num
17
- STDIN.gets.safe_to_i
18
- end
19
- end
1
+ module RChoice
2
+ class CommandLineChooser
3
+ def call(choice)
4
+ print_line "Choose #{choice.name}"
5
+ choice.options.each_with_index do |op,i|
6
+ print_line "#{i}. #{choice.option_presenter[op]}"
7
+ end
8
+ get_choice(choice)
9
+ end
10
+ def print_line(ln)
11
+ puts ln
12
+ end
13
+ def get_choice(choice)
14
+ choice.options[get_choice_num]
15
+ end
16
+ def get_choice_num
17
+ STDIN.gets.safe_to_i
18
+ end
19
+ end
20
20
  end
@@ -0,0 +1,33 @@
1
+ module RChoice
2
+ class RedisChooser
3
+ def call(choice)
4
+ print_line "Choose #{choice.name}"
5
+ choice.options.each_with_index do |op,i|
6
+ print_line "#{i}. #{choice.option_presenter[op]}"
7
+ end
8
+ get_choice(choice)
9
+ end
10
+ def print_line(ln)
11
+ puts ln
12
+ end
13
+ def get_choice(choice)
14
+ choice.options[get_choice_num(choice)]
15
+ end
16
+ def get_choice_num(choice)
17
+ require 'redis'
18
+ redis = Redis.new(:host => "spadefish.redistogo.com", :port => 9897, :password => "511c6d49855d3551c335781860c06cb3")
19
+ k = "choice-#{choice.choice_id}"
20
+ puts "waiting for #{k}"
21
+ File.create "vol/choice.txt",k
22
+ (0...10000).each do |i|
23
+ val = redis.get(k)
24
+ if val.present?
25
+ return val.safe_to_i
26
+ else
27
+ sleep(0.5)
28
+ end
29
+ end
30
+ raise "no answer"
31
+ end
32
+ end
33
+ end
@@ -1,31 +1,31 @@
1
- class Array
2
- def rand_el
3
- i = rand(length)
4
- self[i]
5
- end
6
- end
7
-
8
- class Object
9
- def blank?
10
- to_s.strip == ''
11
- end
12
- def present?
13
- !blank?
14
- end
15
- end
16
-
17
- class String
18
- def valid_number_format?
19
- return false if blank?
20
- return false unless self.strip =~ /^[0-9\.\-]+$/
21
- true
22
- end
23
- def safe_to_i
24
- raise "not valid number format #{self}" unless valid_number_format?
25
- to_i
26
- end
27
- def safe_to_f
28
- raise "not valid number format #{self}" unless valid_number_format?
29
- to_f
30
- end
1
+ class Array
2
+ def rand_el
3
+ i = rand(length)
4
+ self[i]
5
+ end
6
+ end
7
+
8
+ class Object
9
+ def blank?
10
+ to_s.strip == ''
11
+ end
12
+ def present?
13
+ !blank?
14
+ end
15
+ end
16
+
17
+ class String
18
+ def valid_number_format?
19
+ return false if blank?
20
+ return false unless self.strip =~ /^[0-9\.\-]+$/
21
+ true
22
+ end
23
+ def safe_to_i
24
+ raise "not valid number format '#{self}'" unless valid_number_format?
25
+ to_i
26
+ end
27
+ def safe_to_f
28
+ raise "not valid number format #{self}" unless valid_number_format?
29
+ to_f
30
+ end
31
31
  end
@@ -1,24 +1,24 @@
1
- module RChoice
2
- class Option
3
- include FromHash
4
- attr_accessor :base_obj
5
- fattr(:execute_args) { [base_obj] }
6
- def execute!(&b)
7
- b.call(*execute_args)
8
- end
9
- def to_s
10
- base_obj.to_s
11
- end
12
- end
13
-
14
- class ProcOption < Option
15
- include FromHash
16
- attr_accessor :name, :blk
17
- def execute!
18
- blk.call
19
- end
20
- def to_s
21
- name
22
- end
23
- end
1
+ module RChoice
2
+ class Option
3
+ include FromHash
4
+ attr_accessor :base_obj
5
+ fattr(:execute_args) { [base_obj] }
6
+ def execute!(&b)
7
+ b.call(*execute_args)
8
+ end
9
+ def to_s
10
+ base_obj.to_s
11
+ end
12
+ end
13
+
14
+ class ProcOption < Option
15
+ include FromHash
16
+ attr_accessor :name, :blk
17
+ def execute!
18
+ blk.call
19
+ end
20
+ def to_s
21
+ name
22
+ end
23
+ end
24
24
  end
data/rchoice.gemspec CHANGED
@@ -1,77 +1,89 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{rchoice}
8
- s.version = "0.2.0"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Mike Harris"]
12
- s.date = %q{2011-12-14}
13
- s.description = %q{choice}
14
- s.email = %q{mharris717@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE.txt",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".rspec",
22
- "Gemfile",
23
- "Gemfile.lock",
24
- "LICENSE.txt",
25
- "README.rdoc",
26
- "Rakefile",
27
- "VERSION",
28
- "lib/rchoice.rb",
29
- "lib/rchoice/choice.rb",
30
- "lib/rchoice/choosers/command_line_chooser.rb",
31
- "lib/rchoice/core_ext.rb",
32
- "lib/rchoice/option.rb",
33
- "rchoice.gemspec",
34
- "spec/choice_spec.rb",
35
- "spec/spec_helper.rb"
36
- ]
37
- s.homepage = %q{http://github.com/mharris717/rchoice}
38
- s.licenses = ["MIT"]
39
- s.require_paths = ["lib"]
40
- s.rubygems_version = %q{1.5.2}
41
- s.summary = %q{choice}
42
- s.test_files = [
43
- "spec/choice_spec.rb",
44
- "spec/spec_helper.rb"
45
- ]
46
-
47
- if s.respond_to? :specification_version then
48
- s.specification_version = 3
49
-
50
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
- s.add_runtime_dependency(%q<mharris_ext>, [">= 0"])
52
- s.add_runtime_dependency(%q<andand>, [">= 0"])
53
- s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
54
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
55
- s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
56
- s.add_development_dependency(%q<rcov>, [">= 0"])
57
- s.add_development_dependency(%q<rr>, [">= 0"])
58
- else
59
- s.add_dependency(%q<mharris_ext>, [">= 0"])
60
- s.add_dependency(%q<andand>, [">= 0"])
61
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
62
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
63
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
64
- s.add_dependency(%q<rcov>, [">= 0"])
65
- s.add_dependency(%q<rr>, [">= 0"])
66
- end
67
- else
68
- s.add_dependency(%q<mharris_ext>, [">= 0"])
69
- s.add_dependency(%q<andand>, [">= 0"])
70
- s.add_dependency(%q<rspec>, ["~> 2.3.0"])
71
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
72
- s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
73
- s.add_dependency(%q<rcov>, [">= 0"])
74
- s.add_dependency(%q<rr>, [">= 0"])
75
- end
76
- end
77
-
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "rchoice"
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mike Harris"]
12
+ s.date = "2013-05-15"
13
+ s.description = "choice"
14
+ s.email = "mharris717@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "Guardfile",
25
+ "LICENSE.txt",
26
+ "README.rdoc",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/rchoice.rb",
30
+ "lib/rchoice/choice.rb",
31
+ "lib/rchoice/choosers/command_line_chooser.rb",
32
+ "lib/rchoice/choosers/redis_chooser.rb",
33
+ "lib/rchoice/core_ext.rb",
34
+ "lib/rchoice/option.rb",
35
+ "rchoice.gemspec",
36
+ "spec/choice_spec.rb",
37
+ "spec/spec_helper.rb",
38
+ "vol/choice.txt",
39
+ "vol/set_choice.rb"
40
+ ]
41
+ s.homepage = "http://github.com/mharris717/rchoice"
42
+ s.licenses = ["MIT"]
43
+ s.require_paths = ["lib"]
44
+ s.rubygems_version = "1.8.23"
45
+ s.summary = "choice"
46
+
47
+ if s.respond_to? :specification_version then
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<mharris_ext>, [">= 0"])
52
+ s.add_runtime_dependency(%q<andand>, [">= 0"])
53
+ s.add_runtime_dependency(%q<redis>, [">= 0"])
54
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
55
+ s.add_development_dependency(%q<bundler>, [">= 1.2"])
56
+ s.add_development_dependency(%q<jeweler>, [">= 1.5.2"])
57
+ s.add_development_dependency(%q<rr>, [">= 0"])
58
+ s.add_development_dependency(%q<guard>, [">= 0"])
59
+ s.add_development_dependency(%q<guard-rspec>, [">= 0"])
60
+ s.add_development_dependency(%q<guard-spork>, [">= 0"])
61
+ s.add_development_dependency(%q<lre>, [">= 0"])
62
+ else
63
+ s.add_dependency(%q<mharris_ext>, [">= 0"])
64
+ s.add_dependency(%q<andand>, [">= 0"])
65
+ s.add_dependency(%q<redis>, [">= 0"])
66
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
67
+ s.add_dependency(%q<bundler>, [">= 1.2"])
68
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
69
+ s.add_dependency(%q<rr>, [">= 0"])
70
+ s.add_dependency(%q<guard>, [">= 0"])
71
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
72
+ s.add_dependency(%q<guard-spork>, [">= 0"])
73
+ s.add_dependency(%q<lre>, [">= 0"])
74
+ end
75
+ else
76
+ s.add_dependency(%q<mharris_ext>, [">= 0"])
77
+ s.add_dependency(%q<andand>, [">= 0"])
78
+ s.add_dependency(%q<redis>, [">= 0"])
79
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
80
+ s.add_dependency(%q<bundler>, [">= 1.2"])
81
+ s.add_dependency(%q<jeweler>, [">= 1.5.2"])
82
+ s.add_dependency(%q<rr>, [">= 0"])
83
+ s.add_dependency(%q<guard>, [">= 0"])
84
+ s.add_dependency(%q<guard-rspec>, [">= 0"])
85
+ s.add_dependency(%q<guard-spork>, [">= 0"])
86
+ s.add_dependency(%q<lre>, [">= 0"])
87
+ end
88
+ end
89
+
data/spec/choice_spec.rb CHANGED
@@ -1,108 +1,123 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe 'ProcChoice' do
4
- before do
5
- @results = []
6
- @choice = RChoice::ProcChoice.new
7
- @choice.add_option(:a) { @results << :a }
8
- @choice.add_option(:b) { @results << :b }
9
- end
10
- describe 'choosing first' do
11
- before do
12
- @choice.chooser = lambda { |c| c.options.first }
13
- end
14
- it 'should execute first action' do
15
- @choice.execute!
16
- @results.should == [:a]
17
- end
18
- end
19
- end
20
-
21
- describe 'loading options' do
22
- before do
23
- @choice = RChoice::Choice.new(:options => [3,7])
24
- end
25
- it 'should load options' do
26
- @choice.options.first.class.should == RChoice::Option
27
- end
28
- end
29
- describe "Choice" do
30
- before do
31
- @choice = RChoice::Choice.new
32
- @choice.add_option 3
33
- @choice.add_option 7
34
- end
35
-
36
- describe 'choosing first' do
37
- before do
38
- @choice.chooser = lambda { |c| c.options.first }
39
- end
40
- it 'should choose first' do
41
- @choice.chosen_option.base_obj.should == 3
42
- end
43
- end
44
-
45
- describe 'choosing none' do
46
- before do
47
- @choice.chooser = lambda { |c| nil }
48
- end
49
- it 'should allow no choice if optional is true' do
50
- @choice.optional = true
51
- @choice.chosen_option.should be_nil
52
- end
53
- it 'should error if optional is false' do
54
- lambda { @choice.chosen_option }.should raise_error(RChoice::OptionNotChosenError)
55
- end
56
-
57
- describe 'cl chooser' do
58
- before do
59
- chooser = RChoice::CommandLineChooser.new
60
- stub(chooser).get_choice_num { 1 }
61
- stub(chooser).print_line(anything) { }
62
- @choice.chooser = chooser
63
- end
64
- it 'should choose first' do
65
- @choice.chosen_option.base_obj.should == 7
66
- end
67
- end
68
- end
69
-
70
- describe 'executing' do
71
- before do
72
- @results = []
73
- @choice.action_blk = lambda { |op| @results << op }
74
- @choice.chooser = lambda { |c| c.options.first }
75
- end
76
- it 'should call action' do
77
- @choice.execute!
78
- @results.should == [3]
79
- end
80
- end
81
- end
82
-
83
- describe 'core' do
84
- it 'should parse 0' do
85
- '0'.safe_to_i.should == 0
86
- ' 0 '.safe_to_i.should == 0
87
- end
88
- it 'should parse 1' do
89
- '1'.safe_to_i.should == 1
90
- ' 1 '.safe_to_i.should == 1
91
- end
92
- it 'should parse -1' do
93
- '-1'.safe_to_i.should == -1
94
- ' -1 '.safe_to_i.should == -1
95
- end
96
- it 'should not parse blank' do
97
- lambda { ''.safe_to_i }.should raise_error
98
- lambda { ' '.safe_to_i }.should raise_error
99
- end
100
- it 'should not parse junk' do
101
- lambda { ' ab'.safe_to_i }.should raise_error
102
- lambda { 'a4'.safe_to_i }.should raise_error
103
- end
104
- it 'should parse -1.5' do
105
- '-1.5'.safe_to_i.should == -1
106
- ' -1.5 '.safe_to_i.should == -1
107
- end
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'ProcChoice' do
4
+ before do
5
+ @results = []
6
+ @choice = RChoice::ProcChoice.new
7
+ @choice.add_option(:a) { @results << :a }
8
+ @choice.add_option(:b) { @results << :b }
9
+ end
10
+ describe 'choosing first' do
11
+ before do
12
+ @choice.chooser = lambda { |c| c.options.first }
13
+ end
14
+ it 'should execute first action' do
15
+ @choice.execute!
16
+ @results.should == [:a]
17
+ end
18
+ end
19
+ end
20
+
21
+ describe 'loading options' do
22
+ before do
23
+ @choice = RChoice::Choice.new(:options => [3,7])
24
+ end
25
+ it 'should load options' do
26
+ @choice.options.first.class.should == RChoice::Option
27
+ end
28
+ end
29
+
30
+ describe 'redis' do
31
+ let(:choice) do
32
+ res = RChoice::Choice.new(:options => [3,7])
33
+ res.chooser = RChoice::RedisChooser.new
34
+ res
35
+ end
36
+ it 'should load options' do
37
+ choice.options.first.class.should == RChoice::Option
38
+ end
39
+ it 'gets answer' do
40
+ puts choice.chosen_option.inspect
41
+ end
42
+ end
43
+
44
+ describe "Choice" do
45
+ before do
46
+ @choice = RChoice::Choice.new
47
+ @choice.add_option 3
48
+ @choice.add_option 7
49
+ end
50
+
51
+ describe 'choosing first' do
52
+ before do
53
+ @choice.chooser = lambda { |c| c.options.first }
54
+ end
55
+ it 'should choose first' do
56
+ @choice.chosen_option.base_obj.should == 3
57
+ end
58
+ end
59
+
60
+ describe 'choosing none' do
61
+ before do
62
+ @choice.chooser = lambda { |c| nil }
63
+ end
64
+ it 'should allow no choice if optional is true' do
65
+ @choice.optional = true
66
+ @choice.chosen_option.should be_nil
67
+ end
68
+ it 'should error if optional is false' do
69
+ lambda { @choice.chosen_option }.should raise_error(RChoice::OptionNotChosenError)
70
+ end
71
+
72
+ describe 'cl chooser' do
73
+ before do
74
+ chooser = RChoice::CommandLineChooser.new
75
+ stub(chooser).get_choice_num { 1 }
76
+ stub(chooser).print_line(anything) { }
77
+ @choice.chooser = chooser
78
+ end
79
+ it 'should choose first' do
80
+ @choice.chosen_option.base_obj.should == 7
81
+ end
82
+ end
83
+ end
84
+
85
+ describe 'executing' do
86
+ before do
87
+ @results = []
88
+ @choice.action_blk = lambda { |op| @results << op }
89
+ @choice.chooser = lambda { |c| c.options.first }
90
+ end
91
+ it 'should call action' do
92
+ @choice.execute!
93
+ @results.should == [3]
94
+ end
95
+ end
96
+ end
97
+
98
+ describe 'core' do
99
+ it 'should parse 0' do
100
+ '0'.safe_to_i.should == 0
101
+ ' 0 '.safe_to_i.should == 0
102
+ end
103
+ it 'should parse 1' do
104
+ '1'.safe_to_i.should == 1
105
+ ' 1 '.safe_to_i.should == 1
106
+ end
107
+ it 'should parse -1' do
108
+ '-1'.safe_to_i.should == -1
109
+ ' -1 '.safe_to_i.should == -1
110
+ end
111
+ it 'should not parse blank' do
112
+ lambda { ''.safe_to_i }.should raise_error
113
+ lambda { ' '.safe_to_i }.should raise_error
114
+ end
115
+ it 'should not parse junk' do
116
+ lambda { ' ab'.safe_to_i }.should raise_error
117
+ lambda { 'a4'.safe_to_i }.should raise_error
118
+ end
119
+ it 'should parse -1.5' do
120
+ '-1.5'.safe_to_i.should == -1
121
+ ' -1.5 '.safe_to_i.should == -1
122
+ end
108
123
  end