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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sequel/extensions/eval_inspect.rb', line 23

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