Class: DataMapper::Types::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb

Class Method Summary collapse

Class Method Details

.[](*flags) ⇒ Object



27
28
29
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 27

def self.[](*flags)
  new(*flags)
end

.dump(value, property) ⇒ Object



35
36
37
38
39
40
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 35

def self.dump(value, property)
  case value
    when Array then value.collect { |v| self.dump(v, property) }
    else            self.flag_map.invert[value]
  end
end

.flag_mapObject



8
9
10
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 8

def self.flag_map
  @flag_map
end

.flag_map=(value) ⇒ Object



12
13
14
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 12

def self.flag_map=(value)
  @flag_map = value
end

.inherited(target) ⇒ Object



4
5
6
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 4

def self.inherited(target)
  target.instance_variable_set("@primitive", self.primitive)
end

.load(value, property) ⇒ Object



31
32
33
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 31

def self.load(value, property)
  self.flag_map[value]
end

.new(*flags) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 16

def self.new(*flags)
  enum = Class.new(Enum)
  enum.flag_map = {}

  flags.each_with_index do |flag, i|
    enum.flag_map[i + 1] = flag
  end

  enum
end

.typecast(value, property) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/gems/dm-types-0.9.9/lib/dm-types/enum.rb', line 42

def self.typecast(value, property)
  # Attempt to typecast using the class of the first item in the map.
  return value if value.nil?
  case self.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