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

Type checking for Ruby time classes is overly strict

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 12_01_17
    • Component/s: None
    • Labels:
      None
    • Environment:
      Ruby 1.9.1; MongoDB 1.2.1
    • Backwards Compatibility:
      Minor Change

      Description

      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.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: