lotus-utils 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6a206acbf5c8f3f36aecb39a8a834faba7f40d9
4
- data.tar.gz: fdeea0a0daf779be52a3f17a62460dbc5dd30f53
3
+ metadata.gz: 1937436164015390ea262191b1535441167f1e9c
4
+ data.tar.gz: 65bb8ce1f1d128fdca98e1918f96c08cf9fa4175
5
5
  SHA512:
6
- metadata.gz: bfe9cc4e5ef901a898905aff9eea8ffa39c56b0e5fce80c8d40e6493d7a5c15ad3189bec6ebebe50b87185b78a66189f38b45e299df3a258253bfce182e4d854
7
- data.tar.gz: d928d2cf1dc04a209144be6e03d9f25a1a4f064d079a809f82cd15877858b298a106ab378f025f14f09488cc78fad9ef1c352275af3febf46ecae6b189158dac
6
+ metadata.gz: aace7eede8b7e773ea4401d70e5537e89a965d5ed0000ffe9767ad5f78bcd5629080c17755770f3a4358fa61adb63da604df641a685073be6ea2fef9eb44bd88
7
+ data.tar.gz: 380a90d0b5df6cc0e52bf4bfea010f55e57fc207864268d39d3ba3f6d3f3e86541951fda027edbcef7da968be26779ab88b5e22dfb508b8a8fca86c72638568d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,52 @@
1
+ ## v0.2.0
2
+ ### Jun 23, 2014
3
+
4
+ a969fda 2014-06-12 **Luca Guidi** Implemented Lotus::Utils::Kernel.Symbol
5
+
6
+ 13532f5 2014-06-13 **Luca Guidi** [breaking] Implemented Lotus::Utils::Callbacks::Chain#freeze in order to prevent modification after the object has been frozen
7
+
8
+ e438da8 2014-06-13 **Luca Guidi** [breaking] All the Utils::Kernel methods will raise TypeError in case of failed coercion.
9
+
10
+ 7fd90d1 2014-06-13 **Luca Guidi** Make Kernel.Pathname to raise an error when nil is passed as argument
11
+
12
+ 6437791 2014-06-13 **Luca Guidi** [breaking] Make Kernel.Time to raise an error when nil is passed as argument
13
+
14
+ c7f428b 2014-06-13 **Luca Guidi** [breaking] Make Kernel.DateTime to raise an error when nil is passed as argument
15
+
16
+ ab0dc0c 2014-06-13 **Luca Guidi** [breaking] Make Kernel.Date to raise an error when nil is passed as argument
17
+
18
+ 11d5c1c 2014-06-13 **Luca Guidi** [breaking] Make Kernel.Boolean to return false when nil is passed as argument
19
+
20
+ c3bcaea 2014-06-13 **Luca Guidi** [breaking] Make Kernel.String to return an empty string when nil is passed as argument
21
+
22
+ 0bd3826 2014-06-13 **Luca Guidi** [breaking] Make Kernel.Float to return 0.0 when nil is passed as argument
23
+
24
+ 7ce8018 2014-06-13 **Luca Guidi** [breaking] Make Kernel.Integer to return 0 when nil is passed as argument
25
+
26
+ 0377326 2014-06-13 **Luca Guidi** [breaking] Make Kernel.Hash to return an empty hash when nil is passed as argument
27
+
28
+ d3b0e2a 2014-06-13 **Luca Guidi** [breaking] Make Kernel.Set to return an empty set when nil is passed as argument
29
+
30
+ cd2d1b8 2014-06-13 **Luca Guidi** [breaking] Make Kernel.Array to return an empty array when nil is passed as argument
31
+
32
+ 32c15d6 2014-06-09 **Luca Guidi** Implemented Lotus::Utils::LoadPaths#freeze in order to prevent modification after the object has been frozen
33
+
34
+ 2b8d8d5 2014-06-09 **Luca Guidi** Implemented Lotus::Utils::LoadPaths#initialize_copy in order to safely dup and clone
35
+
36
+ 9a91222 2014-06-09 **Luca Guidi** Implemented Lotus::Utils::LoadPaths#push, also aliased as #<<
37
+
38
+ 1d3d986 2014-06-08 **Luca Guidi** Use composition over inheritance for Lotus::Utils::LoadPaths
39
+
40
+ 82d0af3 2014-06-08 **Luca Guidi** [breaking] Use composition over inheritance for Lotus::Utils::Callbacks::Chain
41
+
42
+ 442175b 2014-06-04 **Luca Guidi** Introduced Lotus::Utils::LoadPaths
43
+
44
+ 2734a87 2014-05-28 **Luca Guidi** Introduced Lotus::Utils::String#namespace, in order to return the top level Ruby namespace for the given string
45
+
46
+ a2a19e9 2014-05-10 **Luca Guidi** Support for Ruby 2.1.2
47
+
48
+ 07146a1 2014-05-08 **Luca Guidi** Implemented Lotus::Utils::Kernel.Pathname
49
+
1
50
  ## v0.1.1
2
51
  ### Apr 23, 2014
3
52
 
data/README.md CHANGED
@@ -41,10 +41,9 @@ Or install it yourself as:
41
41
  ## Usage
42
42
 
43
43
  __Lotus::Utils__ is designed to enhance Ruby's code and stdlib.
44
- By default this gem doesn't load any code, require yourself what you need.
44
+ By default this gem doesn't load any code, you must require what you need.
45
45
 
46
- Please read the documentation of each module, in order to understand how to use
47
- each feature.
46
+ Please read the documentation of each module.
48
47
 
49
48
  ## Versioning
50
49
 
@@ -9,7 +9,16 @@ module Lotus
9
9
  #
10
10
  # @since 0.1.0
11
11
  # @private
12
- class Chain < ::Array
12
+ class Chain
13
+ # Return a new chain
14
+ #
15
+ # @return [Lotus::Utils::Callbacks::Chain]
16
+ #
17
+ # @since 0.2.0
18
+ def initialize
19
+ @chain = Array.new
20
+ end
21
+
13
22
  # Adds the given callbacks to the chain
14
23
  #
15
24
  # @param callbacks [Array] one or multiple callbacks to add
@@ -17,9 +26,12 @@ module Lotus
17
26
  #
18
27
  # @return [void]
19
28
  #
29
+ # @raise [RuntimeError] if the object was previously frozen
30
+ #
20
31
  # @see #run
21
32
  # @see Lotus::Utils::Callbacks::Callback
22
33
  # @see Lotus::Utils::Callbacks::MethodCallback
34
+ # @see Lotus::Utils::Callbacks::Chain#freeze
23
35
  #
24
36
  # @since 0.1.0
25
37
  #
@@ -40,10 +52,10 @@ module Lotus
40
52
  def add(*callbacks, &blk)
41
53
  callbacks.push blk if block_given?
42
54
  callbacks.each do |c|
43
- push Factory.fabricate(c)
55
+ @chain.push Factory.fabricate(c)
44
56
  end
45
57
 
46
- uniq!
58
+ @chain.uniq!
47
59
  end
48
60
 
49
61
  # Runs all the callbacks in the chain.
@@ -92,10 +104,30 @@ module Lotus
92
104
  #
93
105
  # Those callbacks will be invoked within the context of `action`.
94
106
  def run(context, *args)
95
- each do |callback|
107
+ @chain.each do |callback|
96
108
  callback.call(context, *args)
97
109
  end
98
110
  end
111
+
112
+ # It freezes the object by preventing further modifications.
113
+ #
114
+ # @since 0.2.0
115
+ #
116
+ # @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-freeze
117
+ #
118
+ # @example
119
+ # require 'lotus/utils/callbacks'
120
+ #
121
+ # chain = Lotus::Utils::Callbacks::Chain.new
122
+ # chain.freeze
123
+ #
124
+ # chain.frozen? # => true
125
+ #
126
+ # chain.add :authenticate! # => RuntimeError
127
+ def freeze
128
+ super
129
+ @chain.freeze
130
+ end
99
131
  end
100
132
 
101
133
  # Callback factory
@@ -1,6 +1,7 @@
1
1
  require 'set'
2
2
  require 'date'
3
3
  require 'time'
4
+ require 'pathname'
4
5
 
5
6
  module Lotus
6
7
  module Utils
@@ -20,20 +21,18 @@ module Lotus
20
21
  #
21
22
  # @return [Array] the result of the coercion
22
23
  #
23
- # @raise [NoMethodError] if arg doesn't implement #nil?
24
- #
25
24
  # @since 0.1.1
26
25
  #
27
- # @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-Array
26
+ # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-Array
28
27
  #
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
28
+ # @see http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-flatten
29
+ # @see http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-compact
30
+ # @see http://www.ruby-doc.org/core-2.1.2/Array.html#method-i-uniq
32
31
  #
33
32
  # @example Basic Usage
34
33
  # require 'lotus/utils/kernel'
35
34
  #
36
- # Lotus::Utils::Kernel.Array(nil) # => nil
35
+ # Lotus::Utils::Kernel.Array(nil) # => []
37
36
  # Lotus::Utils::Kernel.Array(true) # => [true]
38
37
  # Lotus::Utils::Kernel.Array(false) # => [false]
39
38
  # Lotus::Utils::Kernel.Array(1) # => [1]
@@ -63,7 +62,7 @@ module Lotus
63
62
  # response = Response.new(200, {}, 'hello')
64
63
  # Lotus::Utils::Kernel.Array(response) # => [200, {}, "hello"]
65
64
  def self.Array(arg)
66
- super(arg).flatten.compact.uniq unless arg.nil?
65
+ super(arg).flatten.compact.uniq
67
66
  end
68
67
 
69
68
  # Coerces the argument to be a set.
@@ -72,14 +71,14 @@ module Lotus
72
71
  #
73
72
  # @return [Set] the result of the coercion
74
73
  #
75
- # @raise [NoMethodError] if arg doesn't implement #respond_to?
74
+ # @raise [TypeError] if arg doesn't implement #respond_to?
76
75
  #
77
76
  # @since 0.1.1
78
77
  #
79
78
  # @example Basic Usage
80
79
  # require 'lotus/utils/kernel'
81
80
  #
82
- # Lotus::Utils::Kernel.Set(nil) # => nil
81
+ # Lotus::Utils::Kernel.Set(nil) # => #<Set: {}>
83
82
  # Lotus::Utils::Kernel.Set(true) # => #<Set: {true}>
84
83
  # Lotus::Utils::Kernel.Set(false) # => #<Set: {false}>
85
84
  # Lotus::Utils::Kernel.Set(1) # => #<Set: {1}>
@@ -112,33 +111,33 @@ module Lotus
112
111
  # @example Unchecked Exceptions
113
112
  # require 'lotus/utils/kernel'
114
113
  #
115
- # Lotus::Utils::Kernel.Set(BasicObject.new) # => NoMethodError
114
+ # Lotus::Utils::Kernel.Set(BasicObject.new) # => TypeError
116
115
  def self.Set(arg)
117
116
  if arg.respond_to?(:to_set)
118
117
  arg.to_set
119
118
  else
120
119
  Set.new(::Kernel.Array(arg))
121
- end unless arg.nil?
120
+ end
121
+ rescue NoMethodError
122
+ raise TypeError.new("can't convert into Set")
122
123
  end
123
124
 
124
125
  # Coerces the argument to be an hash.
125
126
  #
126
127
  # @param arg [Object] the input
127
128
  #
128
- # @return [Hash, nil] the result of the coercion
129
+ # @return [Hash] the result of the coercion
129
130
  #
130
- # @raise [NoMethodError] if arg doesn't implement #nil?
131
- # @raise [NoMethodError] if arg doesn't implement #respond_to?
132
131
  # @raise [TypeError] if arg can't be coerced
133
132
  #
134
133
  # @since 0.1.1
135
134
  #
136
- # @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-Hash
135
+ # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-Hash
137
136
  #
138
137
  # @example Basic Usage
139
138
  # require 'lotus/utils/kernel'
140
139
  #
141
- # Lotus::Utils::Kernel.Hash(nil) # => nil
140
+ # Lotus::Utils::Kernel.Hash(nil) # => {}
142
141
  # Lotus::Utils::Kernel.Hash({a: 1}) # => { :a => 1 }
143
142
  # Lotus::Utils::Kernel.Hash([[:a, 1]]) # => { :a => 1 }
144
143
  # Lotus::Utils::Kernel.Hash(Set.new([[:a, 1]])) # => { :a => 1 }
@@ -176,47 +175,45 @@ module Lotus
176
175
  # require 'lotus/utils/kernel'
177
176
  #
178
177
  # input = BasicObject.new
179
- # Lotus::Utils::Kernel.Hash(input) # => NoMethodError
178
+ # Lotus::Utils::Kernel.Hash(input) # => TypeError
180
179
  if RUBY_VERSION >= '2.1'
181
180
  def self.Hash(arg)
182
181
  if arg.respond_to?(:to_h)
183
182
  arg.to_h
184
183
  else
185
184
  super(arg)
186
- end unless arg.nil?
185
+ end
186
+ rescue NoMethodError
187
+ raise TypeError.new "can't convert into Hash"
187
188
  end
188
189
  else
189
190
  def self.Hash(arg)
