module British::ClassMethods

Public: ClassMethods to extend in self.included/self.extended Defines an?, is_an?, method_missing

Public Instance Methods

british_method_missing(name, *args) click to toggle source
# File lib/british.rb, line 344
def british_method_missing(name, *args)
  # do British magic
  americanised_name = if name.to_s =~ /_/
                        name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
                      else
                        name.to_s.sub(BRITISH_ENDING_PATTERN, ENDINGS)
                      end

  return send(americanised_name, *args) if respond_to?(americanised_name)

  # call original method_missing (avoid double original method calls)
  return original_method_missing(name, *args) if caller[0] !~ /method_missing/ && defined?(:original_method_missing)
end
class_overwrite_method_missing() click to toggle source

Public: method to overwrite original method_missing with a magic one: this method_missing tries to translate British methods to American ones (or vice versa) before throwing NoMethodError if neither method was found. Works on a class level.

name - original method name
*args - original method args

Example:

# with any American object
british_object.colour    # will be translated into color
british_object.magnetise # will be translated into magnetize

# with any British object
british_object.color     # will be translated into colour
british_object.magnetize # will be translated into magnetise

# all method endings are allowed
british_object.surprize!
british_object.surprize?
british_object.surprize=

# complex names are supported
british_object.initialise_something # initialize_something will be called

Returns the original method's result Calls original method_missing (if British didn't hook anything) Raises NoMethodError if the method cannot be found

# File lib/british.rb, line 265
def class_overwrite_method_missing
  class_eval do
    unless method_defined?(:british_method_missing)
      define_method(:british_method_missing) do |name, *args|
        binded_class = self.class

        # When in our own British class
        if binded_class.include?(British::Initialisable)
          name_str = name.to_s

          # do American magic
          britanised_name = if name_str =~ /_/
                              name_str.gsub(AMERICAN_ENDING_PATTERN, INVERTED_ENDINGS)
                            else
                              name_str.sub(AMERICAN_ENDING_PATTERN, INVERTED_ENDINGS)
                            end

          return send(britanised_name, *args) if respond_to?(britanised_name)
        else
          name_str = name.to_s

          # do British magic
          americanised_name = if name_str =~ /_/
                                name_str.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
                              else
                                name_str.sub(BRITISH_ENDING_PATTERN, ENDINGS)
                              end

          return send(americanised_name, *args) if respond_to?(americanised_name)
        end

        # call original method_missing (avoid double original method calls)
        return original_method_missing(name, *args) if caller[0] !~ /method_missing/ && defined?(:original_method_missing)

        # call original parent's method_missing
        method = binded_class.superclass.instance_method(:original_method_missing)
        return method.bind(self).call(name, *args) if method
      end
    end

    if instance_method(:method_missing) != instance_method(:british_method_missing)
      alias_method :original_method_missing, :method_missing
      alias_method :method_missing, :british_method_missing
    end
  end
end
object_overwrite_method_missing() click to toggle source

Public: method to overwrite original method_missing with a magic one: this method_missing tries to translate British methods to American ones (or vice versa) before throwing NoMethodError if neither method was found. Works on an instance level.

name - original method name
*args - original method args

Example:

# with any American object
british_object.colour    # will be translated into color
british_object.magnetise # will be translated into magnetize

# with any British object
british_object.color     # will be translated into colour
british_object.magnetize # will be translated into magnetise

# all method endings are allowed
british_object.surprize!
british_object.surprize?
british_object.surprize=

# complex names are supported
british_object.initialise_something # initialize_something will be called

Returns the original method's result Calls original method_missing (if British didn't hook anything) Raises NoMethodError if the method cannot be found

# File lib/british.rb, line 341
def object_overwrite_method_missing
  instance_eval do
    unless respond_to?(:british_method_missing)
      def british_method_missing(name, *args)
        # do British magic
        americanised_name = if name.to_s =~ /_/
                              name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS)
                            else
                              name.to_s.sub(BRITISH_ENDING_PATTERN, ENDINGS)
                            end

        return send(americanised_name, *args) if respond_to?(americanised_name)

        # call original method_missing (avoid double original method calls)
        return original_method_missing(name, *args) if caller[0] !~ /method_missing/ && defined?(:original_method_missing)
      end
    end

    if method(:method_missing) != method(:british_method_missing)
      alias :original_method_missing :method_missing
      alias :method_missing :british_method_missing
    end
  end
end