Skip to content

Commit

Permalink
Add Moped::BSON.ObjectId(string)
Browse files Browse the repository at this point in the history
- For compatibility with apps that may use the 10gen driver API with
  regards to creating object ids from strings.

- No inspection override here.

- Closes mongoid#4.
  • Loading branch information
durran committed May 13, 2012
1 parent 9ab6922 commit 421ceb1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/moped/bson.rb
Expand Up @@ -21,5 +21,22 @@ module BSON
FLOAT_PACK = 'E'.freeze FLOAT_PACK = 'E'.freeze


START_LENGTH = [0].pack(INT32_PACK).freeze START_LENGTH = [0].pack(INT32_PACK).freeze

class << self

# Create a new object id from the provided string.
#
# @example Create a new object id.
# Moped::BSON::ObjectId("4faf83c7dbf89b7b29000001")
#
# @param [ String ] string The string to use.
#
# @return [ ObjectId ] The object id.
#
# @since 1.0.0
def ObjectId(string)
ObjectId.from_string(string)
end
end
end end
end end
52 changes: 52 additions & 0 deletions spec/moped/bson_spec.rb
@@ -0,0 +1,52 @@
require "spec_helper"

describe Moped::BSON do

describe ".ObjectId" do

context "when provided a string" do

context "when the string is a valid id" do

let(:id) do
described_class.ObjectId("4faf83c7dbf89b7b29000001")
end

let(:expected) do
Moped::BSON::ObjectId.from_string("4faf83c7dbf89b7b29000001")
end

it "returns an object id" do
id.should eq(expected)
end
end

context "when the string is not a valid id" do

it "raises an error" do
expect {
described_class.ObjectId("test")
}.to raise_error(Moped::Errors::InvalidObjectId)
end
end
end

context "when provided a non string" do

it "raises an error" do
expect {
described_class.ObjectId(1)
}.to raise_error
end
end

context "when provided nil" do

it "raises an error" do
expect {
described_class.ObjectId(nil)
}.to raise_error
end
end
end
end

0 comments on commit 421ceb1

Please sign in to comment.