190
191
  case arg
191
- when NilClass then nil
192
192
  when ::Hash then arg
193
193
  when ::Array, ::Set then Hash[*self.Array(arg)]
194
194
  when ->(a) { a.respond_to?(:to_h) } then arg.to_h
195
195
  else
196
196
  super(arg)
197
197
  end
198
- rescue ArgumentError
199
- raise TypeError
198
+ rescue ArgumentError, NoMethodError
199
+ raise TypeError.new "can't convert into Hash"
200
200
  end
201
201
  end
202
202
 
203
203
  # Coerces the argument to be an integer.
204
204
  #
205
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".
206
+ # error and raise an exception only when the argument can't be coerced.
207
207
  #
208
208
  # @param arg [Object] the argument
209
209
  #
210
- # @return [Fixnum,nil] the result of the coercion
210
+ # @return [Fixnum] the result of the coercion
211
211
  #
212
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
213
  #
217
214
  # @since 0.1.1
218
215
  #
219
- # @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-Integer
216
+ # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-Integer
220
217
  #
221
218
  # @example Basic Usage
222
219
  # require 'bigdecimal'
@@ -252,18 +249,18 @@ module Lotus
252
249
  #
253
250
  # # nil
254
251
  # Kernel.Integer(nil) # => TypeError
255
- # Lotus::Utils::Kernel.Integer(nil) # => nil
252
+ # Lotus::Utils::Kernel.Integer(nil) # => 0
256
253
  #
257
254
  # # float represented as a string
258
- # Kernel.Integer("23.4") # => ArgumentError
255
+ # Kernel.Integer("23.4") # => TypeError
259
256
  # Lotus::Utils::Kernel.Integer("23.4") # => 23
260
257
  #
261
258
  # # rational represented as a string
262
- # Kernel.Integer("2/3") # => ArgumentError
259
+ # Kernel.Integer("2/3") # => TypeError
263
260
  # Lotus::Utils::Kernel.Integer("2/3") # => 2
264
261
  #
265
262
  # # complex represented as a string
266
- # Kernel.Integer("2.5/1") # => ArgumentError
263
+ # Kernel.Integer("2.5/1") # => TypeError
267
264
  # Lotus::Utils::Kernel.Integer("2.5/1") # => 2
268
265
  #
269
266
  # @example Unchecked Exceptions
@@ -291,48 +288,51 @@ module Lotus
291
288
  # input = DateTime.now
292
289
  # Lotus::Utils::Kernel.Integer(input) # => TypeError
293
290
  #
294
- # # Missing #nil?
295
- # input = BasicObject.new
296
- # Lotus::Utils::Kernel.Integer(input) # => NoMethodError
297
- #
298
291
  # # bigdecimal infinity
299
292
  # input = BigDecimal.new("Infinity")
300
- # Lotus::Utils::Kernel.Integer(input) # => FloatDomainError
293
+ # Lotus::Utils::Kernel.Integer(input) # => TypeError
301
294
  #
302
295
  # # bigdecimal NaN
303
296
  # input = BigDecimal.new("NaN")
304
- # Lotus::Utils::Kernel.Integer(input) # => FloatDomainError
297
+ # Lotus::Utils::Kernel.Integer(input) # => TypeError
305
298
  #
306
299
  # # big rational
307
300
  # input = Rational(-8) ** Rational(1, 3)
308
- # Lotus::Utils::Kernel.Integer(input) # => RangeError
301
+ # Lotus::Utils::Kernel.Integer(input) # => TypeError
309
302
  #
310
303
  # # big complex represented as a string
311
304
  # input = Complex(2, 3)
312
- # Lotus::Utils::Kernel.Integer(input) # => RangeError
305
+ # Lotus::Utils::Kernel.Integer(input) # => TypeError
313
306
  def self.Integer(arg)
314
- super(arg) unless arg.nil?
315
- rescue ArgumentError
316
- arg.to_i
307
+ super(arg)
308
+ rescue ArgumentError, TypeError
309
+ begin
310
+ if arg.respond_to?(:to_i)
311
+ arg.to_i
312
+ else
313
+ raise TypeError.new "can't convert into Integer"
314
+ end
315
+ rescue NoMethodError
316
+ raise TypeError.new "can't convert into Integer"
317
+ end
318
+ rescue RangeError
319
+ raise TypeError.new "can't convert into Integer"
317
320
  end
318
321
 
319
322
  # Coerces the argument to be a float.
320
323
  #
