Skip to content

Instantly share code, notes, and snippets.

@skylar
Created July 24, 2014 03:06
Show Gist options
  • Select an option

  • Save skylar/bc9df3067147ddec1e71 to your computer and use it in GitHub Desktop.

Select an option

Save skylar/bc9df3067147ddec1e71 to your computer and use it in GitHub Desktop.
A string-backed Enum type for DataMapper
require 'dm-types/support/flags'
module DataMapper
class Property
class StringEnum < String
min 1
include Flags
def initialize(model, name, options = {})
flags.each do |flag|
fail "Found flag of type #{flag.class}. Only symbols are allowed." unless flag.is_a? Symbol
end
super
end
def load(value)
value.to_sym
end
def dump(value)
value.to_s
end
def typecast_to_primitive(value)
value.to_sym
fail "#{value} is not a valid option" unless flags.include? value
end
end # class Enum
end # class Property
end # module DataMapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment