Module: SimpleEnumeration::DefineSimpleEnumeration

Defined in:
lib/simple_enumeration/define_simple_enumeration.rb

Instance Method Summary collapse

Instance Method Details

#define_simple_enumeration(attribute, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/simple_enumeration/define_simple_enumeration.rb', line 5

def define_simple_enumeration(attribute, options = {})
  enum_class_name = options[:with] || enum_class_name_for(attribute)
  enum_class = enum_class_name.is_a?(String) ? const_get(enum_class_name) : enum_class_name

  define_method "#{attribute}_enumeration" do
    enum_class.new(converted_value: send(attribute))
  end
end

#enum_class_name_for(attribute) ⇒ Object



14
15
16
17
18
19
# File 'lib/simple_enumeration/define_simple_enumeration.rb', line 14

def enum_class_name_for(attribute)
  [
    SimpleEnumeration.camelcase(attribute.to_s),
    'Enumeration'
  ].join
end