Sha256: b094c13ecedd1fdb39f47c4239b7c95e18277e1e08b70c1d9c7b8767adb99a5d

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    if defined?(AbstractMysqlAdapter)
      class AbstractMysqlAdapter
        protected
        def initialize_type_map_with_enum(m)
          initialize_without_enum(m)
          register_enum_type(m, %r(^enum)i)
          register_set_type(m, %r(^set)i)
        end

        alias_method :initialize_without_enum, :initialize_type_map
        alias_method :initialize_type_map, :initialize_type_map_with_enum

        def register_enum_type(mapping, key)
          mapping.register_type(key) do |sql_type|
            if sql_type =~ /(?:enum)\(([^)]+)\)/i
              limit = $1.scan( /'([^']*)'/ ).flatten
              Type::Enum.new(limit: limit)
            end
          end
        end

        def register_set_type(mapping, key)
          mapping.register_type(key) do |sql_type|
            if sql_type =~ /(?:set)\(([^)]+)\)/i
              limit = $1.scan( /'([^']*)'/ ).flatten
              Type::Set.new(limit: limit)
            end
          end
        end
      end
    end
  end

  module Type
    class Enum < Type::Value
      def type
        :enum
      end

      def initialize(options = {})
        options.assert_valid_keys(:limit)
        @limit = options[:limit]
      end
    end

    class Set < Type::Value
      def type
        :set
      end

      def initialize(options = {})
        options.assert_valid_keys(:limit)
        @limit = options[:limit]
      end

      def type_cast_from_database(value)
        value.split(",")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
activerecord_enum-0.4.1 lib/activerecord_enum/activerecord_enum_post42.rb
native_enum-1.0.0 lib/native_enum/activerecord_enum_post42.rb
native_enum-1.0.0pre1 lib/native_enum/activerecord_enum_post42.rb
activerecord_enum-0.4.0 lib/activerecord_enum/activerecord_enum_post42.rb