lib/lotus/utils/kernel.rb in lotus-utils-0.4.2 vs lib/lotus/utils/kernel.rb in lotus-utils-0.4.3

- old
+ new

@@ -138,11 +138,11 @@ arg.to_set else Set.new(::Kernel.Array(arg)) end rescue NoMethodError - raise TypeError.new("can't convert into Set") + raise TypeError.new("can't convert #{inspect_type_error(arg)}into Set") end # Coerces the argument to be a Hash. # # @param arg [Object] the input @@ -203,11 +203,11 @@ arg.to_h else super(arg) end rescue NoMethodError - raise TypeError.new "can't convert into Hash" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Hash" end else def self.Hash(arg) case arg when ::Hash then arg @@ -215,11 +215,11 @@ when ->(a) { a.respond_to?(:to_h) } then arg.to_h else super(arg) end rescue ArgumentError, NoMethodError - raise TypeError.new "can't convert into Hash" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Hash" end end # Coerces the argument to be an Integer. # @@ -334,17 +334,17 @@ 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 into Integer" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Integer" end rescue NoMethodError - raise TypeError.new "can't convert into Integer" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Integer" end rescue RangeError - raise TypeError.new "can't convert into Integer" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Integer" end # Coerces the argument to be a BigDecimal. # # @param arg [Object] the argument @@ -424,14 +424,14 @@ 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 into BigDecimal" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal" end rescue NoMethodError - raise TypeError.new "can't convert into BigDecimal" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into BigDecimal" end # Coerces the argument to be a Float. # # It's similar to Ruby's Kernel.Float, but it doesn't stop at the first @@ -550,17 +550,17 @@ 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 into Float" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float" end rescue NoMethodError - raise TypeError.new "can't convert into Float" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float" end rescue RangeError - raise TypeError.new "can't convert into Float" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float" end # Coerces the argument to be a String. # # Identical behavior of Ruby's Kernel.Array, still here because we want @@ -657,18 +657,18 @@ arg.to_s else super(arg) end rescue NoMethodError - raise TypeError.new "can't convert into String" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into String" end else def self.String(arg) arg = arg.to_str if arg.respond_to?(:to_str) super(arg) rescue NoMethodError - raise TypeError.new "can't convert into String" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into String" end end # Coerces the argument to be a Date. # @@ -729,11 +729,11 @@ arg.to_date else Date.parse(arg.to_s) end rescue ArgumentError, NoMethodError - raise TypeError.new "can't convert into Date" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Date" end # Coerces the argument to be a DateTime. # # @param arg [Object] the argument @@ -797,11 +797,11 @@ when Numeric then DateTime(Time.at(arg)) else DateTime.parse(arg.to_s) end rescue ArgumentError, NoMethodError - raise TypeError.new "can't convert into DateTime" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into DateTime" end # Coerces the argument to be a Time. # # @param arg [Object] the argument @@ -862,11 +862,11 @@ when Numeric then Time.at(arg) else Time.parse(arg.to_s) end rescue ArgumentError, NoMethodError - raise TypeError.new "can't convert into Time" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Time" end # Coerces the argument to be a Boolean. # # @param arg [Object] the argument @@ -915,11 +915,11 @@ when ->(a) { a.respond_to?(:to_bool) } then arg.to_bool else !!arg end rescue NoMethodError - raise TypeError.new "can't convert into Boolean" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Boolean" end # Coerces the argument to be a Pathname. # # @param arg [#to_pathname,#to_str] the argument @@ -973,11 +973,11 @@ when ->(a) { a.respond_to?(:to_pathname) } then arg.to_pathname else super end rescue NoMethodError - raise TypeError.new "can't convert into Pathname" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Pathname" end # Coerces the argument to be a String. # # @param arg [#to_sym] the argument @@ -1019,16 +1019,41 @@ # # Missing #respond_to? # input = BasicObject.new # Lotus::Utils::Kernel.Symbol(input) # => TypeError def self.Symbol(arg) case arg - when '' then raise TypeError.new "can't convert into Symbol" + 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 into Symbol" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol" end rescue NoMethodError - raise TypeError.new "can't convert into Symbol" + raise TypeError.new "can't convert #{inspect_type_error(arg)}into Symbol" + end + + # 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. + # + # @since 0.4.3 + # @api private + 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 + + class << self + private :inspect_type_error end end end end