Permalink
Browse files

Add Moped::BSON.ObjectId(string)

- For compatibility with apps that may use the 10gen driver API with
  regards to creating object ids from strings.

- No inspection override here.

- Closes #4.
  • Loading branch information...
1 parent 9ab6922 commit 421ceb10902e258e7f509b33d3d3b855616a1eb6 @durran durran committed May 13, 2012
Showing with 69 additions and 0 deletions.
  1. +17 −0 lib/moped/bson.rb
  2. +52 −0 spec/moped/bson_spec.rb
View
@@ -21,5 +21,22 @@ module BSON
FLOAT_PACK = 'E'.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
View
@@ -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.