321
324
  # It's similar to Ruby's Kernel.Float, but it doesn't stop at the first
322
- # error and tries to be less "whiny".
325
+ # error and raise an exception only when the argument can't be coerced.
323
326
  #
324
327
  # @param arg [Object] the argument
325
328
  #
326
- # @return [Float,nil] the result of the coercion
329
+ # @return [Float] the result of the coercion
327
330
  #
328
331
  # @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
332
  #
333
333
  # @since 0.1.1
334
334
  #
335
- # @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-Float
335
+ # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-Float
336
336
  #
337
337
  # @example Basic Usage
338
338
  # require 'bigdecimal'
@@ -369,18 +369,18 @@ module Lotus
369
369
  #
370
370
  # # nil
371
371
  # Kernel.Float(nil) # => TypeError
372
- # Lotus::Utils::Kernel.Float(nil) # => nil
372
+ # Lotus::Utils::Kernel.Float(nil) # => 0.0
373
373
  #
374
374
  # # float represented as a string
375
- # Kernel.Float("23.4") # => ArgumentError
375
+ # Kernel.Float("23.4") # => TypeError
376
376
  # Lotus::Utils::Kernel.Float("23.4") # => 23.4
377
377
  #
378
378
  # # rational represented as a string
379
- # Kernel.Float("2/3") # => ArgumentError
379
+ # Kernel.Float("2/3") # => TypeError
380
380
  # Lotus::Utils::Kernel.Float("2/3") # => 2.0
381
381
  #
382
382
  # # complex represented as a string
383
- # Kernel.Float("2.5/1") # => ArgumentError
383
+ # Kernel.Float("2.5/1") # => TypeError
384
384
  # Lotus::Utils::Kernel.Float("2.5/1") # => 2.5
385
385
  #
386
386
  # # bigdecimal infinity
@@ -418,35 +418,45 @@ module Lotus
418
418
  #
419
419
  # # Missing #nil?
420
420
  # input = BasicObject.new
421
- # Lotus::Utils::Kernel.Float(input) # => NoMethodError
421
+ # Lotus::Utils::Kernel.Float(input) # => TypeError
422
422
  #
423
423
  # # big rational
424
424
  # input = Rational(-8) ** Rational(1, 3)
425
- # Lotus::Utils::Kernel.Float(input) # => RangeError
425
+ # Lotus::Utils::Kernel.Float(input) # => TypeError
426
426
  #
427
427
  # # big complex represented as a string
428
428
  # input = Complex(2, 3)
429
- # Lotus::Utils::Kernel.Float(input) # => RangeError
429
+ # Lotus::Utils::Kernel.Float(input) # => TypeError
430
430
  def self.Float(arg)
431
- super(arg) unless arg.nil?
432
- rescue ArgumentError
433
- arg.to_f
431
+ super(arg)
432
+ rescue ArgumentError, TypeError
433
+ begin
434
+ if arg.respond_to?(:to_f)
435
+ arg.to_f
436
+ else
437
+ raise TypeError.new "can't convert into Float"
438
+ end
439
+ rescue NoMethodError
440
+ raise TypeError.new "can't convert into Float"
441
+ end
442
+ rescue RangeError
443
+ raise TypeError.new "can't convert into Float"
434
444
  end
435
445
 
436
446
  # Coerces the argument to be a string.
437
447
  #
438
- # It's similar to Ruby's Kernel.String, but it's less "whiny".
448
+ # Identical behavior of Ruby's Kernel.Array, still here because we want
449
+ # to keep the interface consistent
439
450
  #
440
451
  # @param arg [Object] the argument
441
452
  #
442
- # @return [String,nil] the result of the coercion
453
+ # @return [String] the result of the coercion
443
454
  #
444
455
  # @raise [TypeError] if the argument can't be coerced
445
- # @raise [NoMethodError] if the argument doesn't implement #nil?
446
456
  #
447
457
  # @since 0.1.1
448
458
  #
449
- # @see http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-String
459
+ # @see http://www.ruby-doc.org/core-2.1.2/Kernel.html#method-i-String
450
460
  #
451
461
  # @example Basic Usage
452
462
  # require 'date'
@@ -490,10 +500,6 @@ module Lotus
490
500
  # require 'lotus/utils/kernel'
491
501
  #
492
502
  # SimpleObject = Class.new(BasicObject) do
493
- # def nil?
494
- # false
495
- # end
496
- #
497
503
  # def to_s
498
504
  # 'simple object'
499
505
  # end
