rchoice 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ gem 'rr'
14
+ end
15
+
16
+ gem 'mharris_ext'
17
+ gem 'andand'
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ andand (1.3.1)
5
+ diff-lcs (1.1.2)
6
+ facets (2.9.1)
7
+ fattr (2.2.0)
8
+ git (1.2.5)
9
+ jeweler (1.5.2)
10
+ bundler (~> 1.0.0)
11
+ git (>= 1.2.5)
12
+ rake
13
+ mharris_ext (1.5.0)
14
+ facets
15
+ fattr
16
+ rake (0.8.7)
17
+ rcov (0.9.9)
18
+ rr (1.0.2)
19
+ rspec (2.3.0)
20
+ rspec-core (~> 2.3.0)
21
+ rspec-expectations (~> 2.3.0)
22
+ rspec-mocks (~> 2.3.0)
23
+ rspec-core (2.3.1)
24
+ rspec-expectations (2.3.0)
25
+ diff-lcs (~> 1.1.2)
26
+ rspec-mocks (2.3.0)
27
+
28
+ PLATFORMS
29
+ x86-mingw32
30
+
31
+ DEPENDENCIES
32
+ andand
33
+ bundler (~> 1.0.0)
34
+ jeweler (~> 1.5.2)
35
+ mharris_ext
36
+ rcov
37
+ rr
38
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Mike Harris
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = choice
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to choice
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Mike Harris. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "rchoice"
16
+ gem.homepage = "http://github.com/mharris717/rchoice"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{choice}
19
+ gem.description = %Q{choice}
20
+ gem.email = "mharris717@gmail.com"
21
+ gem.authors = ["Mike Harris"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "choice #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/choice.rb ADDED
@@ -0,0 +1,61 @@
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
data/rchoice.gemspec ADDED
@@ -0,0 +1,73 @@
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.1.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-02}
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/choice.rb",
29
+ "rchoice.gemspec",
30
+ "spec/choice_spec.rb",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/mharris717/rchoice}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.5.2}
37
+ s.summary = %q{choice}
38
+ s.test_files = [
39
+ "spec/choice_spec.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<mharris_ext>, [">= 0"])
48
+ s.add_runtime_dependency(%q<andand>, [">= 0"])
49
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
52
+ s.add_development_dependency(%q<rcov>, [">= 0"])
53
+ s.add_development_dependency(%q<rr>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<mharris_ext>, [">= 0"])
56
+ s.add_dependency(%q<andand>, [">= 0"])
57
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
59
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
60
+ s.add_dependency(%q<rcov>, [">= 0"])
61
+ s.add_dependency(%q<rr>, [">= 0"])
62
+ end
63
+ else
64
+ s.add_dependency(%q<mharris_ext>, [">= 0"])
65
+ s.add_dependency(%q<andand>, [">= 0"])
66
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
67
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
68
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
69
+ s.add_dependency(%q<rcov>, [">= 0"])
70
+ s.add_dependency(%q<rr>, [">= 0"])
71
+ end
72
+ end
73
+
@@ -0,0 +1,58 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Choice" do
4
+ it 'smoke' do
5
+ 2.should == 2
6
+ end
7
+ before do
8
+ @choice = Choice::Choice.new
9
+ @choice.add_option 3
10
+ @choice.add_option 7
11
+ end
12
+
13
+ describe 'choosing first' do
14
+ before do
15
+ @choice.chooser = lambda { |c| c.options.first }
16
+ end
17
+ it 'should choose first' do
18
+ @choice.chosen_option.base_obj.should == 3
19
+ end
20
+ end
21
+
22
+ describe 'choosing none' do
23
+ before do
24
+ @choice.chooser = lambda { |c| nil }
25
+ end
26
+ it 'should allow no choice if optional is true' do
27
+ @choice.optional = true
28
+ @choice.chosen_option.should be_nil
29
+ end
30
+ it 'should error if optional is false' do
31
+ lambda { @choice.chosen_option }.should raise_error(Choice::OptionNotChosenError)
32
+ end
33
+
34
+ describe 'cl chooser' do
35
+ before do
36
+ chooser = Choice::CommandLineChooser.new
37
+ stub(chooser).get_choice_num { 1 }
38
+ stub(chooser).print_line(anything) { }
39
+ @choice.chooser = chooser
40
+ end
41
+ it 'should choose first' do
42
+ @choice.chosen_option.base_obj.should == 7
43
+ end
44
+ end
45
+ end
46
+
47
+ describe 'executing' do
48
+ before do
49
+ @results = []
50
+ @choice.action_blk = lambda { |op| @results << op }
51
+ @choice.chooser = lambda { |c| c.options.first }
52
+ end
53
+ it 'should call action' do
54
+ @choice.execute!
55
+ @results.should == [3]
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,14 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'rr'
5
+ #require 'choice'
6
+ require File.dirname(__FILE__) + "/../lib/choice"
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :rr
14
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rchoice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Harris
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-02 00:00:00.000000000 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mharris_ext
17
+ requirement: &14521524 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *14521524
26
+ - !ruby/object:Gem::Dependency
27
+ name: andand
28
+ requirement: &14520648 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *14520648
37
+ - !ruby/object:Gem::Dependency
38
+ name: rspec
39
+ requirement: &14485716 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 2.3.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *14485716
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: &14484972 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.0
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *14484972
59
+ - !ruby/object:Gem::Dependency
60
+ name: jeweler
61
+ requirement: &14484444 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.5.2
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *14484444
70
+ - !ruby/object:Gem::Dependency
71
+ name: rcov
72
+ requirement: &14483868 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *14483868
81
+ - !ruby/object:Gem::Dependency
82
+ name: rr
83
+ requirement: &14483160 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ! '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *14483160
92
+ description: choice
93
+ email: mharris717@gmail.com
94
+ executables: []
95
+ extensions: []
96
+ extra_rdoc_files:
97
+ - LICENSE.txt
98
+ - README.rdoc
99
+ files:
100
+ - .document
101
+ - .rspec
102
+ - Gemfile
103
+ - Gemfile.lock
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ - Rakefile
107
+ - VERSION
108
+ - lib/choice.rb
109
+ - rchoice.gemspec
110
+ - spec/choice_spec.rb
111
+ - spec/spec_helper.rb
112
+ has_rdoc: true
113
+ homepage: http://github.com/mharris717/rchoice
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ segments:
127
+ - 0
128
+ hash: -306794161
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 1.5.2
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: choice
141
+ test_files:
142
+ - spec/choice_spec.rb
143
+ - spec/spec_helper.rb