Sha256: 303d858bf4a60c65e391da114ca9651ecd34996053f578e2771394594eafa23e

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

require "active_record/connection_adapters/abstract/quoting"

module Rein
  module Type
    # This module contains methods for defining enum types.
    module Enum
      include ActiveRecord::ConnectionAdapters::Quoting

      def create_enum_type(*args)
        reversible do |dir|
          dir.up { _create_enum_type(*args) }
          dir.down { _drop_enum_type(*args) }
        end
      end

      def drop_enum_type(*args)
        reversible do |dir|
          dir.up { _drop_enum_type(*args) }
          dir.down { _create_enum_type(*args) }
        end
      end

      def add_enum_value(enum_name, new_value)
        execute("ALTER TYPE #{enum_name} ADD VALUE #{quote(new_value)}")
      end

      private

      def _create_enum_type(enum_name, enum_values = [])
        enum_values = enum_values.map { |value| quote(value) }.join(", ")
        execute("CREATE TYPE #{enum_name} AS ENUM (#{enum_values})")
      end

      def _drop_enum_type(enum_name)
        execute("DROP TYPE #{enum_name}")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rein-3.2.0 lib/rein/type/enum.rb
rein-3.1.0 lib/rein/type/enum.rb
rein-3.0.0 lib/rein/type/enum.rb