Created
July 24, 2014 03:06
-
-
Save skylar/bc9df3067147ddec1e71 to your computer and use it in GitHub Desktop.
A string-backed Enum type for DataMapper
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
| 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