Module: Hanami::Utils::Kernel
- Defined in:
- gems/gems/hanami-utils-2.2.0.beta1/lib/hanami/utils/kernel.rb
Overview
Kernel utilities
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.
-
.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
Source: | on GitHub
def self.Array(arg) super.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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
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.
Source: | on GitHub
def self.Float(arg) super 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.
Source: | on GitHub
def self.Hash(arg) if arg.respond_to?(:to_h) arg.to_h else super end rescue NoMethodError raise TypeError.new "can't convert #{inspect_type_error(arg)}into Hash" 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.
Source: | on GitHub
def self.Integer(arg) super 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 |
.Pathname(arg) ⇒ Pathname
Coerces the argument to be a Pathname.
Source: | on GitHub
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.
Source: | on GitHub
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
Source: | on GitHub
def self.String(arg) arg = arg.to_str if arg.respond_to?(:to_str) super 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.
Source: | on GitHub
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 # rubocop:disable Lint/DuplicateBranch 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.