Module: RichEngine::Enum::Mixin::ClassMethods

Defined in:
lib/rich_engine/enum/mixin.rb

Overview

Class-level methods made available by including RichEngine::Enum::Mixin.

Instance Method Summary collapse

Instance Method Details

#enum(name, enum_options, enum_name: "#{name}s") ⇒ void

This method returns an undefined value.

Declares an enum on the class. Defines a class method returning the RichEngine::Enum (named after +enum_name+) and an instance method (named +name+) that reads the +@name+ instance variable and returns the matching Value.

Parameters:

  • name (Symbol)

    the name of the enum and instance reader; the value is read from the +@name+ instance variable

  • enum_options (Array, Hash)

    the enum options, as accepted by RichEngine::Enum#initialize

  • enum_name (String) (defaults to: "#{name}s")

    the name of the class-level enum accessor; defaults to the pluralized +name+



43
44
45
46
47
48
49
50
51
# File 'lib/rich_engine/enum/mixin.rb', line 43

def enum(name, enum_options, enum_name: "#{name}s")
  define_singleton_method(enum_name) do
    Enum.new(name, enum_options)
  end

  define_method(name) do
    self.class.public_send(enum_name).public_send(instance_variable_get("@#{name}"))
  end
end