lotus-utils 0.3.2 → 0.3.3

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: e9735bda19b42820e137d3701b680744b1e31efa
4
- data.tar.gz: 2f8d6bd945be961bf835f994dcf0f3337805d4d6
3
+ metadata.gz: 629a22895276c357e5ee478c10eb6fd0c2c4ef4a
4
+ data.tar.gz: 185f59a1c22e690428f1675aa0ebb2a1553ad4ad
5
5
  SHA512:
6
- metadata.gz: bd341a626427d29d9e8785fca9eb600a171ddf1e4acc64ed72f8301276a8d2166f8ac4fc4ce41ecfa285af419e3556e88c735cdb7a29e8e53c801d601089df35
7
- data.tar.gz: 4b895ccb945daeea8eb89f03ae3f77cf8e3a58d6f0b59c717fd9a8737c4239e95f2f823f7c136f01938d5d6da80a037604cfc9a60e1e53b2ee7e20badbc2485c
6
+ metadata.gz: a3e7907833d632771a62ccd573d72238ca7f454f28c84f440eb85b6cf6b49bb635214bc22d05431861ff163246ef82fea184c30b52132c54c8323e7d4eac157c
7
+ data.tar.gz: 589fd11dc42724192687b86d4848b378d90c7a14516fb69117667a7b021df56facd67f91ebbba7fb53fe492a2e010ee22bf6622910fa0a6bed61f83fb19befb7
data/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Lotus::Utils
2
2
  Ruby core extentions and class utilities for Lotus
3
3
 
4
+ ## v0.3.3 - 2015-01-08
5
+ ### Fixed
6
+ - [Luca Guidi] Ensure to return the right offending object if a missing method is called with Utils::String and Hash (eg. `Utils::Hash.new(a: 1).all? {|_, v| v.foo }` blame `v` instead of `Hash`)
7
+ - [Luca Guidi] Raise an error if try to coerce non numeric strings into Integer, Float & BigDecimal (eg. `Utils::Kernel.Integer("hello") # => raise TypeError`)
8
+
4
9
  ## v0.3.2 - 2014-12-23
5
10
  ### Added
6
11
  - [Luca Guidi] Official support for Ruby 2.2
@@ -259,12 +259,16 @@ module Lotus
259
259
  #
260
260
  # @api private
261
261
  # @since 0.3.0
262
+ #
263
+ # @raise [NoMethodError] If doesn't respond to the given method
262
264
  def method_missing(m, *args, &blk)
