Module: SimpleEnum::Attribute

Defined in:
lib/simple_enum/attribute.rb

Overview

SimpleEnum::Attribute is the base class to be included in objects to get the #as_enum functionality. All the including class needs to provide is a setter and getter for `source`, by default the `source` is `<enum>_cd`. This is similar to how relations work in Rails, the idea is not taint the original method.

Constant Summary collapse

EXTENSIONS =

Registered registrator methods from extensions

[]

Instance Method Summary collapse

Instance Method Details

#as_enum(name, values, options = {}) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/simple_enum/attribute.rb', line 19

def as_enum(name, values, options = {})
  options.assert_valid_keys(:source, :prefix, :with, :accessor, :map, :pluralize_scopes)

  hash     = SimpleEnum::Hasher.map(values, options)
  enum     = SimpleEnum::Enum.new(name, hash)
  accessor = SimpleEnum::Accessors.accessor(name, enum, options)

  generate_enum_class_accessors_for(enum, accessor)
  generate_enum_instance_accessors_for(enum, accessor)
  generate_additional_enum_methods_for(enum, accessor, options)

  EXTENSIONS.uniq.each do |extension|
    send "generate_enum_#{extension}_extension_for", enum, accessor
  end

  enum
end