truther 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f4ef43222e0c4a36ad3b2cdd621f8a8370632fa4
4
+ data.tar.gz: ce30bfe4db2ef424cf991592f386c95c96df8ae8
5
+ SHA512:
6
+ metadata.gz: a4abb775f92e3128262749bee91bfd0940fb7591b663a2680ecb85fa66b56ff4cfc5cb8d30c382767320797078e5de00965e19908c409c4fcdc3e5cd14374f9a
7
+ data.tar.gz: 65099bab315c6991c38b409856cf93c28a6e8eb066e6ae320b4aff82f82c4e9d7208e9026a2ec34aae64c6d66b2b4666a4b191595ccb37e6196c4cd5f1c8238b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in truthy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Stephane Bisson
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Truther
2
+
3
+ Easy convertion to boolean from truthy and falsy values.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'truthy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install truthy
18
+
19
+ ## Usage
20
+
21
+ Use `#to_b` on `String`, `Integer`, `Float`, `nil`, `true` or `false`.
22
+
23
+ 'true'.to_b == true
24
+
25
+ 'no'.to_b == false
26
+
27
+ Check `Truthy::TRUTHY_STRINGS` and `Truthy::FALSY_STRINGS` to find out how strings are mapped to boolean.
28
+
29
+ An unrecognized string will raise `Truthy::NeitherTrueNoFalseError` unless a default value is provided. In which case the default value will be returned.
30
+
31
+ 'maybe'.to_b(:i_dont_know) == :i_dont_know
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( http://github.com/stephanebisson/truthy/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/truthy.rb ADDED
@@ -0,0 +1,56 @@
1
+ require "truthy/version"
2
+
3
+ module Truthy
4
+ TRUTHY_STRINGS = ['true', '1', 'yes', 'y', 'oui', 'vrai']
5
+ FALSY_STRINGS = ['false', '0', 'no', 'n', 'non', 'faux']
6
+
7
+ class NeitherTrueNoFalseError < StandardError
8
+ def initialize(str)
9
+ super("'#{str}' is not recognized as truthy or falsy.")
10
+ end
11
+ end
12
+ end
13
+
14
+ class String
15
+ def to_b(when_not_recognized=:raise)
16
+ sanitized_string = self.downcase.strip
17
+ return true if Truthy::TRUTHY_STRINGS.include? sanitized_string
18
+ return false if Truthy::FALSY_STRINGS.include? sanitized_string
19
+
20
+ if when_not_recognized == :raise
21
+ raise Truthy::NeitherTrueNoFalseError.new sanitized_string
22
+ else
23
+ when_not_recognized
24
+ end
25
+ end
26
+ end
27
+
28
+ class NilClass
29
+ def to_b
30
+ false
31
+ end
32
+ end
33
+
34
+ class Integer
35
+ def to_b
36
+ self > 0
37
+ end
38
+ end
39
+
40
+ class Float
41
+ def to_b
42
+ self > 0.0
43
+ end
44
+ end
45
+
46
+ class TrueClass
47
+ def to_b
48
+ true
49
+ end
50
+ end
51
+
52
+ class FalseClass
53
+ def to_b
54
+ false
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module Truthy
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,75 @@
1
+ require 'truthy'
2
+
3
+ describe :to_b do
4
+
5
+ context 'on string' do
6
+
7
+ it 'converts to true' do
8
+ 'true'.to_b.should be_true
9
+ '1'.to_b.should be_true
10
+ 'yes '.to_b.should be_true
11
+ 'Y'.to_b.should be_true
12
+ 'oui'.to_b.should be_true
13
+ 'vrai'.to_b.should be_true
14
+ end
15
+
16
+ it 'converts to false' do
17
+ 'false'.to_b.should be_false
18
+ '0'.to_b.should be_false
19
+ ' NO '.to_b.should be_false
20
+ 'non'.to_b.should be_false
21
+ 'faux'.to_b.should be_false
22
+ 'n'.to_b.should be_false
23
+ end
24
+
25
+ it 'raises error on unrecognized strings' do
26
+ expect {'asdf'.to_b}.to raise_error Truthy::NeitherTrueNoFalseError
27
+ end
28
+
29
+ it 'returns default on unrecognized strings' do
30
+ 'asdf'.to_b(:something_else).should == :something_else
31
+ end
32
+
33
+ end
34
+
35
+ context 'on nil' do
36
+ it 'converts to false' do
37
+ nil.to_b.should be_false
38
+ end
39
+ end
40
+
41
+ context 'on integer' do
42
+ it 'converts to true' do
43
+ 1.to_b.should be_true
44
+ 99.to_b.should be_true
45
+ end
46
+
47
+ it 'converts to false' do
48
+ 0.to_b.should be_false
49
+ -7.to_b.should be_false
50
+ end
51
+ end
52
+
53
+ context 'on float' do
54
+ it 'converts to true' do
55
+ 1.1.to_b.should be_true
56
+ 9.9.to_b.should be_true
57
+ end
58
+
59
+ it 'converts to false' do
60
+ 0.0.to_b.should be_false
61
+ -7.7.to_b.should be_false
62
+ end
63
+ end
64
+
65
+ context 'no-op on boolean' do
66
+ it 'converts to true' do
67
+ true.to_b.should be_true
68
+ end
69
+
70
+ it 'converts to false' do
71
+ false.to_b.should be_false
72
+ end
73
+ end
74
+
75
+ end
data/truthy.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'truthy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "truther"
8
+ spec.version = Truthy::VERSION
9
+ spec.authors = ["Stephane Bisson"]
10
+ spec.email = ["stephane.c.bisson@gmail.com"]
11
+ spec.summary = "Converts many strings to their boolean value."
12
+ spec.description = "Converts true, t, yes, y, 1, oui, etc to boolean true."
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.14"
24
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: truther
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephane Bisson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.14'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.14'
55
+ description: Converts true, t, yes, y, 1, oui, etc to boolean true.
56
+ email:
57
+ - stephane.c.bisson@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/truthy.rb
68
+ - lib/truthy/version.rb
69
+ - spec/truthy_spec.rb
70
+ - truthy.gemspec
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.1.11
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Converts many strings to their boolean value.
95
+ test_files:
96
+ - spec/truthy_spec.rb