hanami-utils 0.7.1 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5efcbfde57472291defc6d0461a2e481f6783549
4
- data.tar.gz: aea84dd95e00df2e2a213f7af4c828bcbccea2ef
3
+ metadata.gz: 4cbb5f4adac5f75b2e56d9cb8bc2f2a5b91cf5e5
4
+ data.tar.gz: da1b1e8502a4b5fd322bfd9ee660d8b4a1c8d9c8
5
5
  SHA512:
6
- metadata.gz: 8938d0f2e8a9491d8c8b4bd6752c3d820ec845dcf7b88cdfd6f7421d10b1ef9dc86b08162c0e4a7cba7134cea4d401c43ab1a623de88923072b52989e205d556
7
- data.tar.gz: 64bf55cee7347ec7a8627cd8185e3e5a891a414699d00e2e9c60ea05ebe4837eea70fd1348393dd8d7a21df9deac3e1af017b3a134e793d6f720f630f7818718
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.
@@ -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.to_s.match(NUMERIC_MATCHER) }
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 ->(a) { a.respond_to?(:to_d) } then arg.to_d
410
- when Float, Complex, Rational
419
+ when Numeric
411
420
  BigDecimal(arg.to_s)
412
- when ->(a) { a.to_s.match(NUMERIC_MATCHER) }
413
- BigDecimal.new(arg)
421
+ when ->(a) { a.respond_to?(:to_d) }
422
+ arg.to_d
414
423
  else
415
- raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal"
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.to_s.match(NUMERIC_MATCHER) }
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 then arg > 0 && arg <= 1
885
- when String, '0' then Boolean(arg.to_i)
886
- when ->(a) { a.respond_to?(:to_bool) } then arg.to_bool
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
@@ -3,6 +3,6 @@ module Hanami
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.7.1'.freeze
6
+ VERSION = '0.7.2'.freeze
7
7
  end
8
8
  end
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.1
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-02-05 00:00:00.000000000 Z
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.5.1
110
+ rubygems_version: 2.6.4
111
111
  signing_key:
112
112
  specification_version: 4
113
113
  summary: Ruby core extentions and Hanami utilities