module British

Public: method_missing which tries to call “British”/“American” version before failing Could be included/extended to/by the particular class or globally (monkey-patching Object)

Examples

Create classes with `initialise` constructor
And your own British methods and attributes
require 'british'

class BritishObject < BlackBoxObject
  # use British::Initialisable within your classes only *1 …
  include British::Initialisable

  attr_reader :colour

  # works exactly like an initialize (including `super` usage)
  def initialise(test)
    @test = test
    @colour = 'red'

    super('black', 'box', 'arguments')
  end

  def magnetise(test)
    @test
  end
end

british_object = BritishObject.new('Hello UK!')
british_object.test # => 'Hello UK!'

# Use American or British endings with any method or attribute
british_object.color     # => "red"
british_object.colour    # => "red"

british_object.magnetize # => "Hello UK!"
british_object.magnetise # => "Hello UK!"

# *1 … patch third party or all the system Objects
# (wouldn't really recommend to do the last one)
String.include(British)
'cheers!'.capitalise # => "Cheers!"

require 'active_support/inflector'
include British

'cheers!'.capitalise # => "Cheers!"
'oi_ya_bloody_wanker'.camelise # => "OiYaBloodyWanker"

# Extend an object instance to make it British
not_british = SomeClass.new # with #color method
refugee = SomeClass.new # with #color method

# Make it British
british = refugee.extend(British)

not_british.colour # undefined method `colour'
british.colour # works well

# Use is_an?/an? with classes like an Array!
[].is_an? Array # => true
[].an? Array    # => true

Constants

AMERICAN_ENDING_PATTERN

Public: Regexp pattern to search/replace American words endings

BRITISH_ENDING_PATTERN

Public: Regexp pattern to search/replace British words endings

ENDINGS

Public: Hash of British ↔ American words endings

INVERTED_ENDINGS

Public: Hash of American ↔ British words endings

Public Class Methods

extended(host_object) click to toggle source

Hook to be called when British module being used to extend an object Add method_missing method with all the British 'magic' behaviour Extends an object instance to make it British

Example:

not_british = SomeClass.new # with color method
refugee = SomeClass.new # with color method

# Make it British
british = refugee.extend(British)

not_british.colour # undefined method `colour'
british.colour # works well

Returns nothing

# File lib/british.rb, line 213
def self.extended(host_object)
  host_object.extend ClassMethods
  host_object.object_overwrite_method_missing

  return unless host_object.private_methods(true).include?(:singleton_method_added)

  # Inject our own singleton_method_added hook to catch it when
  # `method_missing` is added
  host_object.instance_eval do
    def british_singleton_method_added(name)
      original_singleton_method_added(name)
      object_overwrite_method_missing if name == :method_missing
    end

    # do not mess with others :singleton_method_added
    alias original_singleton_method_added singleton_method_added
    alias singleton_method_added british_singleton_method_added
  end
end
included(host_class) click to toggle source

Hook to be called when British module being included itself Add method_missing method with all the British 'magic' behaviour Extends a class to make it British

Example:

class Example
   include British

   def color
      'red'
   end
end

example = Example.new
example.colour # => "red"

Returns nothing

# File lib/british.rb, line 177
def self.included(host_class)
  host_class.extend ClassMethods
  host_class.class_overwrite_method_missing

  return unless host_class.private_methods(true).include?(:method_added)

  # Inject our own method_added hook to catch it when
  # `method_missing` is added
  host_class.instance_eval do
    def british_method_added(name)
      original_method_added(name)
      class_overwrite_method_missing if name == :method_missing
    end

    # do not mess with others :method_added
    alias original_method_added method_added
    alias method_added british_method_added
  end
end

Public Instance Methods

british_method_added(name) click to toggle source
# File lib/british.rb, line 186
def british_method_added(name)
  original_method_added(name)
  class_overwrite_method_missing if name == :method_missing
end
british_singleton_method_added(name) click to toggle source
# File lib/british.rb, line 222
def british_singleton_method_added(name)
  original_singleton_method_added(name)
  object_overwrite_method_missing if name == :method_missing
end