Module: EasilyTypable

Defined in:
lib/easily_typable.rb

Defined Under Namespace

Modules: SubClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/easily_typable.rb', line 22

def method_missing(name, *args, &block)
  if name.to_s.end_with?('?') && !@easily_typable_class_load_attempted
    # attempt to load Rails class and re-invoke once
    class_name = name.to_s.sub(/\?$/, '').split('_').map(&:capitalize).join
    Object.const_get(class_name) rescue nil
    @easily_typable_class_load_attempted = true
    send(name, *args, &block)
  else
    method_missing_without_easily_typable(name, *args, &block)
  end
end

Class Method Details

.included(class_constant) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/easily_typable.rb', line 3

def self.included(class_constant)
  super
  class_constant.class_eval <<-end_eval
    def #{__methodize__(class_constant.name)}?
      self.is_a?(#{class_constant.name})
    end
    def self.inherited(subclass_constant)
      super
      subclass_constant.send :include, SubClassMethods
    end
  end_eval
end

Instance Method Details

#method_missing_without_easily_typableObject

NOTE: Rails Specific Feature Retroactively load Rails model class matching missing method name to add to EasilyTypable hierarchy when it fails on first attempt. This shall result in it working on second attempt if model class exists. Example:



21
# File 'lib/easily_typable.rb', line 21

alias method_missing_without_easily_typable method_missing