module Sequel::EvalInspect

  1. lib/sequel/extensions/eval_inspect.rb
Parent: Sequel

Methods

Public Instance

  1. eval_inspect

Public Instance methods

eval_inspect (obj)

Special case objects where inspect does not generally produce input suitable for eval. Used by Sequel::SQL::Expression#inspect so that it can produce a string suitable for eval even if components of the expression have inspect methods that do not produce strings suitable for eval.

[show source]
# File lib/sequel/extensions/eval_inspect.rb, line 22
def eval_inspect(obj)
  case obj
  when Sequel::SQL::Blob, Sequel::LiteralString, Sequel::SQL::ValueList
    "#{obj.class}.new(#{obj.inspect})"
  when Array
    "[#{obj.map{|o| eval_inspect(o)}.join(', ')}]"
  when Hash
    "{#{obj.map{|k, v| "#{eval_inspect(k)} => #{eval_inspect(v)}"}.join(', ')}}"
  when Time
    datepart = "%Y-%m-%dT" unless obj.is_a?(Sequel::SQLTime)
    if RUBY_VERSION < '1.9'
    # :nocov:
      # Time on 1.8 doesn't handle %N (or %z on Windows), manually set the usec value in the string
      hours, mins = obj.utc_offset.divmod(3600)
      mins /= 60
      "#{obj.class}.parse(#{obj.strftime("#{datepart}%H:%M:%S.#{sprintf('%06i%+03i%02i', obj.usec, hours, mins)}").inspect})#{'.utc' if obj.utc?}"
    # :nocov:
    else
      "#{obj.class}.parse(#{obj.strftime("#{datepart}%T.%N%z").inspect})#{'.utc' if obj.utc?}"
    end
  when DateTime
    # Ignore date of calendar reform
    "DateTime.parse(#{obj.strftime('%FT%T.%N%z').inspect})"
  when Date
    # Ignore offset and date of calendar reform
    "Date.new(#{obj.year}, #{obj.month}, #{obj.day})"
  when BigDecimal
    "BigDecimal.new(#{obj.to_s.inspect})"
  else
    obj.inspect
  end
end