hanami-utils 0.7.1 → 0.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/hanami/utils/kernel.rb +34 -10
- data/lib/hanami/utils/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4cbb5f4adac5f75b2e56d9cb8bc2f2a5b91cf5e5
|
4
|
+
data.tar.gz: da1b1e8502a4b5fd322bfd9ee660d8b4a1c8d9c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 268645d9ea4cb481333cee4315e51e466c3d1900417622d69139f02111a7a0cde5a9e36714a69ddb5c942a51c903420793d7421018a46440c0f7b483563f43ca
|
7
|
+
data.tar.gz: 3784235c1726f07be68e41a8f1d7ae67f0e0dbf4c760bb20f6dfaac994e2146c946042f903eb8549a742b0ce5ae94af77348bdac2dc7d86c59b12ee5089e0783
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Hanami::Utils
|
2
2
|
Ruby core extentions and class utilities for Hanami
|
3
3
|
|
4
|
+
## v0.7.2 - 2016-05-20
|
5
|
+
### Fixed
|
6
|
+
- [Luca Guidi] Make `Hanami::Utils::Kernel.BigDecimal` to convert negative `BigDecimal` or `BigDecimal` with negative exponent.
|
7
|
+
|
4
8
|
## v0.7.1 - 2016-02-05
|
5
9
|
### Fixed
|
6
10
|
- [Yuuji Yaginuma] `Hanami::Utils::Escape`: fixed Ruby warning for `String#chars` with a block, which is deprecated. Using `String#each_char` now.
|
data/lib/hanami/utils/kernel.rb
CHANGED
@@ -4,6 +4,7 @@ require 'time'
|
|
4
4
|
require 'pathname'
|
5
5
|
require 'bigdecimal'
|
6
6
|
require 'hanami/utils'
|
7
|
+
require 'hanami/utils/string'
|
7
8
|
|
8
9
|
# Define top level constant Boolean, so it can be easily used by other libraries
|
9
10
|
# in coercions DSLs
|
@@ -25,6 +26,14 @@ module Hanami
|
|
25
26
|
# @see Hanami::Utils::Kernel.Integer
|
26
27
|
NUMERIC_MATCHER = /\A([\d\/\.\+iE]+|NaN|Infinity)\z/.freeze
|
27
28
|
|
29
|
+
# @since x.x.x
|
30
|
+
# @api private
|
31
|
+
BOOLEAN_FALSE_STRING = '0'.freeze
|
32
|
+
|
33
|
+
# @since x.x.x
|
34
|
+
# @api private
|
35
|
+
BOOLEAN_TRUE_INTEGER = 1
|
36
|
+
|
28
37
|
# Coerces the argument to be an Array.
|
29
38
|
#
|
30
39
|
# It's similar to Ruby's Kernel.Array, but it applies further
|
@@ -319,7 +328,7 @@ module Hanami
|
|
319
328
|
rescue ArgumentError, TypeError, NoMethodError
|
320
329
|
begin
|
321
330
|
case arg
|
322
|
-
when NilClass, ->(a) { a.respond_to?(:to_i) && a
|
331
|
+
when NilClass, ->(a) { a.respond_to?(:to_i) && numeric?(a) }
|
323
332
|
arg.to_i
|
324
333
|
else
|
325
334
|
raise TypeError.new "can't convert #{inspect_type_error(arg)}into Integer"
|
@@ -334,6 +343,7 @@ module Hanami
|
|
334
343
|
# Coerces the argument to be a BigDecimal.
|
335
344
|
#
|
336
345
|
# @param arg [Object] the argument
|
346
|
+
# @param precision [Keyword] precision for Rational objects (Only JRuby).
|
337
347
|
#
|
338
348
|
# @return [BigDecimal] the result of the coercion
|
339
349
|
#
|
@@ -406,13 +416,12 @@ module Hanami
|
|
406
416
|
# Hanami::Utils::Kernel.BigDecimal(input) # => TypeError
|
407
417
|
def self.BigDecimal(arg)
|
408
418
|
case arg
|
409
|
-
when
|
410
|
-
when Float, Complex, Rational
|
419
|
+
when Numeric
|
411
420
|
BigDecimal(arg.to_s)
|
412
|
-
when ->(a) { a.
|
413
|
-
|
421
|
+
when ->(a) { a.respond_to?(:to_d) }
|
422
|
+
arg.to_d
|
414
423
|
else
|
415
|
-
|
424
|
+
BigDecimal.new(arg)
|
416
425
|
end
|
417
426
|
rescue NoMethodError
|
418
427
|
raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal"
|
@@ -535,7 +544,7 @@ module Hanami
|
|
535
544
|
rescue ArgumentError, TypeError
|
536
545
|
begin
|
537
546
|
case arg
|
538
|
-
when NilClass, ->(a) { a.respond_to?(:to_f) && a
|
547
|
+
when NilClass, ->(a) { a.respond_to?(:to_f) && numeric?(a) }
|
539
548
|
arg.to_f
|
540
549
|
else
|
541
550
|
raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float"
|
@@ -881,9 +890,12 @@ module Hanami
|
|
881
890
|
# Hanami::Utils::Kernel.Boolean(input) # => TypeError
|
882
891
|
def self.Boolean(arg)
|
883
892
|
case arg
|
884
|
-
when Numeric
|
885
|
-
|
886
|
-
when
|
893
|
+
when Numeric
|
894
|
+
arg.to_i == BOOLEAN_TRUE_INTEGER
|
895
|
+
when ::String, Utils::String, BOOLEAN_FALSE_STRING
|
896
|
+
Boolean(arg.to_i)
|
897
|
+
when ->(a) { a.respond_to?(:to_bool) }
|
898
|
+
arg.to_bool
|
887
899
|
else
|
888
900
|
!!arg
|
889
901
|
end
|
@@ -1001,6 +1013,18 @@ module Hanami
|
|
1001
1013
|
raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol"
|
1002
1014
|
end
|
1003
1015
|
|
1016
|
+
# Check if the given argument is a string representation of a number
|
1017
|
+
#
|
1018
|
+
# @param arg [Object] the input
|
1019
|
+
#
|
1020
|
+
# @return [TrueClass,FalseClass]
|
1021
|
+
#
|
1022
|
+
# @since x.x.x
|
1023
|
+
# @api private
|
1024
|
+
def self.numeric?(arg)
|
1025
|
+
!(arg.to_s =~ NUMERIC_MATCHER).nil?
|
1026
|
+
end
|
1027
|
+
|
1004
1028
|
# Returns the most useful type error possible
|
1005
1029
|
#
|
1006
1030
|
# If the object does not respond_to?(:inspect), we return the class, else we
|
data/lib/hanami/utils/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hanami-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Luca Guidi
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-
|
13
|
+
date: 2016-05-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: bundler
|
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
107
|
version: '0'
|
108
108
|
requirements: []
|
109
109
|
rubyforge_project:
|
110
|
-
rubygems_version: 2.
|
110
|
+
rubygems_version: 2.6.4
|
111
111
|
signing_key:
|
112
112
|
specification_version: 4
|
113
113
|
summary: Ruby core extentions and Hanami utilities
|