module EasilyTypable

Public Class Methods

included(class_constant) click to toggle source
Calls superclass method
# 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

Protected Class Methods

__methodize__(text) click to toggle source

added this method to break reliance on ActiveSupport and Facets

# File lib/easily_typable.rb, line 57
def self.__methodize__(text)
  text.split(/::/).last.gsub(/([A-Z]+)([A-Z])/,'\1_\2').
  gsub(/([a-z])([A-Z])/,'\1_\2').
  downcase
end

Public Instance Methods

method_missing(name, *args, &block) click to toggle source
# 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
method_missing_without_easily_typable(name, *args, &block)

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:

Alias for: method_missing