Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-79

Type checking for Ruby time classes is overly strict

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Ruby 1.9.1; MongoDB 1.2.1
    • Minor Change

      Rails ActiveSupport comes with a number of classes to make date/time management in Ruby more flexible. The 'duck typing' conventions of Ruby programming usually make it irrelevant whether a time object is an instance of type Time or a class that supports the same methods.

      The BSON conversion within the mongo-ruby driver breaks these conventions. This creates particular difficulties for Rails developers who work with the config.time_zone parameter set, which causes most times to be cast into ActiveSupport::TimeWithZone. This class, along with Date and DateTime and others, presents an exception "Mongo::InvalidDocument: Unsupported type for BSON (1)".

      My suggestion would be to implement more testing in the bson_type method. In addition to the === Time test that's already in the case statement, you could cast to Time when the object responds to 'to_time'. Other types could be similarly tested for their to_* equivalents as well. If that seems too murky, putting class checks against the common extension classes in the Ruby standard library and in ActiveSupport would be a good 80% solution.

            Assignee:
            kbanker Kyle Banker
            Reporter:
            sfeley Stephen Eley
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: