Created
October 6, 2008 22:37
-
-
Save paul/15160 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class DataMapper::Property | |
# translate the value from external source to expected object | |
def typecast(value) | |
value | |
end | |
# translate the value from the primitive to expected object | |
def load(value) | |
value | |
end | |
# translate the value from expected object to the primitive | |
def dump(value) | |
value | |
end | |
end | |
# defaults are specified on a per-type basis as before | |
class DataMapper::Property::String < DataMapper::Property | |
supports_options :length, :size, :default, :nullable, ... | |
DEFAULT_LENGTH = 50 | |
# the initialize can be specific to each type. the current | |
# initialize method in DM::Property is monolithic and has logic to | |
# support all of the basic types. with this new organization the | |
# logic can be separated out on a per-subclass basis. | |
def initialize(options) | |
super | |
@length = options.fetch(:length, DEFAULT_LENGTH) | |
end | |
end | |
# you can specify the "adapter" specific configuration within | |
# the adapters themselves | |
class DataMapper::Adapters::DataObjectsAdapter | |
DataMapper::Property::String.adapter(self) do | |
native_type 'VARCHAR' | |
end | |
end | |
# or within the types if you wish | |
class DataMapper::Property::UUID < DataMapper::Property::String | |
def typecast(value) | |
value.kind_of?(::UUID) ? value : load(value) | |
end | |
def load(value) | |
::UUID.parse(value) unless value.nil? | |
end | |
def dump(value) | |
value.to_s unless value.nil? | |
end | |
# would use VARCHAR for all DOA subclasses because it's | |
# defined for the parent DM::Property::String class, but for | |
# PG it would use the native UUID type | |
adapter(PostgresAdapter) do | |
native_type 'UUID' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment