Module: Constantizable

Extended by:
ActiveSupport::Concern
Defined in:
lib/constantizable.rb,
lib/constantizable/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/constantizable.rb', line 56

def method_missing(method, *args, &block)
  # Refer https://github.com/rails/rails/blob/master/activesupport/lib/active_support/string_inquirer.rb
  # Inquiry happens only if method is an inquiry method (i.e) method name ends with a '?'

  if method[-1] == '?'
    # If Column name isn't present it should fallback to the default `method_missing` 
    # implementation.

    column_name = self.class.instance_variable_get(:@constantized_column)
    super if column_name.nil?

    # The value of the constantized column needs to be titleized or underscored.

    data = self.send(column_name)
    method[0..-2] == data.titleize.tr(' ','').underscore
  else
    super
  end
end