Permalink
Browse files

Ensure uuid returns self if already uuid. Also sets store default.

  • Loading branch information...
1 parent 2d5b932 commit 968d1ee03804b9f614945aed4f79ac92ad7cce20 @jnunemaker committed Nov 19, 2012
Showing with 35 additions and 22 deletions.
  1. +8 −4 lib/toy/extensions/uuid.rb
  2. +27 −18 spec/toy/extensions/uuid_spec.rb
View
@@ -1,14 +1,18 @@
module Toy
module Extensions
module UUID
+ def store_default
+ new
+ end
+
def to_store(value, *)
- return value if value.is_a?(SimpleUUID::UUID)
- SimpleUUID::UUID.new(value)
+ return value if value.is_a?(self)
+ new(value)
end
def from_store(value, *)
- return value if value.is_a?(SimpleUUID::UUID)
- SimpleUUID::UUID.new(value)
+ return value if value.is_a?(self)
+ new(value)
end
end
end
@@ -1,29 +1,38 @@
require 'helper'
-describe "SimpleUUID::UUID.to_store" do
- it "returns value if value is already uuid" do
- uuid = SimpleUUID::UUID.new
- SimpleUUID::UUID.to_store(uuid).should be(uuid)
+describe SimpleUUID::UUID do
+ describe ".store_default" do
+ it "returns new instance of simple uuid" do
+ value = described_class.store_default
+ value.should be_instance_of(described_class)
+ end
end
- it "converts value to uuid if not already uuid" do
- uuid = SimpleUUID::UUID.new
- [uuid.to_guid, uuid.to_s, uuid.to_i].each do |value|
- SimpleUUID::UUID.from_store(value).should eq(uuid)
+ describe ".to_store" do
+ it "returns value if value is already uuid" do
+ uuid = described_class.new
+ described_class.to_store(uuid).should be(uuid)
end
- end
-end
-describe "SimpleUUID::UUID.from_store" do
- it "returns value if value is already uuid" do
- uuid = SimpleUUID::UUID.new
- SimpleUUID::UUID.from_store(uuid).should be(uuid)
+ it "converts value to uuid if not already uuid" do
+ uuid = described_class.new
+ [uuid.to_guid, uuid.to_s, uuid.to_i].each do |value|
+ described_class.from_store(value).should eq(uuid)
+ end
+ end
end
- it "converts value to uuid if not already uuid" do
- uuid = SimpleUUID::UUID.new
- [uuid.to_guid, uuid.to_s, uuid.to_i].each do |value|
- SimpleUUID::UUID.from_store(value).should eq(uuid)
+ describe ".from_store" do
+ it "returns value if value is already uuid" do
+ uuid = described_class.new
+ described_class.from_store(uuid).should be(uuid)
+ end
+
+ it "converts value to uuid if not already uuid" do
+ uuid = described_class.new
+ [uuid.to_guid, uuid.to_s, uuid.to_i].each do |value|
+ described_class.from_store(value).should eq(uuid)
+ end
end
end
end

0 comments on commit 968d1ee

Please sign in to comment.