rchoice 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -13,7 +13,7 @@ GEM
13
13
  mharris_ext (1.5.0)
14
14
  facets
15
15
  fattr
16
- rake (0.8.7)
16
+ rake (0.9.2.2)
17
17
  rcov (0.9.9)
18
18
  rr (1.0.2)
19
19
  rspec (2.3.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -0,0 +1,39 @@
1
+ module RChoice
2
+ class Choice
3
+ include FromHash
4
+ attr_accessor :optional, :action_blk, :chooser
5
+ fattr(:options) { [] }
6
+ fattr(:option_presenter) do
7
+ lambda { |o| o.to_s }
8
+ end
9
+ fattr(:chosen_option) do
10
+ res = chooser.call(self)
11
+ raise OptionNotChosenError.new if !res && !optional
12
+ res
13
+ end
14
+ def execute!
15
+ return unless action_blk
16
+ chosen_option.execute!(&action_blk) if chosen_option
17
+ end
18
+ def make_option(obj,&b)
19
+ return obj if obj.kind_of?(Option)
20
+ obj = {:base_obj => obj} unless obj.kind_of?(Hash)
21
+ Option.new(obj)
22
+ end
23
+ def add_option(obj,&b)
24
+ self.options << make_option(obj,&b)
25
+ end
26
+ def options=(raw)
27
+ @options = raw.map { |r| make_option(r) }
28
+ end
29
+ end
30
+
31
+ class ProcChoice < Choice
32
+ def execute!
33
+ chosen_option.execute! if chosen_option
34
+ end
35
+ def make_option(name,&b)
36
+ ProcOption.new(:name => name, :blk => b)
37
+ end
38
+ end
39
+ end
@@ -0,0 +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
20
+ end
@@ -0,0 +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
31
+ end
@@ -0,0 +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
24
+ end
data/lib/rchoice.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'mharris_ext'
2
+ require 'andand'
3
+
4
+ module RChoice
5
+ class OptionNotChosenError < Exception; end
6
+ end
7
+
8
+ d = File.expand_path(File.dirname(__FILE__))
9
+ Dir["#{d}/rchoice/**/*.rb"].each do |f|
10
+ require f
11
+ end
data/rchoice.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rchoice}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Mike Harris"]
12
- s.date = %q{2011-12-02}
12
+ s.date = %q{2011-12-14}
13
13
  s.description = %q{choice}
