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
/\A([\d\/\.\+iE]+|NaN|Infinity)\z/.freeze
Class Method Summary collapse
-
.Array(arg) ⇒ Array
Coerces the argument to be an Array.
-
.BigDecimal(arg) ⇒ 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.
-
.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
81 82 83 84 85 86 87 |
# File 'lib/hanami/utils/kernel.rb', line 81 def self.Array(arg) super(arg).dup.tap do |a| a.flatten! a.compact! a.uniq! end end |
.BigDecimal(arg) ⇒ BigDecimal
Coerces the argument to be a BigDecimal.
407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/hanami/utils/kernel.rb', line 407 def self.BigDecimal(arg) case arg when ->(a) { a.respond_to?(:to_d) } then arg.to_d when Float, Complex, Rational BigDecimal(arg.to_s) when ->(a) { a.to_s.match(NUMERIC_MATCHER) } BigDecimal.new(arg) else raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal" 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.
882 883 884 885 886 887 888 889 890 891 892 |
# File 'lib/hanami/utils/kernel.rb', line 882 def self.Boolean(arg) case arg when Numeric then arg > 0 && arg <= 1 when String, '0' then Boolean(arg.to_i) when ->(a) { a.respond_to?(:to_bool) } then 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.
698 699 700 701 702 703 704 705 706 |
# File 'lib/hanami/utils/kernel.rb', line 698 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.
765 766 767 768 769 770 771 772 773 774 |
# File 'lib/hanami/utils/kernel.rb', line 765 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.
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/hanami/utils/kernel.rb', line 533 def self.Float(arg) super(arg) rescue ArgumentError, TypeError begin case arg when NilClass, ->(a) { a.respond_to?(:to_f) && a.to_s.match(NUMERIC_MATCHER) } 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.
200 201 202 203 204 205 206 207 208 |
# File 'lib/hanami/utils/kernel.rb', line 200 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.
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 |
# File 'lib/hanami/utils/kernel.rb', line 1012 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.
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/hanami/utils/kernel.rb', line 317 def self.Integer(arg) super(arg) rescue ArgumentError, TypeError, NoMethodError begin case arg when NilClass, ->(a) { a.respond_to?(:to_i) && a.to_s.match(NUMERIC_MATCHER) } 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 |
.Pathname(arg) ⇒ Pathname
Coerces the argument to be a Pathname.
942 943 944 945 946 947 948 949 950 |
# File 'lib/hanami/utils/kernel.rb', line 942 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.
136 137 138 139 140 141 142 143 144 |
# File 'lib/hanami/utils/kernel.rb', line 136 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
637 638 639 640 641 642 |
# File 'lib/hanami/utils/kernel.rb', line 637 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.
993 994 995 996 997 998 999 1000 1001 1002 |
# File 'lib/hanami/utils/kernel.rb', line 993 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.
830 831 832 833 834 835 836 837 838 839 |
# File 'lib/hanami/utils/kernel.rb', line 830 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 |