method

enum

Importance_0
v6.1.3.1 - Show latest stable - 0 notes - Class: Enum
enum(definitions) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/enum.rb, line 160
    def enum(definitions)
      enum_prefix = definitions.delete(:_prefix)
      enum_suffix = definitions.delete(:_suffix)
      enum_scopes = definitions.delete(:_scopes)

      default = {}
      default[:default] = definitions.delete(:_default) if definitions.key?(:_default)

      definitions.each do |name, values|
        assert_valid_enum_definition_values(values)
        # statuses = { }
        enum_values = ActiveSupport::HashWithIndifferentAccess.new
        name = name.to_s

        # def self.statuses() statuses end
        detect_enum_conflict!(name, name.pluralize, true)
        singleton_class.define_method(name.pluralize) { enum_values }
        defined_enums[name] = enum_values

        detect_enum_conflict!(name, name)
        detect_enum_conflict!(name, "#{name}=")

        attr = attribute_alias?(name) ? attribute_alias(name) : name

        decorate_attribute_type(attr, **default) do |subtype|
          EnumType.new(attr, enum_values, subtype)
        end

        value_method_names = []
        _enum_methods_module.module_eval do
          prefix = if enum_prefix == true
            "#{name}_"
          elsif enum_prefix
            "#{enum_prefix}_"
          end

          suffix = if enum_suffix == true
            "_#{name}"
          elsif enum_suffix
            "_#{enum_suffix}"
          end

          pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
          pairs.each do |label, value|
            enum_values[label] = value
            label = label.to_s

            value_method_name = "#{prefix}#{label}#{suffix}"
            value_method_names << value_method_name
            define_enum_methods(name, value_method_name, value, enum_scopes)

            method_friendly_label = label.gsub(/[\W&&[:ascii:]]+/, "_")
            value_method_alias = "#{prefix}#{method_friendly_label}#{suffix}"

            if value_method_alias != value_method_name && !value_method_names.include?(value_method_alias)
              value_method_names << value_method_alias
              define_enum_methods(name, value_method_alias, value, enum_scopes)
            end
          end
        end
        detect_negative_enum_conditions!(value_method_names) if enum_scopes != false
        enum_values.freeze
      end
    end
Register or log in to add new notes.