Module: Enumattr::Ext::Name::ClassMethods

Defined in:
lib/enumattr-ext/name.rb

Instance Method Summary collapse

Instance Method Details

#define_enumattr_class_methods_with_name(enumattr_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/enumattr-ext/name.rb', line 16

def define_enumattr_class_methods_with_name(enumattr_name)
  define_enumattr_class_methods_without_name(enumattr_name)

  method_prefix = "#{enumattr_name}_"
  class_eval(<<-METHOD, __FILE__, __LINE__)
    def self.#{method_prefix}names
      enumattrs[:#{enumattr_name}].enums.map(&:name)
    end

    def self.#{method_prefix}name(key)
      enum = enumattrs[:#{enumattr_name}].enum_by_key(key)
      enum && enum.name
    end

    def self.#{method_prefix}options
      enumattrs[:#{enumattr_name}].enums.map do |enum|
        [ enum.name, enum.value ]
      end
    end
  METHOD
end

#define_enumattr_instance_methods_with_name(enumattr_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/enumattr-ext/name.rb', line 38

def define_enumattr_instance_methods_with_name(enumattr_name)
  define_enumattr_instance_methods_without_name(enumattr_name)

  enums = enumattrs[enumattr_name]
  method_prefix = "#{enumattr_name}_"

  class_eval(<<-METHOD, __FILE__, __LINE__)
    def #{method_prefix}name
      enum = #{method_prefix}enum
      enum && enum.name
    end
  METHOD
end