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.
- data/.document +5 -5
- data/.rspec +1 -1
- data/Gemfile +25 -17
- data/Gemfile.lock +62 -21
- data/Guardfile +27 -0
- data/LICENSE.txt +20 -20
- data/README.rdoc +19 -19
- data/Rakefile +42 -50
- data/VERSION +1 -1
- data/lib/rchoice.rb +10 -10
- data/lib/rchoice/choice.rb +41 -38
- data/lib/rchoice/choosers/command_line_chooser.rb +19 -19
- data/lib/rchoice/choosers/redis_chooser.rb +33 -0
- data/lib/rchoice/core_ext.rb +30 -30
- data/lib/rchoice/option.rb +23 -23
- data/rchoice.gemspec +89 -77
- data/spec/choice_spec.rb +122 -107
- data/spec/spec_helper.rb +24 -14
- data/vol/choice.txt +1 -0
- data/vol/set_choice.rb +5 -0
- metadata +128 -29
@@ -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
|
data/lib/rchoice/core_ext.rb
CHANGED
@@ -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
|
data/lib/rchoice/option.rb
CHANGED
@@ -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
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name =
|
8
|
-
s.version = "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 =
|
13
|
-
s.description =
|
14
|
-
s.email =
|
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
|
-
"
|
25
|
-
"
|
26
|
-
"
|
27
|
-
"
|
28
|
-
"
|
29
|
-
"lib/rchoice
|
30
|
-
"lib/rchoice/
|
31
|
-
"lib/rchoice/
|
32
|
-
"lib/rchoice/
|
33
|
-
"rchoice.
|
34
|
-
"
|
35
|
-
"
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
s.
|
42
|
-
s.
|
43
|
-
|
44
|
-
|
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.
|
54
|
-
s.add_development_dependency(%q<
|
55
|
-
s.add_development_dependency(%q<
|
56
|
-
s.add_development_dependency(%q<
|
57
|
-
s.add_development_dependency(%q<rr>, [">= 0"])
|
58
|
-
|
59
|
-
s.
|
60
|
-
s.
|
61
|
-
s.
|
62
|
-
|
63
|
-
s.add_dependency(%q<
|
64
|
-
s.add_dependency(%q<
|
65
|
-
s.add_dependency(%q<
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
end
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
'
|
106
|
-
|
107
|
-
|
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
|