forked from cgriego/active_attr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This introduces a UUID typecasting option, which effectively functions as a string, but coerced to nil unless the string exactly matches the UUID pattern. This fixes issue cgriego#174
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module ActiveAttr | ||
class UUID | ||
end | ||
|
||
module Typecasting | ||
# Typecasts an Object to a v4 UUID | ||
# | ||
# @example Usage | ||
# UUIDTypecaster.new.call("6ed806a8-60ec-4930-ae63-9075bf37ca41") #=> "6ed806a8-60ec-4930-ae63-9075bf37ca41" | ||
# UUIDTypecaster.new.call("6ed806a8-60ec") #=> nil | ||
# | ||
# @since 0.15.0 | ||
class UUIDTypecaster | ||
# Typecasts an object to a v4 UUID | ||
# | ||
# Attempts to convert using SecureRandom | ||
# | ||
# @example Typecast a partial UUID | ||
# typecaster.call("6ed806a8-60ec") #=> nil | ||
# | ||
# @param [Object, #to_s] value The object to typecast | ||
# | ||
# @return [String, nil] The result of typecasting | ||
# | ||
# @since 0.5.0 | ||
def call(value) | ||
regex = /\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b/ | ||
value.to_s.match?(regex) ? value : nil | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "spec_helper" | ||
require "active_attr/typecasting/uuid_typecaster" | ||
|
||
module ActiveAttr | ||
module Typecasting | ||
describe UUIDTypecaster do | ||
subject(:typecaster) { described_class.new } | ||
|
||
describe "#call" do | ||
it "returns the original string for a UUID" do | ||
value = "6ed806a8-60ec-4930-ae63-9075bf37ca41" | ||
typecaster.call(value).should equal value | ||
end | ||
|
||
it "returns nil for an invalid UUID" do | ||
typecaster.call("6ed806a8-60ec-4930-ae63-9075bf37").should be_nil | ||
end | ||
|
||
it "returns nil for nil" do | ||
typecaster.call(nil).should be_nil | ||
end | ||
end | ||
end | ||
end | ||
end |