Module: Istox::AutoEnum

Defined in:
lib/istox/models/auto_enum.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/istox/models/auto_enum.rb', line 3

def method_missing(name, *args, &block)
  if name[-1] == '?' || name[-1] == '!'
    method_name = name[0...-1]
    if method_name.parameterize.underscore != method_name
      send(:"#{method_name}#{name[-1]}")
    else
      index = _all_enums_in_underscore.find_index { |e| e == method_name }
      if index >= 0
        send(:"#{_all_enums[index]}#{name[-1]}")
      else
        super
      end
    end
  else
    super
  end
end

Instance Method Details

#_all_enumsObject



34
35
36
# File 'lib/istox/models/auto_enum.rb', line 34

def _all_enums
  defined_enums.values.map(&:keys).flatten
end

#_all_enums_in_underscoreObject



30
31
32
# File 'lib/istox/models/auto_enum.rb', line 30

def _all_enums_in_underscore
  defined_enums.values.map(&:keys).flatten.map { |i| i.parameterize.underscore }
end

#respond_to_missing?(name, include_private) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
# File 'lib/istox/models/auto_enum.rb', line 21

def respond_to_missing?(name, include_private)
  if name[-1] == '?' || name[-1] == '!'
    method_name = name[0...-1]
    _all_enums_in_underscore.include?(method_name)
  else
    super
  end
end