Module | Sequel::EvalInspect |
In: |
lib/sequel/extensions/eval_inspect.rb
|
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.
# File lib/sequel/extensions/eval_inspect.rb, line 22 22: def eval_inspect(obj) 23: case obj 24: when Sequel::SQL::Blob, Sequel::LiteralString, Sequel::SQL::ValueList 25: "#{obj.class}.new(#{obj.inspect})" 26: when Array 27: "[#{obj.map{|o| eval_inspect(o)}.join(', ')}]" 28: when Hash 29: "{#{obj.map{|k, v| "#{eval_inspect(k)} => #{eval_inspect(v)}"}.join(', ')}}" 30: when Time 31: datepart = "%Y-%m-%dT" unless obj.is_a?(Sequel::SQLTime) 32: if RUBY_VERSION < '1.9' 33: # :nocov: 34: # Time on 1.8 doesn't handle %N (or %z on Windows), manually set the usec value in the string 35: hours, mins = obj.utc_offset.divmod(3600) 36: mins /= 60 37: "#{obj.class}.parse(#{obj.strftime("#{datepart}%H:%M:%S.#{sprintf('%06i%+03i%02i', obj.usec, hours, mins)}").inspect})#{'.utc' if obj.utc?}" 38: # :nocov: 39: else 40: "#{obj.class}.parse(#{obj.strftime("#{datepart}%T.%N%z").inspect})#{'.utc' if obj.utc?}" 41: end 42: when DateTime 43: # Ignore date of calendar reform 44: "DateTime.parse(#{obj.strftime('%FT%T.%N%z').inspect})" 45: when Date 46: # Ignore offset and date of calendar reform 47: "Date.new(#{obj.year}, #{obj.month}, #{obj.day})" 48: when BigDecimal 49: "BigDecimal.new(#{obj.to_s.inspect})" 50: else 51: obj.inspect 52: end 53: end