boolean 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -cfs
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source :rubygems
2
+
3
+ group :development do
4
+ # FRAMEWORK
5
+ gem 'jeweler'
6
+
7
+ # SPECS
8
+ gem 'rspec'
9
+
10
+ # DOCS
11
+ gem 'yard'
12
+ gem 'RedCloth', require: nil
13
+ end
@@ -0,0 +1,29 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ RedCloth (4.2.7)
5
+ diff-lcs (1.1.2)
6
+ git (1.2.5)
7
+ jeweler (1.5.2)
8
+ bundler (~> 1.0.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rake (0.8.7)
12
+ rspec (2.5.0)
13
+ rspec-core (~> 2.5.0)
14
+ rspec-expectations (~> 2.5.0)
15
+ rspec-mocks (~> 2.5.0)
16
+ rspec-core (2.5.1)
17
+ rspec-expectations (2.5.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.5.0)
20
+ yard (0.6.4)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ RedCloth
27
+ jeweler
28
+ rspec
29
+ yard
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Tim Morgan
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.
@@ -0,0 +1,60 @@
1
+ h1. Boolean -- Additional Boolean-related core extensions
2
+
3
+ | *Author* | Tim Morgan |
4
+ | *Version* | 1.0 (Feb 15, 2011) |
5
+ | *License* | Released under the MIT license. |
6
+
7
+ h2. About
8
+
9
+ *Boolean* adds some helpful methods for working with Ruby's Boolean types,
10
+ @TrueClass@ and @FalseClass@ (the singleton classes whose only instances are
11
+ @true@ and @false@, respectively).
12
+
13
+ With *Boolean*, you get a @Boolean@ mixin so you can refer to @true@ and @false@
14
+ under a common class name:
15
+
16
+ <pre><code>
17
+ if variable.kind_of?(Boolean) then
18
+ [ ... ]
19
+ end
20
+ </code></pre>
21
+
22
+ You can also type-cast Ruby objects into their Boolean values:
23
+
24
+ <pre><code>
25
+ "string".to_bool #=> true
26
+ nil.to_bool #=> false
27
+ </code></pre>
28
+
29
+ And you can parse various Ruby objects to Booleans:
30
+
31
+ <pre><code>
32
+ "yes".parse_bool #=> true
33
+ "no".parse_bool #=> false
34
+ 1.parse_bool => true
35
+ 0.parse_bool => false
36
+ </code></pre>
37
+
38
+ (@parse_bool@ is also aliased as @to_b@ to be consistent with the
39
+ @to_i@/@to_int@ naming paradigm.)
40
+
41
+ Lastly, inline with the @Integer()@ method, you have a @Boolean()@ method:
42
+
43
+ <pre><code>
44
+ Boolean("yes") #=> true
45
+ Boolean("no") #=> false
46
+ Boolean("maybe") #=> ArgumentError
47
+ </code></pre>
48
+
49
+ h2. Installation and Usage
50
+
51
+ Just add the gem to your project's @Gemfile@:
52
+
53
+ <pre><code>
54
+ gem 'boolean'
55
+ </code></pre>
56
+
57
+ All the features shown in the previous section are now available in your project
58
+ code.
59
+
60
+ More information can be found in the class and method documentation.
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+
3
+ #################################### BUNDLER ###################################
4
+
5
+ require 'bundler'
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+ require 'rake'
14
+
15
+ #################################### JEWELER ###################################
16
+
17
+ require 'jeweler'
18
+ Jeweler::Tasks.new do |gem|
19
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
20
+ gem.name = "boolean"
21
+ gem.homepage = "http://rubygems.org/gems/boolean"
22
+ gem.license = "MIT"
23
+ gem.summary = %Q{Useful methods for working with Booleans}
24
+ gem.description = %Q{This gem extends core classes, adding helpful methods for working with Booleans (such as #to_bool and #parse_bool, and a Boolean type).}
25
+ gem.email = "git@timothymorgan.info"
26
+ gem.authors = [ "Tim Morgan" ]
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ ##################################### RSPEC ####################################
31
+
32
+ require 'rspec/core'
33
+ require 'rspec/core/rake_task'
34
+ RSpec::Core::RakeTask.new(:spec) do |spec|
35
+ spec.pattern = FileList['spec/**/*_spec.rb']
36
+ end
37
+
38
+ task :default => :spec
39
+
40
+ ##################################### YARD #####################################
41
+
42
+ require 'yard'
43
+ YARD::Rake::YardocTask.new do |doc|
44
+ doc.options << "-m" << "textile"
45
+ doc.options << "--protected"
46
+ doc.options << "--no-private"
47
+ doc.options << "-r" << "README.textile"
48
+ doc.options << "-o" << "doc"
49
+ doc.options << "--title" << "Boolean Documentation".inspect
50
+
51
+ doc.files = [ 'lib/**/*', 'README.textile' ]
52
+ end
53
+
54
+ desc "Generate API documentation"
55
+ task :doc => :yard
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,63 @@
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{boolean}
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tim Morgan"]
12
+ s.date = %q{2011-02-15}
13
+ s.description = %q{This gem extends core classes, adding helpful methods for working with Booleans (such as #to_bool and #parse_bool, and a Boolean type).}
14
+ s.email = %q{git@timothymorgan.info}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".rspec",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.textile",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "boolean.gemspec",
28
+ "lib/boolean.rb",
29
+ "spec/boolean_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://rubygems.org/gems/boolean}
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.5.2}
36
+ s.summary = %q{Useful methods for working with Booleans}
37
+ s.test_files = [
38
+ "spec/boolean_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
47
+ s.add_development_dependency(%q<rspec>, [">= 0"])
48
+ s.add_development_dependency(%q<yard>, [">= 0"])
49
+ s.add_development_dependency(%q<RedCloth>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<jeweler>, [">= 0"])
52
+ s.add_dependency(%q<rspec>, [">= 0"])
53
+ s.add_dependency(%q<yard>, [">= 0"])
54
+ s.add_dependency(%q<RedCloth>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<jeweler>, [">= 0"])
58
+ s.add_dependency(%q<rspec>, [">= 0"])
59
+ s.add_dependency(%q<yard>, [">= 0"])
60
+ s.add_dependency(%q<RedCloth>, [">= 0"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,124 @@
1
+ # Mixin module that adds no features, but simply includes a @Boolean@ type in
2
+ # the parent class's heirarchy. This allows you to do things like:
3
+ #
4
+ # <pre><code>
5
+ # if variable.kind_of?(Boolean) then
6
+ # [ ... ]
7
+ # end
8
+ # </code></pre>
9
+
10
+ module Boolean; end
11
+
12
+ class TrueClass
13
+ include Boolean
14
+
15
+ # Returns the _typecasted_ value of this object.
16
+ #
17
+ # @return [true] @true@.
18
+ def to_bool() true end
19
+
20
+ # Returns the _parsed_ value of this object.
21
+ #
22
+ # @return [true] @true@.
23
+ def parse_bool() true end
24
+
25
+ # @see #parse_bool
26
+ def to_b() true end
27
+ end
28
+
29
+ class FalseClass
30
+ include Boolean
31
+
32
+ # Returns the _typecasted_ value of this object.
33
+ #
34
+ # @return [false] @false@.
35
+ def to_bool() false end
36
+
37
+ # Returns the _parsed_ value of this object.
38
+ #
39
+ # @return [false] @false@.
40
+ def parse_bool() false end
41
+
42
+ # @see #parse_bool
43
+ def to_b() false end
44
+ end
45
+
46
+ class NilClass
47
+ # Returns the _typecasted_ value of this object.
48
+ #
49
+ # @return [false] @false@.
50
+ def to_bool() false end
51
+
52
+ # Returns the _parsed_ value of this object.
53
+ #
54
+ # @return [false] @false@.
55
+ def parse_bool() false end
56
+
57
+ # @see #parse_bool
58
+ def to_b() false end
59
+ end
60
+
61
+ class Object
62
+ # Returns the _typecasted_ value of this object. This would be @true@ for all
63
+ # objects except @false@ and @nil@, which type-cast to @false@.
64
+ #
65
+ # @return [true, false] The typecast value of this object.
66
+ def to_bool() true end
67
+ end
68
+
69
+ class String
70
+ # Returns the _parsed_ value of this object. Strings beginning with any of
71
+ # "y", "t", or "1" are considered @true@, whereas all else are considered
72
+ # @false@.
73
+ #
74
+ # @return [true, false] The parsed Boolean value of this string.
75
+ # @see #parse_bool!
76
+ def parse_bool() %w( y Y 1 t T ).include? self[0] end
77
+
78
+ # @see #parse_bool
79
+ def to_b() parse_bool end
80
+
81
+ # Similar to {#parse_bool}, but raises an error unless the string can be
82
+ # explicitly parsed to @true@ or @false@. Strings beginning with "n", "f", or
83
+ # "0" are considered false.
84
+ #
85
+ # @return [true, false] The parsed Boolean value of this string.
86
+ # @raise [ArgumentError] If the string does not seem to represent @true@ or
87
+ # @false@.
88
+ # @example
89
+ # "true".parse_bool! #=> true
90
+ # "no".parse_bool! #=> false
91
+ # "maybe".parse_bool! #=> ArgumentError
92
+ def parse_bool!
93
+ if %w( y Y 1 t T ).include? self[0] then
94
+ true
95
+ elsif %w( n N 0 f F ).include? self[0] then
96
+ false
97
+ else
98
+ raise ArgumentError, "Invalid value for parse_bool!: #{inspect}"
99
+ end
100
+ end
101
+
102
+ # @see #parse_bool!
103
+ def to_b!() parse_bool! end
104
+ end
105
+
106
+ module Kernel
107
+ # @see String#parse_bool!
108
+ def Boolean(string)
109
+ string.parse_bool!
110
+ rescue ArgumentError => err
111
+ raise ArgumentError, err.message.gsub('parse_bool!', 'Boolean()')
112
+ end
113
+ end
114
+
115
+ class Numeric
116
+ # Returns the _parsed_ value of this object. Numbers equal to zero are
117
+ # considered @false@; all others are considered @true@.
118
+ #
119
+ # @return [true, false] The parsed Boolean value of this number.
120
+ def parse_bool() not zero? end
121
+
122
+ # @see #parse_bool
123
+ def to_b() not zero? end
124
+ end
@@ -0,0 +1,156 @@
1
+ require 'spec_helper'
2
+
3
+ describe TrueClass do
4
+ [ :to_bool, :parse_bool, :to_b ].each do |method|
5
+ describe "##{method}" do
6
+ it("should return true") { true.send(method).should eql(true) }
7
+ end
8
+ end
9
+
10
+ it("should be a kind of Boolean") { true.should be_kind_of(Boolean) }
11
+ end
12
+
13
+ describe FalseClass do
14
+ [ :to_bool, :parse_bool, :to_b ].each do |method|
15
+ describe "##{method}" do
16
+ it("should return false") { false.send(method).should eql(false) }
17
+ end
18
+ end
19
+
20
+ it("should be a kind of Boolean") { false.should be_kind_of(Boolean) }
21
+ end
22
+
23
+ describe NilClass do
24
+ [ :to_bool, :parse_bool, :to_b ].each do |method|
25
+ describe "##{method}" do
26
+ it("should return false") { nil.send(method).should eql(false) }
27
+ end
28
+ end
29
+ end
30
+
31
+ describe Object do
32
+ describe "#to_bool" do
33
+ it("should return true") { Object.new.to_bool.should eql(true) }
34
+ end
35
+
36
+ it("should not be a kind of Boolean") { Object.new.should_not be_kind_of(Boolean) }
37
+ end
38
+
39
+ describe Numeric do
40
+ [ :parse_bool, :to_b ].each do |method|
41
+ describe "##{method}" do
42
+ it "should return true if the number is not zero" do
43
+ 1.send(method).should eql(true)
44
+ 1.0.send(method).should eql(true)
45
+ -1.send(method).should eql(true)
46
+ -1.0.send(method).should eql(true)
47
+ end
48
+
49
+ it "should return false if the number is zero" do
50
+ 0.send(method).should eql(false)
51
+ 0.0.send(method).should eql(false)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ describe String do
58
+ [ :parse_bool, :to_b ].each do |method|
59
+ describe "##{method}" do
60
+ it "should return true if the string starts with 1, y, Y, t, or T" do
61
+ "y".send(method).should eql(true)
62
+ "Y".send(method).should eql(true)
63
+ "yes".send(method).should eql(true)
64
+ "YES".send(method).should eql(true)
65
+ "Yes".send(method).should eql(true)
66
+ "t".send(method).should eql(true)
67
+ "T".send(method).should eql(true)
68
+ "true".send(method).should eql(true)
69
+ "TRUE".send(method).should eql(true)
70
+ "True".send(method).should eql(true)
71
+ "1".send(method).should eql(true)
72
+ end
73
+
74
+ it "should return false otherwise" do
75
+ "n".send(method).should eql(false)
76
+ "N".send(method).should eql(false)
77
+ "no".send(method).should eql(false)
78
+ "NO".send(method).should eql(false)
79
+ "No".send(method).should eql(false)
80
+ "f".send(method).should eql(false)
81
+ "F".send(method).should eql(false)
82
+ "false".send(method).should eql(false)
83
+ "FALSE".send(method).should eql(false)
84
+ "False".send(method).should eql(false)
85
+ "0".send(method).should eql(false)
86
+
87
+ "m".send(method).should eql(false)
88
+ "maybe".send(method).should eql(false)
89
+ "MAYBE".send(method).should eql(false)
90
+ "Maybe".send(method).should eql(false)
91
+ "i".send(method).should eql(false)
92
+ "I".send(method).should eql(false)
93
+ "i don't know".send(method).should eql(false)
94
+ "I DON'T KNOW".send(method).should eql(false)
95
+ "I don't know".send(method).should eql(false)
96
+ end
97
+ end
98
+ end
99
+
100
+ [ :parse_bool!, :to_b! ].each do |method|
101
+ describe "##{method}" do
102
+ it "should return true if the string starts with 1, y, Y, t, or T" do
103
+ "y".send(method).should eql(true)
104
+ "Y".send(method).should eql(true)
105
+ "yes".send(method).should eql(true)
106
+ "YES".send(method).should eql(true)
107
+ "Yes".send(method).should eql(true)
108
+ "t".send(method).should eql(true)
109
+ "T".send(method).should eql(true)
110
+ "true".send(method).should eql(true)
111
+ "TRUE".send(method).should eql(true)
112
+ "True".send(method).should eql(true)
113
+ "1".send(method).should eql(true)
114
+ end
115
+
116
+ it "should return false if the string starts with 0, n, N, f, or F" do
117
+ "n".send(method).should eql(false)
118
+ "N".send(method).should eql(false)
119
+ "no".send(method).should eql(false)
120
+ "NO".send(method).should eql(false)
121
+ "No".send(method).should eql(false)
122
+ "f".send(method).should eql(false)
123
+ "F".send(method).should eql(false)
124
+ "false".send(method).should eql(false)
125
+ "FALSE".send(method).should eql(false)
126
+ "False".send(method).should eql(false)
127
+ "0".send(method).should eql(false)
128
+ end
129
+
130
+ it "should raise ArgumentError otherwise" do
131
+ -> { "m".send(method) }.should raise_error(ArgumentError)
132
+ -> { "maybe".send(method) }.should raise_error(ArgumentError)
133
+ -> { "MAYBE".send(method) }.should raise_error(ArgumentError)
134
+ -> { "Maybe".send(method) }.should raise_error(ArgumentError)
135
+ -> { "i".send(method) }.should raise_error(ArgumentError)
136
+ -> { "I".send(method) }.should raise_error(ArgumentError)
137
+ -> { "i don't know".send(method) }.should raise_error(ArgumentError)
138
+ -> { "I DON'T KNOW".send(method) }.should raise_error(ArgumentError)
139
+ -> { "I don't know".send(method) }.should raise_error(ArgumentError)
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ describe Kernel do
146
+ describe "#Boolean" do
147
+ it "should call String#parse_bool! and return the result" do
148
+ string = mock('String')
149
+ result = mock('result')
150
+
151
+ string.should_receive(:parse_bool!).once.and_return(result)
152
+
153
+ Boolean(string).should eql(result)
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'boolean'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
9
+
10
+ RSpec.configure do |config|
11
+ # configuration here
12
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boolean
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Tim Morgan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-15 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: jeweler
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: yard
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: RedCloth
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: *id004
60
+ description: "This gem extends core classes, adding helpful methods for working with Booleans (such as #to_bool and #parse_bool, and a Boolean type)."
61
+ email: git@timothymorgan.info
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE.txt
68
+ - README.textile
69
+ files:
70
+ - .rspec
71
+ - Gemfile
72
+ - Gemfile.lock
73
+ - LICENSE.txt
74
+ - README.textile
75
+ - Rakefile
76
+ - VERSION
77
+ - boolean.gemspec
78
+ - lib/boolean.rb
79
+ - spec/boolean_spec.rb
80
+ - spec/spec_helper.rb
81
+ has_rdoc: true
82
+ homepage: http://rubygems.org/gems/boolean
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 4093550238213875755
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: "0"
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.5.2
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Useful methods for working with Booleans
112
+ test_files:
113
+ - spec/boolean_spec.rb
114
+ - spec/spec_helper.rb