-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
As implied in the subject, I've been playing with monkeypatching Time to give me more output options, but it doesn't seem to be working. I feel as though I've narrowed it down to something in Mongoid since a normally-created Time object serializes as-expected . . . but I can't find anything in the Mongoid source code to create this problem, so I figured I'd bring it up here.
My monkeypatching:
class Time def as_json(*args) options = args.extract_options! puts " -> in time.as_json(#{options.to_json})" return to_s if !options[:schema] case options[:schema] when :mobile return self.to_i when :unix_timestamps return self.to_i else to_s end end def to_json(*args) puts " -> in time.to_json(#{args.to_json})" as_json(*args).to_json end def to_s(*args) puts " -> in time.to_s(#{args.to_json}" self.to_i end end
The test code:
_time = Time.new _created_at = User.first.created_at puts '------------------------------' puts "_time" puts "class: #{_time.class.name}" puts "superclass: #{_time.class.superclass.name}" puts "superclass superclass: #{_time.class.superclass.superclass.name}" puts _time.to_json(:schema => :unix_timestamps) puts '' puts "_created_at" puts "class: #{_created_at.class.name}" puts "superclass: #{_created_at.class.superclass.name}" puts "superclass superclass: #{_created_at.class.superclass.superclass.name}" puts _created_at.to_json(:schema => :unix_timestamps) puts '------------------------------'
And the output:
------------------------------ _time class: Time superclass: Object superclass superclass: BasicObject -> in time.to_json([{"schema":"unix_timestamps"}]) -> in time.as_json({"schema":"unix_timestamps"}) 1347675471 _created_at class: Time superclass: Object superclass superclass: BasicObject "2011-11-12T02:30:02Z" ------------------------------
any ideas? I'm running mongoid-3.0.5, rails-3.2.0, ruby 1.9.3p194
Thanks!