Module: Hanami::Utils::Kernel
- Defined in:
- lib/hanami/utils/kernel.rb
Overview
Kernel utilities
Constant Summary collapse
- NUMERIC_MATCHER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Matcher for numeric values
%r{\A([\d\/\.\+iE]+|NaN|Infinity)\z}.freeze
- BOOLEAN_FALSE_STRING =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
"0"
- BOOLEAN_TRUE_INTEGER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
1
Class Method Summary collapse
-
.Array(arg) ⇒ Array
Coerces the argument to be an Array.
-
.BigDecimal(arg, precision = ::Float::DIG) ⇒ BigDecimal
Coerces the argument to be a BigDecimal.
-
.Boolean(arg) ⇒ true, false
Coerces the argument to be a Boolean.
-
.Date(arg) ⇒ Date
Coerces the argument to be a Date.
-
.DateTime(arg) ⇒ DateTime
Coerces the argument to be a DateTime.
-
.Float(arg) ⇒ Float
Coerces the argument to be a Float.
-
.Hash(arg) ⇒ Hash
Coerces the argument to be a Hash.
-
.inspect_type_error(arg) ⇒ Object
private
Returns the most useful type error possible.
-
.Integer(arg) ⇒ Fixnum
Coerces the argument to be an Integer.
-
.numeric?(arg) ⇒ TrueClass, FalseClass
private
Checks if the given argument is a string representation of a number.
-
.Pathname(arg) ⇒ Pathname
Coerces the argument to be a Pathname.
-
.Set(arg) ⇒ Set
Coerces the argument to be a Set.
-
.String(arg) ⇒ String
Coerces the argument to be a String.
-
.Symbol(arg) ⇒ Symbol
Coerces the argument to be a Symbol.
-
.Time(arg) ⇒ Time
Coerces the argument to be a Time.
Class Method Details
.Array(arg) ⇒ Array
Coerces the argument to be an Array.
It’s similar to Ruby’s Kernel.Array, but it applies further transformations:
* flatten
* compact
* uniq
94 95 96 97 98 99 100 |
# File 'lib/hanami/utils/kernel.rb', line 94 def self.Array(arg) super(arg).dup.tap do |a| a.flatten! a.compact! a.uniq! end end |
.BigDecimal(arg, precision = ::Float::DIG) ⇒ BigDecimal
Coerces the argument to be a BigDecimal.
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'lib/hanami/utils/kernel.rb', line 421 def self.BigDecimal(arg, precision = ::Float::DIG) case arg when NilClass # This is only needed by Ruby 2.6 raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal" when Rational arg.to_d(precision) when Numeric BigDecimal(arg.to_s) when ->(a) { a.respond_to?(:to_d) } arg.to_d else ::Kernel.BigDecimal(arg, precision) end rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal" end |
.Boolean(arg) ⇒ true, false
Coerces the argument to be a Boolean.
900 901 902 903 904 905 906 907 908 909 910 911 912 913 |
# File 'lib/hanami/utils/kernel.rb', line 900 def self.Boolean(arg) case arg when Numeric arg.to_i == BOOLEAN_TRUE_INTEGER when ::String, Utils::String, BOOLEAN_FALSE_STRING Boolean(arg.to_i) when ->(a) { a.respond_to?(:to_bool) } arg.to_bool else !!arg end rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Boolean" end |
.Date(arg) ⇒ Date
Coerces the argument to be a Date.
715 716 717 718 719 720 721 722 723 |
# File 'lib/hanami/utils/kernel.rb', line 715 def self.Date(arg) if arg.respond_to?(:to_date) arg.to_date else Date.parse(arg.to_s) end rescue ArgumentError, NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Date" end |
.DateTime(arg) ⇒ DateTime
Coerces the argument to be a DateTime.
783 784 785 786 787 788 789 790 791 792 |
# File 'lib/hanami/utils/kernel.rb', line 783 def self.DateTime(arg) case arg when ->(a) { a.respond_to?(:to_datetime) } then arg.to_datetime when Numeric then DateTime(Time.at(arg)) else DateTime.parse(arg.to_s) end rescue ArgumentError, NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into DateTime" end |
.Float(arg) ⇒ Float
Coerces the argument to be a Float.
It’s similar to Ruby’s Kernel.Float, but it doesn’t stop at the first error and raise an exception only when the argument can’t be coerced.
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/hanami/utils/kernel.rb', line 550 def self.Float(arg) super(arg) rescue ArgumentError, TypeError begin case arg when NilClass, ->(a) { a.respond_to?(:to_f) && numeric?(a) } arg.to_f else raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float" end rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float" end rescue RangeError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float" end |
.Hash(arg) ⇒ Hash
Coerces the argument to be a Hash.
213 214 215 216 217 218 219 220 221 |
# File 'lib/hanami/utils/kernel.rb', line 213 def self.Hash(arg) if arg.respond_to?(:to_h) arg.to_h else super(arg) end rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Hash" end |
.inspect_type_error(arg) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the most useful type error possible
If the object does not respond_to?(:inspect), we return the class, else we return nil. In all cases, this method is tightly bound to callers, as this method appends the required space to make the error message look good.
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
# File 'lib/hanami/utils/kernel.rb', line 1045 def self.inspect_type_error(arg) (arg.respond_to?(:inspect) ? arg.inspect : arg.to_s) + " " rescue NoMethodError # missing the #respond_to? method, fall back to returning the class' name begin arg.class.name + " instance " rescue NoMethodError # missing the #class method, can't fall back to anything better than nothing # Callers will have to guess from their code nil end end |
.Integer(arg) ⇒ Fixnum
Coerces the argument to be an Integer.
It’s similar to Ruby’s Kernel.Integer, but it doesn’t stop at the first error and raise an exception only when the argument can’t be coerced.
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/hanami/utils/kernel.rb', line 330 def self.Integer(arg) super(arg) rescue ArgumentError, TypeError, NoMethodError begin case arg when NilClass, ->(a) { a.respond_to?(:to_i) && numeric?(a) } arg.to_i else raise TypeError.new "can't convert #{inspect_type_error(arg)}into Integer" end rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Integer" end rescue RangeError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Integer" end |
.numeric?(arg) ⇒ TrueClass, FalseClass
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Checks if the given argument is a string representation of a number
1033 1034 1035 |
# File 'lib/hanami/utils/kernel.rb', line 1033 def self.numeric?(arg) !(arg.to_s =~ NUMERIC_MATCHER).nil? end |
.Pathname(arg) ⇒ Pathname
Coerces the argument to be a Pathname.
963 964 965 966 967 968 969 970 971 |
# File 'lib/hanami/utils/kernel.rb', line 963 def self.Pathname(arg) case arg when ->(a) { a.respond_to?(:to_pathname) } then arg.to_pathname else super end rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Pathname" end |
.Set(arg) ⇒ Set
Coerces the argument to be a Set.
149 150 151 152 153 154 155 156 157 |
# File 'lib/hanami/utils/kernel.rb', line 149 def self.Set(arg) if arg.respond_to?(:to_set) arg.to_set else Set.new(::Kernel.Array(arg)) end rescue NoMethodError raise TypeError.new("can't convert #{inspect_type_error(arg)}into Set") end |
.String(arg) ⇒ String
Coerces the argument to be a String.
Identical behavior of Ruby’s Kernel.Array, still here because we want to keep the interface consistent
654 655 656 657 658 659 |
# File 'lib/hanami/utils/kernel.rb', line 654 def self.String(arg) arg = arg.to_str if arg.respond_to?(:to_str) super(arg) rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into String" end |
.Symbol(arg) ⇒ Symbol
Coerces the argument to be a Symbol.
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 |
# File 'lib/hanami/utils/kernel.rb', line 1014 def self.Symbol(arg) case arg when "" then raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol" when ->(a) { a.respond_to?(:to_sym) } then arg.to_sym else raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol" end rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol" end |
.Time(arg) ⇒ Time
Coerces the argument to be a Time.
848 849 850 851 852 853 854 855 856 857 |
# File 'lib/hanami/utils/kernel.rb', line 848 def self.Time(arg) case arg when ->(a) { a.respond_to?(:to_time) } then arg.to_time when Numeric then Time.at(arg) else Time.parse(arg.to_s) end rescue ArgumentError, NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Time" end |