Module: Sequel::EvalInspect

Included in:
Sequel
Defined in:
lib/sequel/extensions/eval_inspect.rb

Instance Method Summary collapse

Instance Method Details

#eval_inspect(obj) ⇒ Object

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.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sequel/extensions/eval_inspect.rb', line 27

def eval_inspect(obj)
  case obj
  when Sequel::SQL::Blob, Sequel::LiteralString, BigDecimal
    "#{obj.class}.new(#{obj.to_s.inspect})"
  when Sequel::SQL::ValueList
    "#{obj.class}.new(#{obj.to_a.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)
    "#{obj.class}.parse(#{obj.strftime("#{datepart}%T.%N%z").inspect})#{'.utc' if obj.utc?}"
  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})"
  else
    obj.inspect
  end
end