Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

BSON::ObjectId#inspect returns a string that evals into an object id #38

Merged
merged 1 commit into from Jul 7, 2015

Conversation

tonyta
Copy link
Contributor

@tonyta tonyta commented Jul 6, 2015

This will let BSON::ObjectId#inspect to return a string that can be evaluated into an equivalent object id. This would be super useful for copying and pasting object ids (especially when used with Mongoid).

A BSON.ObjectId constructor method passes the argument to #from_string, allowing object ids to behave kinda like literals.

Seemingly sending an arg to a constant as a constructor is similar to some behavior found in the standard lib: Pathname('./lib') or Hash(a: 1, b: 2)

If this is helpful, perhaps something similar can be implemented for BSON::Binary as well.

Now

>> BSON::ObjectId.new
=> BSON::ObjectId('559afa3e546f6e2631000000')

>> BSON::ObjectId('559afa3e546f6e2631000000')
=> BSON::ObjectId('559afa3e546f6e2631000000')
馃槂馃槂馃槂馃槂

# Mongoid
>> Song.first.attributes
=> {"_id"=>BSON::ObjectId('559b0037546f6e28440a0000'),
   "length"=>219,
   "name"=>"Shake It Off",
   "artist_id"=>BSON::ObjectId('559afff3546f6e2844030000'),
   "album_id"=>BSON::ObjectId('559aff93546f6e2844020000')}

>> {"_id"=>BSON::ObjectId('559b0037546f6e28440a0000'), "length"=>219, "name"=>"Shake It Off", "artist_id"=>BSON::ObjectId('559afff3546f6e2844030000'), "album_id"=>BSON::ObjectId('559aff93546f6e2844020000')}
=> {"_id"=>BSON::ObjectId('559b0037546f6e28440a0000'),
   "length"=>219,
   "name"=>"Shake It Off",
   "artist_id"=>BSON::ObjectId('559afff3546f6e2844030000'),
   "album_id"=>BSON::ObjectId('559aff93546f6e2844020000')}

Before (990c7157)

>> BSON::ObjectId.new
=> <BSON::ObjectId:0x70280035828000 data=559afa3e546f6e2631000000>

>> <BSON::ObjectId:0x70280035828000 data=559afa3e546f6e2631000000>
馃槨 SyntaxError: unexpected '<', expecting end-of-input

Way Before (27d9a915)

>> BSON::ObjectId.new
=> BSON::ObjectId('559afa3e546f6e2631000000')

>> BSON::ObjectId('559afa3e546f6e2631000000')
馃槨 NoMethodError: undefined method `ObjectId' for BSON:Module

Hope this helps!

`BSON::ObjectId('559acc9e546f6e09418d0200')` will now pass the
given string to BSON::ObjectId.from_string and return an equivalent
object id.

This change will allow for copying and pasting object ids without
having to use `#to_s` and `.from_string`, which would be nice when
working between different terminals
@estolfo
Copy link
Contributor

estolfo commented Jul 7, 2015

Cool, thanks!

estolfo added a commit that referenced this pull request Jul 7, 2015
BSON::ObjectId#inspect returns a string that evals into an object id
@estolfo estolfo merged commit 6116ba6 into mongodb:master Jul 7, 2015
@jonhyman
Copy link

@tonyta thanks, I'm upgrading our code base to the new driver and was going to submit something similar, so glad you did this!

@tonyta
Copy link
Contributor Author

tonyta commented Jul 16, 2015

@jonhyman Thank you for all your awesome work! I'm happy to have helped! 馃樃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants