Sha256: 1cf1cac649ade182dff4af925c74f5b846bcd2a2daea3529c58c34e0d3197da7

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

require 'enum_type/extensions'

# Adds the @enum_type@ method to a model.
#
# @example Basic usage
#   class MyModel < ActiveRecord::Base
#     extend EnumType
#     enum_type :status, values: %w( open closed flagged deleted )
#   end

module EnumType

  # Defines a field whose type is an enumeration. Values are stored as strings
  # in the backend (or your database's enumerated type), however in the Rails
  # level they are represented as symbols.
  #
  # @overload enum_type(field, ..., options={})
  #   @param [Symbol] field An enumerated field.
  #   @param [Hash] options A hash of options.
  #   @option options [true, false] :allow_nil (false) If @true@, a nil value
  #   is allowed.
  #   @option options [Array<String>] :values If given, restricts valid values
  #   to those in the given array.
  #
  # @example Enumerated field with restricted types
  #   enum_type :color, values: %w( red orange yellow green blue purple )

  def enum_type(*fields)
    options = fields.extract_options!
    fields.each do |field|
      define_method field do
        value = read_attribute(field)
        value ? value.to_sym : value
      end

      define_method :"#{field}=" do |value|
        write_attribute field, value.try(:to_s)
      end

      validates_presence_of(field) unless options[:allow_nil]
      validates_inclusion_of(field, in: options[:values].map(&:to_sym), allow_nil: options[:allow_nil]) if options[:values]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
enum_type-2.1.2 lib/enum_type.rb
enum_type-2.1.1 lib/enum_type.rb
enum_type-2.1.0 lib/enum_type.rb
enum_type-2.0.0 lib/enum_type.rb