lib/acfs/resource/attributes/uuid.rb in acfs-1.0.0.dev.1.b305 vs lib/acfs/resource/attributes/uuid.rb in acfs-1.0.0
- old
+ new
@@ -1,20 +1,18 @@
module Acfs::Resource::Attributes
-
# @api public
#
# UUID attribute type. Use it in your model as an attribute type:
#
# @example
# class User < Acfs::Resource
# attribute :id, :uuid
# end
#
class UUID < Base
-
#
- REGEXP = /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i
+ UUID_REGEXP = /[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}/i
# @api public
#
# Check if given object looks like a UUID, eg:
# `450b7a40-94ad-11e3-baa8-0800200c9a66`
@@ -28,26 +26,19 @@
# | 2 | 4 |
# | 3 | 4 |
# | 4 | 4 |
# | 5 | 12 |
#
- # @param [Object] obj Object to cast.
+ # @param [Object] value Object to cast.
# @return [String] Casted object as UUID.
#
- def cast_type(obj)
- cast_string obj.to_s
- end
-
- private
-
- def cast_string(str)
- if nil_allowed? && str.blank?
- return nil
- elsif str =~ REGEXP
- str
+ def cast_value(value)
+ if value.blank?
+ nil
+ elsif value.to_s =~ UUID_REGEXP
+ value
else
- raise ArgumentError.new "given String `#{str}` " \
- "does not look like a UUID"
+ raise TypeError.new "Invalid UUID: `#{value.to_s}'"
end
end
end
# Lower-case alias for automatic type lookup