Sha256: b6caa77b662a5942644839e5be87120eec57cba2b8f9411e61c9e0a505ef68ab

Contents?: true

Size: 1.71 KB

Versions: 11

Compression:

Stored size: 1.71 KB

Contents

# This module provides all the column helper methods to deal with the
# values and adds the common type management code for the adapters.
module ActiveRecordEnumerations
  module Column
    # Add the values accessor to the column class.
    def self.included(klass)
      klass.module_eval <<-EOE
        def values; @limit; end
      EOE
    end

    # Add the type to the native database types. This will most
    # likely need to be modified in the adapter as well.
    def native_database_types
      types = super
      types[:enum] = { :name => "enum" }
      types
    end

    # The new constructor with a values argument.
    def initialize(name, default, sql_type = nil, null = true, values = nil)
      super(name, default, sql_type, null)
      @limit = values if type == :enum
    end

    # The class for enum is Symbol.
    def klass
      if type == :enum
        Symbol
      else
        super
      end
    end

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

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

    # The enum simple type.
    def simplified_type(field_type)
      if field_type =~ /enum/i
        :enum
      else
        super
      end
    end

    # Safely convert the value to a symbol. 
    def self.value_to_symbol(value)
      case value
      when Symbol
        value
      when String
        value.empty? ? nil : value.intern
      else
        nil
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
smukherjee-openbill-0.1.5 vendor/plugins/enum-column/lib/enum/enum_adapter.rb
smukherjee-openbill-0.1.6 vendor/plugins/enum-column/lib/enum/enum_adapter.rb
smukherjee-openbill-0.1.7 vendor/plugins/enum-column/lib/enum/enum_adapter.rb
enum_column-0.1.6 lib/enum_column/enum_adapter.rb
enum_column-0.1.5 lib/enum_column/enum_adapter.rb
enum_column-0.1.4 lib/enum_column/enum_adapter.rb
enum_column-0.1.2 lib/enum_column/enum_adapter.rb
enum_column-0.1.1 lib/enum/enum_adapter.rb
enum_column-0.1.0 lib/enum/enum_adapter.rb
openbill-0.1.6 vendor/plugins/enum-column/lib/enum/enum_adapter.rb
openbill-0.1.5 vendor/plugins/enum-column/lib/enum/enum_adapter.rb