263
- h = @hash.__send__(m, *args, &blk)
264
- h = self.class.new(h) if h.is_a?(::Hash)
265
- h
266
- rescue NoMethodError
267
- raise NoMethodError.new(%(undefined method `#{ m }' for #{ @hash }:#{ self.class }))
265
+ if respond_to?(m)
266
+ h = @hash.__send__(m, *args, &blk)
267
+ h = self.class.new(h) if h.is_a?(::Hash)
268
+ h
269
+ else
270
+ raise NoMethodError.new(%(undefined method `#{ m }' for #{ @hash }:#{ self.class }))
271
+ end
268
272
  end
269
273
 
270
274
  # Override Ruby's respond_to_missing? in order to support ::Hash interface
@@ -17,6 +17,14 @@ module Lotus
17
17
  # Kernel utilities
18
18
  # @since 0.1.1
19
19
  module Kernel
20
+ # Matcher for numeric values
21
+ #
22
+ # @since 0.3.3
23
+ # @api private
24
+ #
25
+ # @see Lotus::Utils::Kernel.Integer
26
+ NUMERIC_MATCHER = /\A([\d\/\.\+iE]+|NaN|Infinity)\z/.freeze
27
+
20
28
  # Coerces the argument to be an Array.
21
29
  #
22
30
  # It's similar to Ruby's Kernel.Array, but it applies further
@@ -285,6 +293,10 @@ module Lotus
285
293
  # input = OpenStruct.new(color: 'purple')
286
294
  # Lotus::Utils::Kernel.Integer(input) # => TypeError
287
295
  #
296
+ # # String that doesn't represent an integer
297
+ # input = 'hello'
298
+ # Lotus::Utils::Kernel.Integer(input) # => TypeError
299
+ #
288
300
  # # When true
289
301
  # input = true
290
302
  # Lotus::Utils::Kernel.Integer(input) # => TypeError
@@ -320,7 +332,8 @@ module Lotus
320
332
  super(arg)
321
333
  rescue ArgumentError, TypeError, NoMethodError
322
334
  begin
323
- if arg.respond_to?(:to_i)
335
+ case arg
336
+ when NilClass, ->(a) { a.respond_to?(:to_i) && a.to_s.match(NUMERIC_MATCHER) }
324
337
  arg.to_i
325
338
  else
326
339
  raise TypeError.new "can't convert into Integer"
@@ -398,6 +411,10 @@ module Lotus
398
411
  # input = Time.now
399
412
  # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
400
413
  #
414
+ # # String that doesn't represent a big decimal
415
+ # input = 'hello'
416
+ # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
417
+ #
401
418
  # # Missing #respond_to?
402
419
  # input = BasicObject.new
403
420
  # Lotus::Utils::Kernel.BigDecimal(input) # => TypeError
@@ -406,8 +423,10 @@ module Lotus
406
423
  when ->(a) { a.respond_to?(:to_d) } then arg.to_d
407
424
  when Float, Complex, Rational
408
425
  BigDecimal(arg.to_s)
409
- else
426
+ when ->(a) { a.to_s.match(NUMERIC_MATCHER) }
410
427
  BigDecimal.new(arg)
428
+ else
429
+ raise TypeError.new "can't convert into BigDecimal"
411
430
  end
412
431
  rescue NoMethodError
413
432
  raise TypeError.new "can't convert into BigDecimal"
@@ -514,6 +533,10 @@ module Lotus
514
533
  # input = BasicObject.new
515
534
  # Lotus::Utils::Kernel.Float(input) # => TypeError
516
535
  #
536
+ # # String that doesn't represent a float
537
+ # input = 'hello'
538
+ # Lotus::Utils::Kernel.Float(input) # => TypeError
539
+ #
517
540
  # # big rational
518
541
  # input = Rational(-8) ** Rational(1, 3)
519
542
  # Lotus::Utils::Kernel.Float(input) # => TypeError
@@ -525,7 +548,8 @@ module Lotus
525
548
  super(arg)
526
549
  rescue ArgumentError, TypeError
527
550
  begin
528
- if arg.respond_to?(:to_f)
551
+ case arg
552
+ when NilClass, ->(a) { a.respond_to?(:to_f) && a.to_s.match(NUMERIC_MATCHER) }
529
553
  arg.to_f
530
554
  else
531
555
  raise TypeError.new "can't convert into Float"
@@ -207,20 +207,28 @@ module Lotus
207
207
  # @see http://www.ruby-doc.org/core/String.html#method-i-gsub
208
208
  #
209
209
  # @since 0.3.0
210
- def gsub(pattern, replacement, &blk)
211
- @string.gsub(pattern, replacement, &blk)
210
+ def gsub(pattern, replacement = nil, &blk)
211
+ if block_given?
212
+ @string.gsub(pattern, &blk)
213
+ else
214
+ @string.gsub(pattern, replacement)
215
+ end
212
216
  end
213
217
 
214
218
  # Override Ruby's method_missing in order to provide ::String interface
215
219
  #
216
220
  # @api private
217
221
  # @since 0.3.0
222
+ #
223
+ # @raise [NoMethodError] If doesn't respond to the given method
218
224
  def method_missing(m, *args, &blk)
219
- s = @string.__send__(m, *args, &blk)
220
- s = self.class.new(s) if s.is_a?(::String)
221
- s
222
- rescue NoMethodError
223
- raise NoMethodError.new(%(undefined method `#{ m }' for "#{ @string }":#{ self.class }))
225
+ if respond_to?(m)
226
+ s = @string.__send__(m, *args, &blk)
227
+ s = self.class.new(s) if s.is_a?(::String)
228
+ s
229
+ else
230
+ raise NoMethodError.new(%(undefined method `#{ m }' for "#{ @string }":#{ self.class }))
231
+ end
224
232
  end
225
233
 
226
234
  # Override Ruby's respond_to_missing? in order to support ::String interface
@@ -1,5 +1,8 @@
1
1
  module Lotus
2
2
  module Utils
3
- VERSION = '0.3.2'.freeze
3
+ # Defines the version
4
+ #
5
+ # @since 0.1.0
6
+ VERSION = '0.3.3'.freeze
4
7
  end
5
8
  end
data/lotus-utils.gemspec CHANGED
@@ -6,8 +6,8 @@ require 'lotus/utils/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'lotus-utils'
8
8
  spec.version = Lotus::Utils::VERSION
9
- spec.authors = ['Luca Guidi']
10
- spec.email = ['me@lucaguidi.com']
9
+ spec.authors = ['Luca Guidi', 'Trung Lê']
10
+ spec.email = ['me@lucaguidi.com', 'trung.le@ruby-journal.com']
11
11
  spec.description = %q{Lotus utilities}
12
12
  spec.summary = %q{Ruby core extentions and Louts utilities}
13
13
  spec.homepage = 'http://lotusrb.org'
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
+ - Trung Lê
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
12
+ date: 2015-01-08 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: bundler
@@ -55,6 +56,7 @@ dependencies:
55
56
  description: Lotus utilities
56
57
  email:
57
58
  - me@lucaguidi.com
59
+ - trung.le@ruby-journal.com
58
60
  executables: []
59
61
  extensions: []
60
62
  extra_rdoc_files: []
@@ -96,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
98
  version: '0'
97
99
  requirements: []
98
100
  rubyforge_project:
99
- rubygems_version: 2.2.2
101
+ rubygems_version: 2.4.5
100
102
  signing_key:
101
103
  specification_version: 4
102
104
  summary: Ruby core extentions and Louts utilities