Class: DataMapper::Property::Enum
- Inherits:
-
Integer
- Object
- Integer
- DataMapper::Property::Enum
- Includes:
- Flags
- Defined in:
- lib/dm-types/enum.rb
Instance Method Summary collapse
- #dump(value) ⇒ Object
-
#initialize(model, name, options = {}) ⇒ Enum
constructor
A new instance of Enum.
- #load(value) ⇒ Object
- #typecast_to_primitive(value) ⇒ Object
Methods included from Flags
Constructor Details
#initialize(model, name, options = {}) ⇒ Enum
Returns a new instance of Enum.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dm-types/enum.rb', line 10 def initialize(model, name, = {}) super @flag_map = {} flags = .fetch(:flags, self.class.flags) flags.each_with_index do |flag, i| @flag_map[i + 1] = flag end if defined?(::DataMapper::Validations) unless model.skip_auto_validation_for?(self) if self.class.ancestors.include?(Property::Enum) allowed = flag_map.values_at(*flag_map.keys.sort) model.validates_within name, model.({ :set => allowed }, self, :within) end end end end |
Instance Method Details
#dump(value) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/dm-types/enum.rb', line 34 def dump(value) case value when ::Array then value.collect { |v| dump(v) } else flag_map.invert[value] end end |
#load(value) ⇒ Object
30 31 32 |
# File 'lib/dm-types/enum.rb', line 30 def load(value) flag_map[value] end |
#typecast_to_primitive(value) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/dm-types/enum.rb', line 41 def typecast_to_primitive(value) # Attempt to typecast using the class of the first item in the map. case flag_map[1] when ::Symbol then value.to_sym when ::String then value.to_s when ::Fixnum then value.to_i else value end end |