@@ -511,49 +517,36 @@ module Lotus
511
517
  # Lotus::Utils::Kernel.String(simple) # => "simple object"
512
518
  # Lotus::Utils::Kernel.String(isbn) # => "123"
513
519
  #
514
- # @example Error Handling
520
+ # @example Comparison with Ruby
515
521
  # require 'lotus/utils/kernel'
516
522
  #
517
523
  # # nil
518
524
  # Kernel.String(nil) # => ""
519
- # Lotus::Utils::Kernel.String(nil) # => nil
525
+ # Lotus::Utils::Kernel.String(nil) # => ""
520
526
  #
521
527
  # @example Unchecked Exceptions
522
528
  # require 'lotus/utils/kernel'
523
529
  #
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
530
  # # Missing #to_s or #to_str
535
531
  # input = BaseObject.new
536
532
  # Lotus::Utils::Kernel.Integer(input) # => TypeError
537
533
  def self.String(arg)
538
- super(arg) unless arg.nil?
534
+ super(arg)
539
535
  end
540
536
 
541
537
  # Coerces the argument to be a Date.
542
538
  #
543
539
  # @param arg [Object] the argument
544
540
  #
545
- # @return [Date,nil] the result of the coercion
541
+ # @return [Date] the result of the coercion
546
542
  #
547
- # @raise [NoMethodError] if the argument doesn't implement #respond_to? or #to_s
548
- # @raise [ArgumentError] if the argument can't be coerced
543
+ # @raise [TypeError] if the argument can't be coerced
549
544
  #
550
545
  # @since 0.1.1
551
546
  #
552
547
  # @example Basic Usage
553
548
  # require 'lotus/utils/kernel'
554
549
  #
555
- # Lotus::Utils::Kernel.Date(nil) # => nil
556
- #
557
550
  # Lotus::Utils::Kernel.Date(Date.today)
558
551
  # # => #<Date: 2014-04-17 ((2456765j,0s,0n),+0s,2299161j)>
559
552
  #
@@ -584,33 +577,40 @@ module Lotus
584
577
  # @example Unchecked Exceptions
585
578
  # require 'lotus/utils/kernel'
586
579
  #
587
- # # Missing #nil?
580
+ # # nil
581
+ # input = nil
582
+ # Lotus::Utils::Kernel.Date(input) # => TypeError
583
+ #
584
+ # # Missing #respond_to?
585
+ # input = BasicObject.new
586
+ # Lotus::Utils::Kernel.Date(input) # => TypeError
587
+ #
588
+ # # Missing #to_s?
588
589
  # input = BasicObject.new
589
- # Lotus::Utils::Kernel.Date(input) # => NoMethodError
590
+ # Lotus::Utils::Kernel.Date(input) # => TypeError
590
591
  def self.Date(arg)
591
592
  if arg.respond_to?(:to_date)
592
593
  arg.to_date
593
594
  else
594
595
  Date.parse(arg.to_s)
595
- end unless arg.nil?
596
+ end
597
+ rescue ArgumentError, NoMethodError
598
+ raise TypeError.new "can't convert into Date"
596
599
  end
597
600
 
598
601
  # Coerces the argument to be a DateTime.
599
602
  #
600
603
  # @param arg [Object] the argument
601
604
  #
602
- # @return [DateTime,nil] the result of the coercion
605
+ # @return [DateTime] the result of the coercion
603
606
  #
604
- # @raise [NoMethodError] if the argument doesn't implement #respond_to? or #to_s
605
- # @raise [ArgumentError] if the argument can't be coerced
607
+ # @raise [TypeError] if the argument can't be coerced
606
608
  #
607
609
  # @since 0.1.1
608
610
  #
609
611
  # @example Basic Usage
610
612
  # require 'lotus/utils/kernel'
611
613
  #
612
- # Lotus::Utils::Kernel.DateTime(nil) # => nil
613
- #
614
614
  # Lotus::Utils::Kernel.DateTime(3483943)
615
615
  # # => Time.at(3483943).to_datetime #<DateTime: 1970-02-10T08:45:43+01:00 ((2440628j,27943s,0n),+3600s,2299161j)>
616
616
  #
@@ -644,35 +644,41 @@ module Lotus
644
644
  # @example Unchecked Exceptions
645
645
  # require 'lotus/utils/kernel'
646
646
  #
