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