Module: ActiveRecordEnumerations::Column

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Add the values accessor to the column class.



7
8
9
10
11
# File 'lib/enum_column/enum_adapter.rb', line 7

def self.included(klass)
  klass.module_eval <<-EOE
    def values; @limit; end
  EOE
end

.value_to_symbol(value) ⇒ Object

Safely convert the value to a symbol.



65
66
67
68
69
70
71
72
73
74
# File 'lib/enum_column/enum_adapter.rb', line 65

def self.value_to_symbol(value)
  case value
  when Symbol
    value
  when String
    value.empty? ? nil : value.intern
  else
    nil
  end
end

Instance Method Details

#initialize(name, default, sql_type = nil, null = true, values = nil) ⇒ Object

The new constructor with a values argument.



22
23
24
25
# File 'lib/enum_column/enum_adapter.rb', line 22

def initialize(name, default, sql_type = nil, null = true, values = nil)
  super(name, default, sql_type, null)
  @limit = values if type == :enum
end

#klassObject

The class for enum is Symbol.



28
29
30
31
32
33
34
# File 'lib/enum_column/enum_adapter.rb', line 28

def klass
  if type == :enum
    Symbol
  else
    super
  end
end

#native_database_typesObject

Add the type to the native database types. This will most likely need to be modified in the adapter as well.



15
16
17
18
19
# File 'lib/enum_column/enum_adapter.rb', line 15

def native_database_types
  types = super
  types[:enum] = { :name => "enum" }
  types
end

#simplified_type(field_type) ⇒ Object

The enum simple type.



56
57
58
59
60
61
62
# File 'lib/enum_column/enum_adapter.rb', line 56

def simplified_type(field_type)
  if field_type =~ /enum/i
    :enum
  else
    super
  end
end

#type_cast(value) ⇒ Object

Convert to a symbol.



37
38
39
40
41
42
43
44
# File 'lib/enum_column/enum_adapter.rb', line 37

def type_cast(value)
  return nil if value.nil?
  if type == :enum
    ActiveRecordEnumerations::Column.value_to_symbol(value)
  else
    super
  end
end

#type_cast_code(var_name) ⇒ Object

Code to convert to a symbol.



47
48
49
50
51
52
53
# File 'lib/enum_column/enum_adapter.rb', line 47

def type_cast_code(var_name)
  if type == :enum
    "ActiveRecordEnumerations::Column.value_to_symbol(#{var_name})"
  else
    super
  end
end