14
14
  s.email = %q{mharris717@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,7 +25,11 @@ Gem::Specification.new do |s|
25
25
  "README.rdoc",
26
26
  "Rakefile",
27
27
  "VERSION",
28
- "lib/choice.rb",
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",
29
33
  "rchoice.gemspec",
30
34
  "spec/choice_spec.rb",
31
35
  "spec/spec_helper.rb"
data/spec/choice_spec.rb CHANGED
@@ -1,11 +1,34 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "Choice" do
4
- it 'smoke' do
5
- 2.should == 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
6
18
  end
19
+ end
20
+
21
+ describe 'loading options' do
7
22
  before do
8
- @choice = Choice::Choice.new
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
9
32
  @choice.add_option 3
10
33
  @choice.add_option 7
11
34
  end
@@ -28,12 +51,12 @@ describe "Choice" do
28
51
  @choice.chosen_option.should be_nil
29
52
  end
30
53
  it 'should error if optional is false' do
31
- lambda { @choice.chosen_option }.should raise_error(Choice::OptionNotChosenError)
54
+ lambda { @choice.chosen_option }.should raise_error(RChoice::OptionNotChosenError)
32
55
  end
33
56
 
34
57
  describe 'cl chooser' do
35
58
  before do
36
- chooser = Choice::CommandLineChooser.new
59
+ chooser = RChoice::CommandLineChooser.new
37
60
  stub(chooser).get_choice_num { 1 }
38
61
  stub(chooser).print_line(anything) { }
39
62
  @choice.chooser = chooser
@@ -56,3 +79,30 @@ describe "Choice" do
56
79
  end
57
80
  end
58
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
108
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
4
  require 'rr'
5
5
  #require 'choice'
6
- require File.dirname(__FILE__) + "/../lib/choice"
6
+ require File.dirname(__FILE__) + "/../lib/rchoice"
7
7
 
8
8
  # Requires supporting files with custom matchers and macros, etc,
9
9
  # in ./support/ and its subdirectories.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rchoice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-02 00:00:00.000000000 -05:00
12
+ date: 2011-12-14 00:00:00.000000000 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mharris_ext
17
- requirement: &14521524 !ruby/object:Gem::Requirement
17
+ requirement: &15690072 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *14521524
25
+ version_requirements: *15690072
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: andand
28
- requirement: &14520648 !ruby/object:Gem::Requirement
28
+ requirement: &15689724 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *14520648
36
+ version_requirements: *15689724
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rspec
39
- requirement: &14485716 !ruby/object:Gem::Requirement
39
+ requirement: &15689184 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 2.3.0
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *14485716
47
+ version_requirements: *15689184
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &14484972 !ruby/object:Gem::Requirement
50
+ requirement: &15688704 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.0.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *14484972
58
+ version_requirements: *15688704
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: jeweler
61
- requirement: &14484444 !ruby/object:Gem::Requirement
61
+ requirement: &15688152 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.5.2
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *14484444
69
+ version_requirements: *15688152
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: rcov
72
- requirement: &14483868 !ruby/object:Gem::Requirement
72
+ requirement: &15644580 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ! '>='
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *14483868
80
+ version_requirements: *15644580
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rr
83
- requirement: &14483160 !ruby/object:Gem::Requirement
83
+ requirement: &15643872 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,7 +88,7 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *14483160
91
+ version_requirements: *15643872
92
92
  description: choice
93
93
  email: mharris717@gmail.com
94
94
  executables: []
@@ -105,7 +105,11 @@ files:
105
105
  - README.rdoc
106
106
  - Rakefile
107
107
  - VERSION
108
- - lib/choice.rb
108
+ - lib/rchoice.rb
109
+ - lib/rchoice/choice.rb
110
+ - lib/rchoice/choosers/command_line_chooser.rb
111
+ - lib/rchoice/core_ext.rb
112
+ - lib/rchoice/option.rb
109
113
  - rchoice.gemspec
110
114
  - spec/choice_spec.rb
111
115
  - spec/spec_helper.rb
@@ -125,7 +129,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
125
129
  version: '0'
126
130
  segments:
127
131
  - 0
128
- hash: -306794161
132
+ hash: -547180143
129
133
  required_rubygems_version: !ruby/object:Gem::Requirement
130
134
  none: false
131
135
  requirements:
data/lib/choice.rb DELETED
@@ -1,61 +0,0 @@
1
- require 'mharris_ext'
2
- require 'andand'
3
-
4
- class Array
5
- def rand_el
6
- i = rand(length)
7
- self[i]
8
- end
9
- end
10
-
11
- module Choice
12
- class OptionNotChosenError < Exception; end
13
- class Choice
14
- include FromHash
15
- attr_accessor :optional, :action_blk, :chooser
16
- fattr(:options) { [] }
17
- fattr(:option_presenter) do
18
- lambda { |o| o.to_s }
19
- end
20
- fattr(:chosen_option) do
21
- res = chooser.call(self)
22
- raise OptionNotChosenError.new if !res && !optional
23
- res
24
- end
25
- def execute!
26
- return unless action_blk
27
- chosen_option.execute!(&action_blk) if chosen_option
28
- end
29
- def add_option(obj)
30
- self.options << Option.new(:base_obj => obj)
31
- end
32
- end
33
-
34
- class Option
35
- include FromHash
36
- attr_accessor :base_obj
37
- fattr(:execute_args) { [base_obj] }
38
- def execute!(&b)
39
- b.call(*execute_args)
40
- end
41
- end
42
-
43
- class CommandLineChooser
44
- def call(choice)
45
- print_line "Choose"
46
- choice.options.each_with_index do |op,i|
47
- print_line "#{i}. #{op.base_obj.to_s}"
48
- end
49
- get_choice(choice)
50
- end
51
- def print_line(ln)
52
- puts ln
53
- end
54
- def get_choice(choice)
55
- choice.options[get_choice_num]
56
- end
57
- def get_choice_num
58
- STDIN.gets.to_i
59
- end
60
- end
61
- end