eor 0.1.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 +3 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +96 -0
- data/Rakefile +26 -0
- data/lib/eor.rb +29 -0
- data/lib/eor/version.rb +3 -0
- data/spec/eor.rb +66 -0
- metadata +88 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ben Weissmann
|
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,96 @@
|
|
1
|
+
= EOr
|
2
|
+
|
3
|
+
http://github.com/benweissmann/eor
|
4
|
+
|
5
|
+
By Ben Weissmann, mailto:ben@benweissmann.com
|
6
|
+
|
7
|
+
Adds Proc#eor, which takes an arbitrary number of procs and an optional backup value,
|
8
|
+
and returns the result of the first Proc that doesn't throw an error.
|
9
|
+
|
10
|
+
Procs recive as arguments all previous errors.
|
11
|
+
|
12
|
+
If all Procs throw errors, eor returns nil.
|
13
|
+
|
14
|
+
== Usage
|
15
|
+
|
16
|
+
See the spec folder for more in-depth examples.
|
17
|
+
|
18
|
+
=== Basics
|
19
|
+
|
20
|
+
proc{ 'brevity' }.eor
|
21
|
+
# => "brevity"
|
22
|
+
|
23
|
+
proc { not_a_method }.eor
|
24
|
+
# => nil
|
25
|
+
|
26
|
+
=== With Backup Values
|
27
|
+
|
28
|
+
proc { not_a_method }.eor proc { 'is' }
|
29
|
+
# => "is"
|
30
|
+
|
31
|
+
proc { not_a_method }.eor do
|
32
|
+
'the'
|
33
|
+
end
|
34
|
+
# => "the"
|
35
|
+
|
36
|
+
proc { not_a_method }.eor 'soul'
|
37
|
+
# => "soul"
|
38
|
+
|
39
|
+
proc { not_a_method }.eor proc {10 / 0}
|
40
|
+
# => nil
|
41
|
+
|
42
|
+
=== With Many Procs
|
43
|
+
|
44
|
+
proc { not_a_method }.eor proc { 10 / 0 }, proc { system }, 'of'
|
45
|
+
# => "of"
|
46
|
+
|
47
|
+
proc { not_a_method }.eor proc {10 / 0 }, proc { 'wit' }, proc { $foo = true }
|
48
|
+
# => "wit"
|
49
|
+
|
50
|
+
$foo # foo wasn't set by the previous line, becuase a successful proc was found first.
|
51
|
+
# => nil
|
52
|
+
|
53
|
+
proc { not_a_method }.eor proc {10 / 0 }, proc { '--Shakespeare' }, proc { " " }
|
54
|
+
# => "--Shakespeare"
|
55
|
+
|
56
|
+
proc { not_a_method }.eor proc {10 / 0 }, proc { system }
|
57
|
+
# => nil
|
58
|
+
|
59
|
+
=== Using the Previous Errors
|
60
|
+
|
61
|
+
proc { not_a_method }.eor proc {10 / 0 }, proc { system }, proc {|e| e}
|
62
|
+
# => #<NameError: undefined local variable or method `not_a_method' for main:Object>
|
63
|
+
|
64
|
+
proc { not_a_method }.eor proc {10 / 0 }, proc { system }, proc {|*errs| errs.last}
|
65
|
+
# => #<ArgumentError: wrong number of arguments>
|
66
|
+
|
67
|
+
proc { not_a_method }.eor proc {10 / 0 }, proc { system }, proc {|*errs| errs.inspect}
|
68
|
+
# => "[#<NameError: undefined local variable or method `not_a_method' for main:Object>,
|
69
|
+
#<ZeroDivisionError: divided by 0>,
|
70
|
+
#<ArgumentError: wrong number of arguments>]"
|
71
|
+
|
72
|
+
== Installing and Building
|
73
|
+
|
74
|
+
To install via RubyGems (from http://rubygems.org)
|
75
|
+
|
76
|
+
gem install eor
|
77
|
+
|
78
|
+
To build the gem from these sources:
|
79
|
+
|
80
|
+
rake build
|
81
|
+
|
82
|
+
To install the gem from these sources:
|
83
|
+
|
84
|
+
rake install
|
85
|
+
|
86
|
+
== Contact
|
87
|
+
|
88
|
+
Questions, comment, suggestions, and flames can be sent to mailto:ben@benweissmann.com
|
89
|
+
|
90
|
+
== Contribute
|
91
|
+
|
92
|
+
Have a patch? Email it to mailto:ben@benweissmann.com or fork me and submit a pull request.
|
93
|
+
|
94
|
+
== Copyright
|
95
|
+
|
96
|
+
Copyright (c) 2010 Ben Weissmann. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'lib/eor/version'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.version = EOr::EOR_VERSION
|
9
|
+
gem.name = "eor"
|
10
|
+
gem.summary = "Adds #eor to the Proc class"
|
11
|
+
gem.description = "Proc#eor allows you to specify any number of Procs, and get the result of the first successful Proc"
|
12
|
+
gem.email = "benweissmann@gmail.com"
|
13
|
+
gem.homepage = "http://github.com/benweissmann/eor"
|
14
|
+
gem.authors = ["Ben Weissmann"]
|
15
|
+
gem.add_development_dependency "rspec", ">= 0"
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
|
24
|
+
Spec::Rake::SpecTask.new do |t|
|
25
|
+
t.spec_files = FileList['spec/**/*.rb']
|
26
|
+
end
|
data/lib/eor.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Dir[File.join(File.dirname(__FILE__), 'eor', '**', '*.rb')].each {|f| require f}
|
2
|
+
|
3
|
+
class Proc
|
4
|
+
def eor *args, &block
|
5
|
+
args.unshift self
|
6
|
+
args.push block
|
7
|
+
return EOr.new.eor(args)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class EOr
|
12
|
+
def eor procs, args=[]
|
13
|
+
begin
|
14
|
+
return value_of procs.shift, args
|
15
|
+
rescue Exception => e
|
16
|
+
return eor(procs, args.push(e))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def value_of o, args
|
23
|
+
if o.kind_of? Proc
|
24
|
+
return o.call *args
|
25
|
+
else
|
26
|
+
return o
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/eor/version.rb
ADDED
data/spec/eor.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'lib/eor'
|
2
|
+
|
3
|
+
describe Proc, '#eor' do
|
4
|
+
it "should return the result of a successful Proc" do
|
5
|
+
proc{ 'hi' }.eor.should == 'hi'
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return nil for a failed Proc" do
|
9
|
+
proc{ fail }.eor.should be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return the result of a second proc if the first fails" do
|
13
|
+
proc{ fail }.eor(proc{ 10 }).should == 10
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should return the result of the first proc if it is successful, even if there are alternatives" do
|
17
|
+
proc { :foo }.eor(proc{ 10 }).should == :foo
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should accept a block" do
|
21
|
+
r = proc { not_a_method }.eor do
|
22
|
+
3.14
|
23
|
+
end
|
24
|
+
|
25
|
+
r.should == 3.14
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should accept a non-proc value" do
|
29
|
+
proc { not_a_method }.eor("foo").should == "foo"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return nil if two procs fail" do
|
33
|
+
proc { not_a_method }.eor(proc {10 / 0}).should be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should accept many procs" do
|
37
|
+
proc { not_a_method }.eor(proc { 10 / 0 }, proc { system }, 'foo').should == "foo"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should stop processing after a successful proc" do
|
41
|
+
proc { not_a_method }.eor(proc {10 / 0 }, proc { 'foo' }, proc { $foo = true }).should == "foo"
|
42
|
+
$foo.should be_nil
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should return the first good value" do
|
46
|
+
proc { not_a_method }.eor(proc {10 / 0 }, proc { 'foo' }, proc { " " }).should == "foo"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return nil for many bad values" do
|
50
|
+
proc { not_a_method }.eor(proc {10 / 0 }, proc { system }).should be_nil
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should provide the errors, in order" do
|
54
|
+
a, b, c = proc { not_a_method }.eor(proc {10 / 0 }, proc { system }, proc {|a, b, c| [a, b, c]})
|
55
|
+
|
56
|
+
a.should be_an_instance_of NameError
|
57
|
+
b.should be_an_instance_of ZeroDivisionError
|
58
|
+
c.should be_an_instance_of ArgumentError
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe EOr, "::EOR_VERSION" do
|
63
|
+
it "should be a string" do
|
64
|
+
EOr::EOR_VERSION.should be_an_instance_of String
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ben Weissmann
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-30 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: Proc#eor allows you to specify any number of Procs, and get the result of the first successful Proc
|
36
|
+
email: benweissmann@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- lib/eor.rb
|
51
|
+
- lib/eor/version.rb
|
52
|
+
- spec/eor.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/benweissmann/eor
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --charset=UTF-8
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: "Adds #eor to the Proc class"
|
87
|
+
test_files:
|
88
|
+
- spec/eor.rb
|