Module: British::ClassMethods

Defined in:
lib/british.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#class_overwrite_method_missingObject

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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# 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_missingObject

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



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# 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