647
- # # Missing #nil?
647
+ # # When nil
648
+ # input = nil
649
+ # Lotus::Utils::Kernel.DateTime(input) # => TypeError
650
+ #
651
+ # # Missing #respond_to?
652
+ # input = BasicObject.new
653
+ # Lotus::Utils::Kernel.DateTime(input) # => TypeError
654
+ #
655
+ # # Missing #to_s?
648
656
  # input = BasicObject.new
649
- # Lotus::Utils::Kernel.DateTime(input) # => NoMethodError
657
+ # Lotus::Utils::Kernel.DateTime(input) # => TypeError
650
658
  def self.DateTime(arg)
651
659
  case arg
652
660
  when ->(a) { a.respond_to?(:to_datetime) } then arg.to_datetime
653
661
  when Numeric then DateTime(Time.at(arg))
654
- when NilClass then nil
655
662
  else
656
663
  DateTime.parse(arg.to_s)
657
664
  end
665
+ rescue ArgumentError, NoMethodError
666
+ raise TypeError.new "can't convert into DateTime"
658
667
  end
659
668
 
660
669
  # Coerces the argument to be a Time.
661
670
  #
662
671
  # @param arg [Object] the argument
663
672
  #
664
- # @return [Time,nil] the result of the coercion
673
+ # @return [Time] the result of the coercion
665
674
  #
666
- # @raise [NoMethodError] if the argument doesn't implement #respond_to? or #to_s
667
- # @raise [ArgumentError] if the argument can't be coerced
675
+ # @raise [TypeError] if the argument can't be coerced
668
676
  #
669
677
  # @since 0.1.1
670
678
  #
671
679
  # @example Basic Usage
672
680
  # require 'lotus/utils/kernel'
673
681
  #
674
- # Lotus::Utils::Kernel.Time(nil) # => nil
675
- #
676
682
  # Lotus::Utils::Kernel.Time(Time.now)
677
683
  # # => 2014-04-18 15:56:39 +0200
678
684
  #
@@ -703,33 +709,42 @@ module Lotus
703
709
  # @example Unchecked Exceptions
704
710
  # require 'lotus/utils/kernel'
705
711
  #
706
- # # Missing #nil?
712
+ # # When nil
713
+ # input = nil
714
+ # Lotus::Utils::Kernel.Time(input) # => TypeError
715
+ #
716
+ # # Missing #respond_to?
717
+ # input = BasicObject.new
718
+ # Lotus::Utils::Kernel.Time(input) # => TypeError
719
+ #
720
+ # # Missing #to_s?
707
721
  # input = BasicObject.new
708
- # Lotus::Utils::Kernel.Time(input) # => NoMethodError
722
+ # Lotus::Utils::Kernel.Time(input) # => TypeError
709
723
  def self.Time(arg)
710
724
  case arg
711
725
  when ->(a) { a.respond_to?(:to_time) } then arg.to_time
712
726
  when Numeric then Time.at(arg)
713
- when NilClass then nil
714
727
  else
715
728
  Time.parse(arg.to_s)
716
729
  end
730
+ rescue ArgumentError, NoMethodError
731
+ raise TypeError.new "can't convert into Time"
717
732
  end
718
733
 
719
734
  # Coerces the argument to be a boolean.
720
735
  #
721
736
  # @param arg [Object] the argument
722
737
  #
723
- # @return [true,false,nil] the result of the coercion
738
+ # @return [true,false] the result of the coercion
724
739
  #
725
- # @raise [NoMethodError] if the argument doesn't implenent #respond_to?
740
+ # @raise [TypeError] if the argument can't be coerced
726
741
  #
727
742
  # @since 0.1.1
728
743
  #
729
744
  # @example Basic Usage
730
745
  # require 'lotus/utils/kernel'
731
746
  #
732
- # Lotus::Utils::Kernel.Boolean(nil) # => nil
747
+ # Lotus::Utils::Kernel.Boolean(nil) # => false
733
748
  # Lotus::Utils::Kernel.Boolean(0) # => false
734
749
  # Lotus::Utils::Kernel.Boolean(1) # => true
735
750
  # Lotus::Utils::Kernel.Boolean('0') # => false
@@ -756,16 +771,127 @@ module Lotus
756
771
  #
757
772
  # # Missing #respond_to?
758
773
  # input = BasicObject.new
759
- # Lotus::Utils::Kernel.Boolean(input) # => NoMethodError
774
+ # Lotus::Utils::Kernel.Boolean(input) # => TypeError
760
775
  def self.Boolean(arg)
761
776
  case arg
762
777
  when Numeric then arg > 0 && arg <= 1
763
778
  when String, '0' then Boolean(arg.to_i)
