Class: DataMapper::Property::Enum
- Inherits:
-
Object
- Object
- DataMapper::Property::Enum
- Includes:
- Flags
- Defined in:
- lib/dm-types/enum.rb
Instance Method Summary (collapse)
- - (Object) dump(value)
-
- (Enum) initialize(model, name, options = {})
constructor
A new instance of Enum.
- - (Object) load(value)
- - (Object) typecast(value)
Methods included from Flags
Constructor Details
- (Enum) initialize(model, name, options = {})
A new instance of Enum
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/dm-types/enum.rb', line 12 def initialize(model, name, = {}) @flag_map = {} flags = .fetch(:flags, self.class.flags) flags.each_with_index do |flag, i| @flag_map[i + 1] = flag end if self.class..include?(:set) && !.include?(:set) [:set] = @flag_map.values_at(*@flag_map.keys.sort) end super end |
Instance Method Details
- (Object) dump(value)
31 32 33 34 35 36 |
# File 'lib/dm-types/enum.rb', line 31 def dump(value) case value when ::Array then value.collect { |v| dump(v) } else flag_map.invert[value] end end |
- (Object) load(value)
27 28 29 |
# File 'lib/dm-types/enum.rb', line 27 def load(value) flag_map[value] end |
- (Object) typecast(value)
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dm-types/enum.rb', line 38 def typecast(value) return if value.nil? # 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 |