lotus-utils 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +2 -1
- data/CHANGELOG.md +39 -0
- data/CONTRIBUTING.md +44 -0
- data/Gemfile +3 -3
- data/{LICENSE.txt → LICENSE.md} +0 -0
- data/README.md +9 -7
- data/Rakefile +7 -0
- data/lib/lotus/utils.rb +1 -1
- data/lib/lotus/utils/class_attribute.rb +2 -0
- data/lib/lotus/utils/kernel.rb +773 -0
- data/lib/lotus/utils/version.rb +1 -1
- data/test/kernel_test.rb +1752 -0
- data/test/test_helper.rb +13 -8
- data/test/version_test.rb +1 -1
- metadata +9 -5
- data/.coveralls.yml +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6a206acbf5c8f3f36aecb39a8a834faba7f40d9
|
4
|
+
data.tar.gz: fdeea0a0daf779be52a3f17a62460dbc5dd30f53
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bfe9cc4e5ef901a898905aff9eea8ffa39c56b0e5fce80c8d40e6493d7a5c15ad3189bec6ebebe50b87185b78a66189f38b45e299df3a258253bfce182e4d854
|
7
|
+
data.tar.gz: d928d2cf1dc04a209144be6e03d9f25a1a4f064d079a809f82cd15877858b298a106ab378f025f14f09488cc78fad9ef1c352275af3febf46ecae6b189158dac
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
## v0.1.1
|
2
|
+
### Apr 23, 2014
|
3
|
+
|
4
|
+
c125e0c _2014-04-18_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Time
|
5
|
+
|
6
|
+
b366672 _2014-04-18_ **Luca Guidi** Implemented Lotus::Utils::Kernel.DateTime
|
7
|
+
|
8
|
+
37a010f _2014-04-17_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Date
|
9
|
+
|
10
|
+
484ec9d _2014-04-17_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Float
|
11
|
+
|
12
|
+
69dd6eb _2014-04-16_ **Christopher Keele** Add missing stdlib Set require to ClassAttribute.
|
13
|
+
|
14
|
+
82c0f54 _2014-04-12_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Boolean
|
15
|
+
|
16
|
+
48ba6fb _2014-04-11_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Hash
|
17
|
+
|
18
|
+
c10d561 _2014-04-11_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Set
|
19
|
+
|
20
|
+
5582e72 _2014-04-11_ **Karl Freeman** add Github contributing guidelines
|
21
|
+
|
22
|
+
012f266 _2014-04-11_ **Luca Guidi** Implemented Lotus::Utils::Kernel.String
|
23
|
+
|
24
|
+
e1a35e2 _2014-04-08_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Integer
|
25
|
+
|
26
|
+
152c856 _2014-04-08_ **Luca Guidi** Implemented Lotus::Utils::Kernel.Array
|
27
|
+
|
28
|
+
## v0.1.0
|
29
|
+
### Jan 23, 2014
|
30
|
+
|
31
|
+
1212e9d _2014-01-16_ **Luca Guidi** Introduced Lotus::Utils::String#demodulize
|
32
|
+
|
33
|
+
f9badc2 _2014-01-16_ **Luca Guidi** Fix for ClassAttribute: ensure class attributes to be inherited even the subclass is defined into a different namespace
|
34
|
+
|
35
|
+
c19d26b _2014-01-07_ **Luca Guidi** Introducing Lotus::Utils::IO
|
36
|
+
|
37
|
+
0453716 _2013-08-08_ **Luca Guidi** Added Class loading mechanism
|
38
|
+
|
39
|
+
272304b _2013-08-07_ **Luca Guidi** Initial mess
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
## Contributing
|
2
|
+
In the spirit of [free software][free-sw], **everyone** is encouraged to help
|
3
|
+
improve this project.
|
4
|
+
|
5
|
+
[free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
|
6
|
+
|
7
|
+
Here are some ways *you* can contribute:
|
8
|
+
|
9
|
+
* by using alpha, beta, and prerelease versions
|
10
|
+
* by reporting bugs
|
11
|
+
* by suggesting new features
|
12
|
+
* by writing or editing documentation
|
13
|
+
* by writing specifications
|
14
|
+
* by writing code (**no patch is too small**: fix typos, add comments, clean up
|
15
|
+
inconsistent whitespace)
|
16
|
+
* by refactoring code
|
17
|
+
* by closing [issues][]
|
18
|
+
* by reviewing patches
|
19
|
+
|
20
|
+
[issues]: https://github.com/lotus/utils/issues
|
21
|
+
|
22
|
+
## Submitting an Issue
|
23
|
+
We use the [GitHub issue tracker][issues] to track bugs and features. Before
|
24
|
+
submitting a bug report or feature request, check to make sure it hasn't
|
25
|
+
already been submitted. When submitting a bug report, please include a [Gist][]
|
26
|
+
that includes a stack trace and any details that may be necessary to reproduce
|
27
|
+
the bug, including your gem version, Ruby version, and operating system.
|
28
|
+
Ideally, a bug report should include a pull request with failing specs.
|
29
|
+
|
30
|
+
[gist]: https://gist.github.com/
|
31
|
+
|
32
|
+
## Submitting a Pull Request
|
33
|
+
1. [Fork the repository.][fork]
|
34
|
+
2. [Create a topic branch.][branch]
|
35
|
+
3. Add specs for your unimplemented feature or bug fix.
|
36
|
+
4. Run `bundle exec rake`. If your specs pass, return to step 3.
|
37
|
+
5. Implement your feature or bug fix.
|
38
|
+
6. Run `bundle exec rake`. If your specs fail, return to step 5.
|
39
|
+
7. Add, commit, and push your changes.
|
40
|
+
8. [Submit a pull request.][pr]
|
41
|
+
|
42
|
+
[fork]: http://help.github.com/fork-a-repo/
|
43
|
+
[branch]: http://learn.github.com/p/branching.html
|
44
|
+
[pr]: http://help.github.com/send-pull-requests/
|
data/Gemfile
CHANGED
@@ -2,9 +2,9 @@ source 'https://rubygems.org'
|
|
2
2
|
gemspec
|
3
3
|
|
4
4
|
unless ENV['TRAVIS']
|
5
|
-
gem '
|
6
|
-
gem 'yard',
|
7
|
-
gem 'simplecov', require: false
|
5
|
+
gem 'byebug', require: false, platforms: :ruby if RUBY_VERSION == '2.1.1'
|
6
|
+
gem 'yard', require: false
|
8
7
|
end
|
9
8
|
|
9
|
+
gem 'simplecov', require: false
|
10
10
|
gem 'coveralls', require: false
|
data/{LICENSE.txt → LICENSE.md}
RENAMED
File without changes
|
data/README.md
CHANGED
@@ -4,11 +4,12 @@ Ruby core extentions and class utilities for [Lotus](http://lotusrb.org)
|
|
4
4
|
|
5
5
|
## Status
|
6
6
|
|
7
|
-
[![Gem Version](
|
8
|
-
[![Build Status](
|
9
|
-
[![
|
10
|
-
[![Code
|
11
|
-
[![Dependencies](
|
7
|
+
[![Gem Version](http://img.shields.io/gem/v/lotus-utils.svg)](https://badge.fury.io/rb/lotus-utils)
|
8
|
+
[![Build Status](http://img.shields.io/travis/lotus/utils/master.svg)](https://travis-ci.org/lotus/utils?branch=master)
|
9
|
+
[![Coverage](http://img.shields.io/coveralls/lotus/utils/master.svg)](https://coveralls.io/r/lotus/utils)
|
10
|
+
[![Code Climate](http://img.shields.io/codeclimate/github/lotus/utils.svg)](https://codeclimate.com/github/lotus/utils)
|
11
|
+
[![Dependencies](http://img.shields.io/gemnasium/lotus/utils.svg)](https://gemnasium.com/lotus/utils)
|
12
|
+
[![Inline Docs](http://inch-pages.github.io/github/lotus/utils.svg)](http://inch-pages.github.io/github/lotus/utils)
|
12
13
|
|
13
14
|
## Contact
|
14
15
|
|
@@ -16,7 +17,8 @@ Ruby core extentions and class utilities for [Lotus](http://lotusrb.org)
|
|
16
17
|
* Mailing List: http://lotusrb.org/mailing-list
|
17
18
|
* API Doc: http://rdoc.info/gems/lotus-utils
|
18
19
|
* Bugs/Issues: https://github.com/lotus/utils/issues
|
19
|
-
* Support: http://stackoverflow.com/questions/tagged/
|
20
|
+
* Support: http://stackoverflow.com/questions/tagged/lotus-ruby
|
21
|
+
* Chat: https://gitter.im/lotus/chat
|
20
22
|
|
21
23
|
## Rubies
|
22
24
|
|
@@ -50,7 +52,7 @@ __Lotus::Utils__ uses [Semantic Versioning 2.0.0](http://semver.org)
|
|
50
52
|
|
51
53
|
## Contributing
|
52
54
|
|
53
|
-
1. Fork it
|
55
|
+
1. Fork it ( https://github.com/lotus/utils/fork )
|
54
56
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
55
57
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
56
58
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
data/lib/lotus/utils.rb
CHANGED
@@ -0,0 +1,773 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'date'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
module Lotus
|
6
|
+
module Utils
|
7
|
+
# Kernel utilities
|
8
|
+
# @since 0.1.1
|
9
|
+
module Kernel
|
10
|
+
# Coerces the argument to be an array.
|
11
|
+
#
|
12
|
+
# It's similar to Ruby's Kernel.Array, but it applies further
|
13
|
+
# transformations:
|
14
|
+
#
|
15
|
+
# * flatten
|
16
|
+
# * compact
|
17
|
+
# * uniq
|
18
|
+
#
|
19
|
+
# @param arg [Object] the input
|
20
|
+
#
|
21
|
+
# @return [Array] the result of the coercion
|
22
|
+
#
|
23
|
+
# @raise [NoMethodError] if arg doesn't implement #nil?
|
24
|
+
#
|
25
|
+
# @since 0.1.1
|
26
|
+
#
|
27
|
+
# @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-Array
|
28
|
+
#
|
29
|
+
# @see http://www.ruby-doc.org/core-2.1.1/Array.html#method-i-flatten
|
30
|
+
# @see http://www.ruby-doc.org/core-2.1.1/Array.html#method-i-compact
|
31
|
+
# @see http://www.ruby-doc.org/core-2.1.1/Array.html#method-i-uniq
|
32
|
+
#
|
33
|
+
# @example Basic Usage
|
34
|
+
# require 'lotus/utils/kernel'
|
35
|
+
#
|
36
|
+
# Lotus::Utils::Kernel.Array(nil) # => nil
|
37
|
+
# Lotus::Utils::Kernel.Array(true) # => [true]
|
38
|
+
# Lotus::Utils::Kernel.Array(false) # => [false]
|
39
|
+
# Lotus::Utils::Kernel.Array(1) # => [1]
|
40
|
+
# Lotus::Utils::Kernel.Array([1]) # => [1]
|
41
|
+
# Lotus::Utils::Kernel.Array([1, [2]]) # => [1,2]
|
42
|
+
# Lotus::Utils::Kernel.Array([1, [2, nil]]) # => [1,2]
|
43
|
+
# Lotus::Utils::Kernel.Array([1, [2, nil, 1]]) # => [1,2]
|
44
|
+
#
|
45
|
+
# @example Array Interface
|
46
|
+
# require 'lotus/utils/kernel'
|
47
|
+
#
|
48
|
+
# ResultSet = Struct.new(:records) do
|
49
|
+
# def to_a
|
50
|
+
# records.to_a.sort
|
51
|
+
# end
|
52
|
+
# end
|
53
|
+
#
|
54
|
+
# Response = Struct.new(:status, :headers, :body) do
|
55
|
+
# def to_ary
|
56
|
+
# [status, headers, body]
|
57
|
+
# end
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# set = ResultSet.new([2,1,3])
|
61
|
+
# Lotus::Utils::Kernel.Array(set) # => [1,2,3]
|
62
|
+
#
|
63
|
+
# response = Response.new(200, {}, 'hello')
|
64
|
+
# Lotus::Utils::Kernel.Array(response) # => [200, {}, "hello"]
|
65
|
+
def self.Array(arg)
|
66
|
+
super(arg).flatten.compact.uniq unless arg.nil?
|
67
|
+
end
|
68
|
+
|
69
|
+
# Coerces the argument to be a set.
|
70
|
+
#
|
71
|
+
# @param arg [Object] the input
|
72
|
+
#
|
73
|
+
# @return [Set] the result of the coercion
|
74
|
+
#
|
75
|
+
# @raise [NoMethodError] if arg doesn't implement #respond_to?
|
76
|
+
#
|
77
|
+
# @since 0.1.1
|
78
|
+
#
|
79
|
+
# @example Basic Usage
|
80
|
+
# require 'lotus/utils/kernel'
|
81
|
+
#
|
82
|
+
# Lotus::Utils::Kernel.Set(nil) # => nil
|
83
|
+
# Lotus::Utils::Kernel.Set(true) # => #<Set: {true}>
|
84
|
+
# Lotus::Utils::Kernel.Set(false) # => #<Set: {false}>
|
85
|
+
# Lotus::Utils::Kernel.Set(1) # => #<Set: {1}>
|
86
|
+
# Lotus::Utils::Kernel.Set([1]) # => #<Set: {1}>
|
87
|
+
# Lotus::Utils::Kernel.Set([1, 1]) # => #<Set: {1}>
|
88
|
+
# Lotus::Utils::Kernel.Set([1, [2]]) # => #<Set: {1, [2]}>
|
89
|
+
# Lotus::Utils::Kernel.Set([1, [2, nil]]) # => #<Set: {1, [2, nil]}>
|
90
|
+
# Lotus::Utils::Kernel.Set({a: 1}) # => #<Set: {[:a, 1]}>
|
91
|
+
#
|
92
|
+
# @example Set Interface
|
93
|
+
# require 'securerandom'
|
94
|
+
# require 'lotus/utils/kernel'
|
95
|
+
#
|
96
|
+
# UuidSet = Class.new do
|
97
|
+
# def initialize(*uuids)
|
98
|
+
# @uuids = uuids
|
99
|
+
# end
|
100
|
+
#
|
101
|
+
# def to_set
|
102
|
+
# Set.new.tap do |set|
|
103
|
+
# @uuids.each {|uuid| set.add(uuid) }
|
104
|
+
# end
|
105
|
+
# end
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# uuids = UuidSet.new(SecureRandom.uuid)
|
109
|
+
# Lotus::Utils::Kernel.Set(uuids)
|
110
|
+
# # => #<Set: {"daa798b4-630c-4e11-b29d-92f0b1c7d075"}>
|
111
|
+
#
|
112
|
+
# @example Unchecked Exceptions
|
113
|
+
# require 'lotus/utils/kernel'
|
114
|
+
#
|
115
|
+
# Lotus::Utils::Kernel.Set(BasicObject.new) # => NoMethodError
|
116
|
+
def self.Set(arg)
|
117
|
+
if arg.respond_to?(:to_set)
|
118
|
+
arg.to_set
|
119
|
+
else
|
120
|
+
Set.new(::Kernel.Array(arg))
|
121
|
+
end unless arg.nil?
|
122
|
+
end
|
123
|
+
|
124
|
+
# Coerces the argument to be an hash.
|
125
|
+
#
|
126
|
+
# @param arg [Object] the input
|
127
|
+
#
|
128
|
+
# @return [Hash, nil] the result of the coercion
|
129
|
+
#
|
130
|
+
# @raise [NoMethodError] if arg doesn't implement #nil?
|
131
|
+
# @raise [NoMethodError] if arg doesn't implement #respond_to?
|
132
|
+
# @raise [TypeError] if arg can't be coerced
|
133
|
+
#
|
134
|
+
# @since 0.1.1
|
135
|
+
#
|
136
|
+
# @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-Hash
|
137
|
+
#
|
138
|
+
# @example Basic Usage
|
139
|
+
# require 'lotus/utils/kernel'
|
140
|
+
#
|
141
|
+
# Lotus::Utils::Kernel.Hash(nil) # => nil
|
142
|
+
# Lotus::Utils::Kernel.Hash({a: 1}) # => { :a => 1 }
|
143
|
+
# Lotus::Utils::Kernel.Hash([[:a, 1]]) # => { :a => 1 }
|
144
|
+
# Lotus::Utils::Kernel.Hash(Set.new([[:a, 1]])) # => { :a => 1 }
|
145
|
+
#
|
146
|
+
# @example Hash Interface
|
147
|
+
# require 'lotus/utils/kernel'
|
148
|
+
#
|
149
|
+
# Room = Class.new do
|
150
|
+
# def initialize(*args)
|
151
|
+
# @args = args
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# def to_h
|
155
|
+
# Hash[*@args]
|
156
|
+
# end
|
157
|
+
# end
|
158
|
+
#
|
159
|
+
# Record = Class.new do
|
160
|
+
# def initialize(attributes = {})
|
161
|
+
# @attributes = attributes
|
162
|
+
# end
|
163
|
+
#
|
164
|
+
# def to_hash
|
165
|
+
# @attributes
|
166
|
+
# end
|
167
|
+
# end
|
168
|
+
#
|
169
|
+
# room = Room.new(:key, 123456)
|
170
|
+
# Lotus::Utils::Kernel.Hash(room) # => { :key => 123456 }
|
171
|
+
#
|
172
|
+
# record = Record.new(name: 'L')
|
173
|
+
# Lotus::Utils::Kernel.Hash(record) # => { :name => "L" }
|
174
|
+
#
|
175
|
+
# @example Unchecked Exceptions
|
176
|
+
# require 'lotus/utils/kernel'
|
177
|
+
#
|
178
|
+
# input = BasicObject.new
|
179
|
+
# Lotus::Utils::Kernel.Hash(input) # => NoMethodError
|
180
|
+
if RUBY_VERSION >= '2.1'
|
181
|
+
def self.Hash(arg)
|
182
|
+
if arg.respond_to?(:to_h)
|
183
|
+
arg.to_h
|
184
|
+
else
|
185
|
+
super(arg)
|
186
|
+
end unless arg.nil?
|
187
|
+
end
|
188
|
+
else
|
189
|
+
def self.Hash(arg)
|
190
|
+
case arg
|
191
|
+
when NilClass then nil
|
192
|
+
when ::Hash then arg
|
193
|
+
when ::Array, ::Set then Hash[*self.Array(arg)]
|
194
|
+
when ->(a) { a.respond_to?(:to_h) } then arg.to_h
|
195
|
+
else
|
196
|
+
super(arg)
|
197
|
+
end
|
198
|
+
rescue ArgumentError
|
199
|
+
raise TypeError
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
# Coerces the argument to be an integer.
|
204
|
+
#
|
205
|
+
# It's similar to Ruby's Kernel.Integer, but it doesn't stop at the first
|
206
|
+
# error and tries to be less "whiny".
|
207
|
+
#
|
208
|
+
# @param arg [Object] the argument
|
209
|
+
#
|
210
|
+
# @return [Fixnum,nil] the result of the coercion
|
211
|
+
#
|
212
|
+
# @raise [TypeError] if the argument can't be coerced
|
213
|
+
# @raise [NoMethodError] if the argument doesn't implenent #nil?
|
214
|
+
# @raise [TypeError,FloatDomainError,RangeError] if the argument it's too
|
215
|
+
# big.
|
216
|
+
#
|
217
|
+
# @since 0.1.1
|
218
|
+
#
|
219
|
+
# @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-Integer
|
220
|
+
#
|
221
|
+
# @example Basic Usage
|
222
|
+
# require 'bigdecimal'
|
223
|
+
# require 'lotus/utils/kernel'
|
224
|
+
#
|
225
|
+
# Lotus::Utils::Kernel.Integer(1) # => 1
|
226
|
+
# Lotus::Utils::Kernel.Integer(1.2) # => 1
|
227
|
+
# Lotus::Utils::Kernel.Integer(011) # => 9
|
228
|
+
# Lotus::Utils::Kernel.Integer(0xf5) # => 245
|
229
|
+
# Lotus::Utils::Kernel.Integer("1") # => 1
|
230
|
+
# Lotus::Utils::Kernel.Integer(Rational(0.3)) # => 0
|
231
|
+
# Lotus::Utils::Kernel.Integer(Complex(0.3)) # => 0
|
232
|
+
# Lotus::Utils::Kernel.Integer(BigDecimal.new(12.00001)) # => 12
|
233
|
+
# Lotus::Utils::Kernel.Integer(176605528590345446089)
|
234
|
+
# # => 176605528590345446089
|
235
|
+
#
|
236
|
+
# Lotus::Utils::Kernel.Integer(Time.now) # => 1396947161
|
237
|
+
#
|
238
|
+
# @example Integer Interface
|
239
|
+
# require 'lotus/utils/kernel'
|
240
|
+
#
|
241
|
+
# UltimateAnswer = Struct.new(:question) do
|
242
|
+
# def to_int
|
243
|
+
# 42
|
244
|
+
# end
|
245
|
+
# end
|
246
|
+
#
|
247
|
+
# answer = UltimateAnswer.new('The Ultimate Question of Life')
|
248
|
+
# Lotus::Utils::Kernel.Integer(answer) # => 42
|
249
|
+
#
|
250
|
+
# @example Error Handling
|
251
|
+
# require 'lotus/utils/kernel'
|
252
|
+
#
|
253
|
+
# # nil
|
254
|
+
# Kernel.Integer(nil) # => TypeError
|
255
|
+
# Lotus::Utils::Kernel.Integer(nil) # => nil
|
256
|
+
#
|
257
|
+
# # float represented as a string
|
258
|
+
# Kernel.Integer("23.4") # => ArgumentError
|
259
|
+
# Lotus::Utils::Kernel.Integer("23.4") # => 23
|
260
|
+
#
|
261
|
+
# # rational represented as a string
|
262
|
+
# Kernel.Integer("2/3") # => ArgumentError
|
263
|
+
# Lotus::Utils::Kernel.Integer("2/3") # => 2
|
264
|
+
#
|
265
|
+
# # complex represented as a string
|
266
|
+
# Kernel.Integer("2.5/1") # => ArgumentError
|
267
|
+
# Lotus::Utils::Kernel.Integer("2.5/1") # => 2
|
268
|
+
#
|
269
|
+
# @example Unchecked Exceptions
|
270
|
+
# require 'date'
|
271
|
+
# require 'bigdecimal'
|
272
|
+
# require 'lotus/utils/kernel'
|
273
|
+
#
|
274
|
+
# # Missing #to_int and #to_i
|
275
|
+
# input = OpenStruct.new(color: 'purple')
|
276
|
+
# Lotus::Utils::Kernel.Integer(input) # => TypeError
|
277
|
+
#
|
278
|
+
# # When true
|
279
|
+
# input = true
|
280
|
+
# Lotus::Utils::Kernel.Integer(input) # => TypeError
|
281
|
+
#
|
282
|
+
# # When false
|
283
|
+
# input = false
|
284
|
+
# Lotus::Utils::Kernel.Integer(input) # => TypeError
|
285
|
+
#
|
286
|
+
# # When Date
|
287
|
+
# input = Date.today
|
288
|
+
# Lotus::Utils::Kernel.Integer(input) # => TypeError
|
289
|
+
#
|
290
|
+
# # When DateTime
|
291
|
+
# input = DateTime.now
|
292
|
+
# Lotus::Utils::Kernel.Integer(input) # => TypeError
|
293
|
+
#
|
294
|
+
# # Missing #nil?
|
295
|
+
# input = BasicObject.new
|
296
|
+
# Lotus::Utils::Kernel.Integer(input) # => NoMethodError
|
297
|
+
#
|
298
|
+
# # bigdecimal infinity
|
299
|
+
# input = BigDecimal.new("Infinity")
|
300
|
+
# Lotus::Utils::Kernel.Integer(input) # => FloatDomainError
|
301
|
+
#
|
302
|
+
# # bigdecimal NaN
|
303
|
+
# input = BigDecimal.new("NaN")
|
304
|
+
# Lotus::Utils::Kernel.Integer(input) # => FloatDomainError
|
305
|
+
#
|
306
|
+
# # big rational
|
307
|
+
# input = Rational(-8) ** Rational(1, 3)
|
308
|
+
# Lotus::Utils::Kernel.Integer(input) # => RangeError
|
309
|
+
#
|
310
|
+
# # big complex represented as a string
|
311
|
+
# input = Complex(2, 3)
|
312
|
+
# Lotus::Utils::Kernel.Integer(input) # => RangeError
|
313
|
+
def self.Integer(arg)
|
314
|
+
super(arg) unless arg.nil?
|
315
|
+
rescue ArgumentError
|
316
|
+
arg.to_i
|
317
|
+
end
|
318
|
+
|
319
|
+
# Coerces the argument to be a float.
|
320
|
+
#
|
321
|
+
# It's similar to Ruby's Kernel.Float, but it doesn't stop at the first
|
322
|
+
# error and tries to be less "whiny".
|
323
|
+
#
|
324
|
+
# @param arg [Object] the argument
|
325
|
+
#
|
326
|
+
# @return [Float,nil] the result of the coercion
|
327
|
+
#
|
328
|
+
# @raise [TypeError] if the argument can't be coerced
|
329
|
+
# @raise [NoMethodError] if the argument doesn't implenent #nil?
|
330
|
+
# @raise [TypeError,FloatDomainError,RangeError] if the argument it's too
|
331
|
+
# big.
|
332
|
+
#
|
333
|
+
# @since 0.1.1
|
334
|
+
#
|
335
|
+
# @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-Float
|
336
|
+
#
|
337
|
+
# @example Basic Usage
|
338
|
+
# require 'bigdecimal'
|
339
|
+
# require 'lotus/utils/kernel'
|
340
|
+
#
|
341
|
+
# Lotus::Utils::Kernel.Float(1) # => 1.0
|
342
|
+
# Lotus::Utils::Kernel.Float(1.2) # => 1.2
|
343
|
+
# Lotus::Utils::Kernel.Float(011) # => 9.0
|
344
|
+
# Lotus::Utils::Kernel.Float(0xf5) # => 245.0
|
345
|
+
# Lotus::Utils::Kernel.Float("1") # => 1.0
|
346
|
+
# Lotus::Utils::Kernel.Float(Rational(0.3)) # => 0.3
|
347
|
+
# Lotus::Utils::Kernel.Float(Complex(0.3)) # => 0.3
|
348
|
+
# Lotus::Utils::Kernel.Float(BigDecimal.new(12.00001)) # => 12.00001
|
349
|
+
# Lotus::Utils::Kernel.Float(176605528590345446089)
|
350
|
+
# # => 176605528590345446089.0
|
351
|
+
#
|
352
|
+
# Lotus::Utils::Kernel.Float(Time.now) # => 397750945.515169
|
353
|
+
#
|
354
|
+
# @example Float Interface
|
355
|
+
# require 'lotus/utils/kernel'
|
356
|
+
#
|
357
|
+
# class Pi
|
358
|
+
# def to_f
|
359
|
+
# 3.14
|
360
|
+
# end
|
361
|
+
# end
|
362
|
+
#
|
363
|
+
# pi = Pi.new
|
364
|
+
# Lotus::Utils::Kernel.Float(pi) # => 3.14
|
365
|
+
#
|
366
|
+
# @example Error Handling
|
367
|
+
# require 'bigdecimal'
|
368
|
+
# require 'lotus/utils/kernel'
|
369
|
+
#
|
370
|
+
# # nil
|
371
|
+
# Kernel.Float(nil) # => TypeError
|
372
|
+
# Lotus::Utils::Kernel.Float(nil) # => nil
|
373
|
+
#
|
374
|
+
# # float represented as a string
|
375
|
+
# Kernel.Float("23.4") # => ArgumentError
|
376
|
+
# Lotus::Utils::Kernel.Float("23.4") # => 23.4
|
377
|
+
#
|
378
|
+
# # rational represented as a string
|
379
|
+
# Kernel.Float("2/3") # => ArgumentError
|
380
|
+
# Lotus::Utils::Kernel.Float("2/3") # => 2.0
|
381
|
+
#
|
382
|
+
# # complex represented as a string
|
383
|
+
# Kernel.Float("2.5/1") # => ArgumentError
|
384
|
+
# Lotus::Utils::Kernel.Float("2.5/1") # => 2.5
|
385
|
+
#
|
386
|
+
# # bigdecimal infinity
|
387
|
+
# input = BigDecimal.new("Infinity")
|
388
|
+
# Lotus::Utils::Kernel.Float(input) # => Infinity
|
389
|
+
#
|
390
|
+
# # bigdecimal NaN
|
391
|
+
# input = BigDecimal.new("NaN")
|
392
|
+
# Lotus::Utils::Kernel.Float(input) # => NaN
|
393
|
+
#
|
394
|
+
# @example Unchecked Exceptions
|
395
|
+
# require 'date'
|
396
|
+
# require 'bigdecimal'
|
397
|
+
# require 'lotus/utils/kernel'
|
398
|
+
#
|
399
|
+
# # Missing #to_f
|
400
|
+
# input = OpenStruct.new(color: 'purple')
|
401
|
+
# Lotus::Utils::Kernel.Float(input) # => TypeError
|
402
|
+
#
|
403
|
+
# # When true
|
404
|
+
# input = true
|
405
|
+
# Lotus::Utils::Kernel.Float(input) # => TypeError
|
406
|
+
#
|
407
|
+
# # When false
|
408
|
+
# input = false
|
409
|
+
# Lotus::Utils::Kernel.Float(input) # => TypeError
|
410
|
+
#
|
411
|
+
# # When Date
|
412
|
+
# input = Date.today
|
413
|
+
# Lotus::Utils::Kernel.Float(input) # => TypeError
|
414
|
+
#
|
415
|
+
# # When DateTime
|
416
|
+
# input = DateTime.now
|
417
|
+
# Lotus::Utils::Kernel.Float(input) # => TypeError
|
418
|
+
#
|
419
|
+
# # Missing #nil?
|
420
|
+
# input = BasicObject.new
|
421
|
+
# Lotus::Utils::Kernel.Float(input) # => NoMethodError
|
422
|
+
#
|
423
|
+
# # big rational
|
424
|
+
# input = Rational(-8) ** Rational(1, 3)
|
425
|
+
# Lotus::Utils::Kernel.Float(input) # => RangeError
|
426
|
+
#
|
427
|
+
# # big complex represented as a string
|
428
|
+
# input = Complex(2, 3)
|
429
|
+
# Lotus::Utils::Kernel.Float(input) # => RangeError
|
430
|
+
def self.Float(arg)
|
431
|
+
super(arg) unless arg.nil?
|
432
|
+
rescue ArgumentError
|
433
|
+
arg.to_f
|
434
|
+
end
|
435
|
+
|
436
|
+
# Coerces the argument to be a string.
|
437
|
+
#
|
438
|
+
# It's similar to Ruby's Kernel.String, but it's less "whiny".
|
439
|
+
#
|
440
|
+
# @param arg [Object] the argument
|
441
|
+
#
|
442
|
+
# @return [String,nil] the result of the coercion
|
443
|
+
#
|
444
|
+
# @raise [TypeError] if the argument can't be coerced
|
445
|
+
# @raise [NoMethodError] if the argument doesn't implement #nil?
|
446
|
+
#
|
447
|
+
# @since 0.1.1
|
448
|
+
#
|
449
|
+
# @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-String
|
450
|
+
#
|
451
|
+
# @example Basic Usage
|
452
|
+
# require 'date'
|
453
|
+
# require 'bigdecimal'
|
454
|
+
# require 'lotus/utils/kernel'
|
455
|
+
#
|
456
|
+
# Lotus::Utils::Kernel.String('') # => ""
|
457
|
+
# Lotus::Utils::Kernel.String('ciao') # => "ciao"
|
458
|
+
#
|
459
|
+
# Lotus::Utils::Kernel.String(true) # => "true"
|
460
|
+
# Lotus::Utils::Kernel.String(false) # => "false"
|
461
|
+
#
|
462
|
+
# Lotus::Utils::Kernel.String(:lotus) # => "lotus"
|
463
|
+
#
|
464
|
+
# Lotus::Utils::Kernel.String(Picture) # => "Picture" # class
|
465
|
+
# Lotus::Utils::Kernel.String(Lotus) # => "Lotus" # module
|
466
|
+
#
|
467
|
+
# Lotus::Utils::Kernel.String([]) # => "[]"
|
468
|
+
# Lotus::Utils::Kernel.String([1,2,3]) # => "[1, 2, 3]"
|
469
|
+
# Lotus::Utils::Kernel.String(%w[a b c]) # => "[\"a\", \"b\", \"c\"]"
|
470
|
+
#
|
471
|
+
# Lotus::Utils::Kernel.String({}) # => "{}"
|
472
|
+
# Lotus::Utils::Kernel.String({a: 1, 'b' => 'c'}) # => "{:a=>1, \"b\"=>\"c\"}"
|
473
|
+
#
|
474
|
+
# Lotus::Utils::Kernel.String(Date.today) # => "2014-04-11"
|
475
|
+
# Lotus::Utils::Kernel.String(DateTime.now) # => "2014-04-11T10:15:06+02:00"
|
476
|
+
# Lotus::Utils::Kernel.String(Time.now) # => "2014-04-11 10:15:53 +0200"
|
477
|
+
#
|
478
|
+
# Lotus::Utils::Kernel.String(1) # => "1"
|
479
|
+
# Lotus::Utils::Kernel.String(3.14) # => "3.14"
|
480
|
+
# Lotus::Utils::Kernel.String(013) # => "11"
|
481
|
+
# Lotus::Utils::Kernel.String(0xc0ff33) # => "12648243"
|
482
|
+
#
|
483
|
+
# Lotus::Utils::Kernel.String(Rational(-22)) # => "-22/1"
|
484
|
+
# Lotus::Utils::Kernel.String(Complex(11, 2)) # => "11+2i"
|
485
|
+
# Lotus::Utils::Kernel.String(BigDecimal.new(7944.2343, 10)) # => "0.79442343E4"
|
486
|
+
# Lotus::Utils::Kernel.String(BigDecimal.new('Infinity')) # => "Infinity"
|
487
|
+
# Lotus::Utils::Kernel.String(BigDecimal.new('NaN')) # => "Infinity"
|
488
|
+
#
|
489
|
+
# @example String interface
|
490
|
+
# require 'lotus/utils/kernel'
|
491
|
+
#
|
492
|
+
# SimpleObject = Class.new(BasicObject) do
|
493
|
+
# def nil?
|
494
|
+
# false
|
495
|
+
# end
|
496
|
+
#
|
497
|
+
# def to_s
|
498
|
+
# 'simple object'
|
499
|
+
# end
|
500
|
+
# end
|
501
|
+
#
|
502
|
+
# Isbn = Struct.new(:code) do
|
503
|
+
# def to_str
|
504
|
+
# code.to_s
|
505
|
+
# end
|
506
|
+
# end
|
507
|
+
#
|
508
|
+
# simple = SimpleObject.new
|
509
|
+
# isbn = Isbn.new(123)
|
510
|
+
#
|
511
|
+
# Lotus::Utils::Kernel.String(simple) # => "simple object"
|
512
|
+
# Lotus::Utils::Kernel.String(isbn) # => "123"
|
513
|
+
#
|
514
|
+
# @example Error Handling
|
515
|
+
# require 'lotus/utils/kernel'
|
516
|
+
#
|
517
|
+
# # nil
|
518
|
+
# Kernel.String(nil) # => ""
|
519
|
+
# Lotus::Utils::Kernel.String(nil) # => nil
|
520
|
+
#
|
521
|
+
# @example Unchecked Exceptions
|
522
|
+
# require 'lotus/utils/kernel'
|
523
|
+
#
|
524
|
+
# BaseObject = Class.new(BasicObject) do
|
525
|
+
# def nil?
|
526
|
+
# false
|
527
|
+
# end
|
528
|
+
# end
|
529
|
+
#
|
530
|
+
# # Missing #nil?
|
531
|
+
# input = BasicObject.new
|
532
|
+
# Lotus::Utils::Kernel.String(input) # => NoMethodError
|
533
|
+
#
|
534
|
+
# # Missing #to_s or #to_str
|
535
|
+
# input = BaseObject.new
|
536
|
+
# Lotus::Utils::Kernel.Integer(input) # => TypeError
|
537
|
+
def self.String(arg)
|
538
|
+
super(arg) unless arg.nil?
|
539
|
+
end
|
540
|
+
|
541
|
+
# Coerces the argument to be a Date.
|
542
|
+
#
|
543
|
+
# @param arg [Object] the argument
|
544
|
+
#
|
545
|
+
# @return [Date,nil] the result of the coercion
|
546
|
+
#
|
547
|
+
# @raise [NoMethodError] if the argument doesn't implement #respond_to? or #to_s
|
548
|
+
# @raise [ArgumentError] if the argument can't be coerced
|
549
|
+
#
|
550
|
+
# @since 0.1.1
|
551
|
+
#
|
552
|
+
# @example Basic Usage
|
553
|
+
# require 'lotus/utils/kernel'
|
554
|
+
#
|
555
|
+
# Lotus::Utils::Kernel.Date(nil) # => nil
|
556
|
+
#
|
557
|
+
# Lotus::Utils::Kernel.Date(Date.today)
|
558
|
+
# # => #<Date: 2014-04-17 ((2456765j,0s,0n),+0s,2299161j)>
|
559
|
+
#
|
560
|
+
# Lotus::Utils::Kernel.Date(DateTime.now)
|
561
|
+
# # => #<Date: 2014-04-17 ((2456765j,0s,0n),+0s,2299161j)>
|
562
|
+
#
|
563
|
+
# Lotus::Utils::Kernel.Date(Time.now)
|
564
|
+
# # => #<Date: 2014-04-17 ((2456765j,0s,0n),+0s,2299161j)>
|
565
|
+
#
|
566
|
+
# Lotus::Utils::Kernel.Date('2014-04-17')
|
567
|
+
# # => #<Date: 2014-04-17 ((2456765j,0s,0n),+0s,2299161j)>
|
568
|
+
#
|
569
|
+
# Lotus::Utils::Kernel.Date('2014-04-17 22:37:15')
|
570
|
+
# # => #<Date: 2014-04-17 ((2456765j,0s,0n),+0s,2299161j)>
|
571
|
+
#
|
572
|
+
# @example Date Interface
|
573
|
+
# require 'lotus/utils/kernel'
|
574
|
+
#
|
575
|
+
# class Christmas
|
576
|
+
# def to_date
|
577
|
+
# Date.parse('Dec, 25')
|
578
|
+
# end
|
579
|
+
# end
|
580
|
+
#
|
581
|
+
# Lotus::Utils::Kernel.Date(Christmas.new)
|
582
|
+
# # => #<Date: 2014-12-25 ((2457017j,0s,0n),+0s,2299161j)>
|
583
|
+
#
|
584
|
+
# @example Unchecked Exceptions
|
585
|
+
# require 'lotus/utils/kernel'
|
586
|
+
#
|
587
|
+
# # Missing #nil?
|
588
|
+
# input = BasicObject.new
|
589
|
+
# Lotus::Utils::Kernel.Date(input) # => NoMethodError
|
590
|
+
def self.Date(arg)
|
591
|
+
if arg.respond_to?(:to_date)
|
592
|
+
arg.to_date
|
593
|
+
else
|
594
|
+
Date.parse(arg.to_s)
|
595
|
+
end unless arg.nil?
|
596
|
+
end
|
597
|
+
|
598
|
+
# Coerces the argument to be a DateTime.
|
599
|
+
#
|
600
|
+
# @param arg [Object] the argument
|
601
|
+
#
|
602
|
+
# @return [DateTime,nil] the result of the coercion
|
603
|
+
#
|
604
|
+
# @raise [NoMethodError] if the argument doesn't implement #respond_to? or #to_s
|
605
|
+
# @raise [ArgumentError] if the argument can't be coerced
|
606
|
+
#
|
607
|
+
# @since 0.1.1
|
608
|
+
#
|
609
|
+
# @example Basic Usage
|
610
|
+
# require 'lotus/utils/kernel'
|
611
|
+
#
|
612
|
+
# Lotus::Utils::Kernel.DateTime(nil) # => nil
|
613
|
+
#
|
614
|
+
# Lotus::Utils::Kernel.DateTime(3483943)
|
615
|
+
# # => Time.at(3483943).to_datetime #<DateTime: 1970-02-10T08:45:43+01:00 ((2440628j,27943s,0n),+3600s,2299161j)>
|
616
|
+
#
|
617
|
+
# Lotus::Utils::Kernel.DateTime(DateTime.now)
|
618
|
+
# # => #<DateTime: 2014-04-18T09:33:49+02:00 ((2456766j,27229s,690849000n),+7200s,2299161j)>
|
619
|
+
#
|
620
|
+
# Lotus::Utils::Kernel.DateTime(Date.today)
|
621
|
+
# # => #<DateTime: 2014-04-18T00:00:00+00:00 ((2456766j,0s,0n),+0s,2299161j)>
|
622
|
+
#
|
623
|
+
# Lotus::Utils::Kernel.Date(Time.now)
|
624
|
+
# # => #<DateTime: 2014-04-18T09:34:49+02:00 ((2456766j,27289s,832907000n),+7200s,2299161j)>
|
625
|
+
#
|
626
|
+
# Lotus::Utils::Kernel.DateTime('2014-04-18')
|
627
|
+
# # => #<DateTime: 2014-04-18T00:00:00+00:00 ((2456766j,0s,0n),+0s,2299161j)>
|
628
|
+
#
|
629
|
+
# Lotus::Utils::Kernel.DateTime('2014-04-18 09:35:42')
|
630
|
+
# # => #<DateTime: 2014-04-18T09:35:42+00:00 ((2456766j,34542s,0n),+0s,2299161j)>
|
631
|
+
#
|
632
|
+
# @example DateTime Interface
|
633
|
+
# require 'lotus/utils/kernel'
|
634
|
+
#
|
635
|
+
# class NewYearEve
|
636
|
+
# def to_datetime
|
637
|
+
# DateTime.parse('Jan, 1')
|
638
|
+
# end
|
639
|
+
# end
|
640
|
+
#
|
641
|
+
# Lotus::Utils::Kernel.Date(NewYearEve.new)
|
642
|
+
# # => #<DateTime: 2014-01-01T00:00:00+00:00 ((2456659j,0s,0n),+0s,2299161j)>
|
643
|
+
#
|
644
|
+
# @example Unchecked Exceptions
|
645
|
+
# require 'lotus/utils/kernel'
|
646
|
+
#
|
647
|
+
# # Missing #nil?
|
648
|
+
# input = BasicObject.new
|
649
|
+
# Lotus::Utils::Kernel.DateTime(input) # => NoMethodError
|
650
|
+
def self.DateTime(arg)
|
651
|
+
case arg
|
652
|
+
when ->(a) { a.respond_to?(:to_datetime) } then arg.to_datetime
|
653
|
+
when Numeric then DateTime(Time.at(arg))
|
654
|
+
when NilClass then nil
|
655
|
+
else
|
656
|
+
DateTime.parse(arg.to_s)
|
657
|
+
end
|
658
|
+
end
|
659
|
+
|
660
|
+
# Coerces the argument to be a Time.
|
661
|
+
#
|
662
|
+
# @param arg [Object] the argument
|
663
|
+
#
|
664
|
+
# @return [Time,nil] the result of the coercion
|
665
|
+
#
|
666
|
+
# @raise [NoMethodError] if the argument doesn't implement #respond_to? or #to_s
|
667
|
+
# @raise [ArgumentError] if the argument can't be coerced
|
668
|
+
#
|
669
|
+
# @since 0.1.1
|
670
|
+
#
|
671
|
+
# @example Basic Usage
|
672
|
+
# require 'lotus/utils/kernel'
|
673
|
+
#
|
674
|
+
# Lotus::Utils::Kernel.Time(nil) # => nil
|
675
|
+
#
|
676
|
+
# Lotus::Utils::Kernel.Time(Time.now)
|
677
|
+
# # => 2014-04-18 15:56:39 +0200
|
678
|
+
#
|
679
|
+
# Lotus::Utils::Kernel.Time(DateTime.now)
|
680
|
+
# # => 2014-04-18 15:56:39 +0200
|
681
|
+
#
|
682
|
+
# Lotus::Utils::Kernel.Time(Date.today)
|
683
|
+
# # => 2014-04-18 00:00:00 +0200
|
684
|
+
#
|
685
|
+
# Lotus::Utils::Kernel.Time('2014-04-18')
|
686
|
+
# # => 2014-04-18 00:00:00 +0200
|
687
|
+
#
|
688
|
+
# Lotus::Utils::Kernel.Time('2014-04-18 15:58:02')
|
689
|
+
# # => 2014-04-18 15:58:02 +0200
|
690
|
+
#
|
691
|
+
# @example Time Interface
|
692
|
+
# require 'lotus/utils/kernel'
|
693
|
+
#
|
694
|
+
# class Epoch
|
695
|
+
# def to_time
|
696
|
+
# Time.at(0)
|
697
|
+
# end
|
698
|
+
# end
|
699
|
+
#
|
700
|
+
# Lotus::Utils::Kernel.Time(Epoch.new)
|
701
|
+
# # => 1970-01-01 01:00:00 +0100
|
702
|
+
#
|
703
|
+
# @example Unchecked Exceptions
|
704
|
+
# require 'lotus/utils/kernel'
|
705
|
+
#
|
706
|
+
# # Missing #nil?
|
707
|
+
# input = BasicObject.new
|
708
|
+
# Lotus::Utils::Kernel.Time(input) # => NoMethodError
|
709
|
+
def self.Time(arg)
|
710
|
+
case arg
|
711
|
+
when ->(a) { a.respond_to?(:to_time) } then arg.to_time
|
712
|
+
when Numeric then Time.at(arg)
|
713
|
+
when NilClass then nil
|
714
|
+
else
|
715
|
+
Time.parse(arg.to_s)
|
716
|
+
end
|
717
|
+
end
|
718
|
+
|
719
|
+
# Coerces the argument to be a boolean.
|
720
|
+
#
|
721
|
+
# @param arg [Object] the argument
|
722
|
+
#
|
723
|
+
# @return [true,false,nil] the result of the coercion
|
724
|
+
#
|
725
|
+
# @raise [NoMethodError] if the argument doesn't implenent #respond_to?
|
726
|
+
#
|
727
|
+
# @since 0.1.1
|
728
|
+
#
|
729
|
+
# @example Basic Usage
|
730
|
+
# require 'lotus/utils/kernel'
|
731
|
+
#
|
732
|
+
# Lotus::Utils::Kernel.Boolean(nil) # => nil
|
733
|
+
# Lotus::Utils::Kernel.Boolean(0) # => false
|
734
|
+
# Lotus::Utils::Kernel.Boolean(1) # => true
|
735
|
+
# Lotus::Utils::Kernel.Boolean('0') # => false
|
736
|
+
# Lotus::Utils::Kernel.Boolean('1') # => true
|
737
|
+
# Lotus::Utils::Kernel.Boolean(Object.new) # => true
|
738
|
+
#
|
739
|
+
# @example Boolean Interface
|
740
|
+
# require 'lotus/utils/kernel'
|
741
|
+
#
|
742
|
+
# Answer = Struct.new(:answer) do
|
743
|
+
# def to_bool
|
744
|
+
# case answer
|
745
|
+
# when 'yes' then true
|
746
|
+
# else false
|
747
|
+
# end
|
748
|
+
# end
|
749
|
+
# end
|
750
|
+
#
|
751
|
+
# answer = Answer.new('yes')
|
752
|
+
# Lotus::Utils::Kernel.Boolean(answer) # => true
|
753
|
+
#
|
754
|
+
# @example Unchecked Exceptions
|
755
|
+
# require 'lotus/utils/kernel'
|
756
|
+
#
|
757
|
+
# # Missing #respond_to?
|
758
|
+
# input = BasicObject.new
|
759
|
+
# Lotus::Utils::Kernel.Boolean(input) # => NoMethodError
|
760
|
+
def self.Boolean(arg)
|
761
|
+
case arg
|
762
|
+
when Numeric then arg > 0 && arg <= 1
|
763
|
+
when String, '0' then Boolean(arg.to_i)
|
764
|
+
when NilClass then nil
|
765
|
+
when ->(a) { a.respond_to?(:to_bool) } then arg.to_bool
|
766
|
+
else
|
767
|
+
!!arg
|
768
|
+
end
|
769
|
+
end
|
770
|
+
end
|
771
|
+
end
|
772
|
+
end
|
773
|
+
|