764
- when NilClass then nil
765
779
  when ->(a) { a.respond_to?(:to_bool) } then arg.to_bool
766
780
  else
767
781
  !!arg
768
782
  end
783
+ rescue NoMethodError
784
+ raise TypeError.new "can't convert into Boolean"
785
+ end
786
+
787
+ # Coerces the argument to be a Pathname.
788
+ #
789
+ # @param arg [#to_pathname,#to_str] the argument
790
+ #
791
+ # @return [Pathname] the result of the coercion
792
+ #
793
+ # @raise [TypeError] if the argument can't be coerced
794
+ #
795
+ # @since 0.1.2
796
+ #
797
+ # @example Basic Usage
798
+ # require 'lotus/utils/kernel'
799
+ #
800
+ # Lotus::Utils::Kernel.Pathname(Pathname.new('/path/to')) # => #<Pathname:/path/to>
801
+ # Lotus::Utils::Kernel.Pathname('/path/to') # => #<Pathname:/path/to>
802
+ #
803
+ # @example Pathname Interface
804
+ # require 'lotus/utils/kernel'
805
+ #
806
+ # class HomePath
807
+ # def to_pathname
808
+ # Pathname.new Dir.home
809
+ # end
810
+ # end
811
+ #
812
+ # Lotus::Utils::Kernel.Pathname(HomePath.new) # => #<Pathname:/Users/luca>
813
+ #
814
+ # @example String Interface
815
+ # require 'lotus/utils/kernel'
816
+ #
817
+ # class RootPath
818
+ # def to_str
819
+ # '/'
820
+ # end
821
+ # end
822
+ #
823
+ # Lotus::Utils::Kernel.Pathname(RootPath.new) # => #<Pathname:/>
824
+ #
825
+ # @example Unchecked Exceptions
826
+ # require 'lotus/utils/kernel'
827
+ #
828
+ # # When nil
829
+ # input = nil
830
+ # Lotus::Utils::Kernel.Pathname(input) # => TypeError
831
+ #
832
+ # # Missing #respond_to?
833
+ # input = BasicObject.new
834
+ # Lotus::Utils::Kernel.Pathname(input) # => TypeError
835
+ def self.Pathname(arg)
836
+ case arg
837
+ when ->(a) { a.respond_to?(:to_pathname) } then arg.to_pathname
838
+ else
839
+ super
840
+ end
841
+ rescue NoMethodError
842
+ raise TypeError.new "can't convert into Pathname"
843
+ end
844
+
845
+ # Coerces the argument to be a String.
846
+ #
847
+ # @param arg [#to_sym] the argument
848
+ #
849
+ # @return [Symbol] the result of the coercion
850
+ #
851
+ # @raise [TypeError] if the argument can't be coerced
852
+ #
853
+ # @since 0.2.0
854
+ #
855
+ # @example Basic Usage
856
+ # require 'lotus/utils/kernel'
857
+ #
858
+ # Lotus::Utils::Kernel.Symbol(:hello) # => :hello
859
+ # Lotus::Utils::Kernel.Symbol('hello') # => :hello
860
+ #
861
+ # @example Symbol Interface
862
+ # require 'lotus/utils/kernel'
863
+ #
864
+ # class StatusSymbol
865
+ # def to_sym
866
+ # :success
867
+ # end
868
+ # end
869
+ #
870
+ # Lotus::Utils::Kernel.Symbol(StatusSymbol.new) # => :success
871
+ #
872
+ # @example Unchecked Exceptions
873
+ # require 'lotus/utils/kernel'
874
+ #
875
+ # # When nil
876
+ # input = nil
877
+ # Lotus::Utils::Kernel.Symbol(input) # => TypeError
878
+ #
879
+ # # When empty string
880
+ # input = ''
881
+ # Lotus::Utils::Kernel.Symbol(input) # => TypeError
882
+ #
883
+ # # Missing #respond_to?
884
+ # input = BasicObject.new
885
+ # Lotus::Utils::Kernel.Symbol(input) # => TypeError
886
+ def self.Symbol(arg)
887
+ case arg
888
+ when '' then raise TypeError.new "can't convert into Symbol"
889
+ when ->(a) { a.respond_to?(:to_sym) } then arg.to_sym
890
+ else
891
+ raise TypeError.new "can't convert into Symbol"
892
+ end
893
+ rescue NoMethodError
894
+ raise TypeError.new "can't convert into Symbol"
769
895
  end
770
896
  end
771
897
  end