Module: British

Defined in:
lib/british.rb

Overview

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

Defined Under Namespace

Modules: ClassMethods, Initialisable

Constant Summary collapse

ENDINGS =

Public: Hash of British ↔ American words endings

{
  # Latin-derived spellings
  'our'  => 'or',
  're'   => 'er',
  'ce'   => 'se',
  'xion' => 'ction',

  # Greek-derived spellings
  'ise'     => 'ize',
  'isation' => 'ization',
  'yse'     => 'yze',
  'ogue'    => 'og'
}.freeze
INVERTED_ENDINGS =

Public: Hash of American ↔ British words endings

{
  # Latin-derived spellings
  'or'    => 'our',
  'er'    => 're',
  'se'    => 'ce',
  'ction' => 'xion',

  # Greek-derived spellings
  'ize'     => 'ise',
  'ization' => 'isation',
  'yze'     => 'yse',
  'og'      => 'ogue'
}.freeze
BRITISH_ENDING_PATTERN =

Public: Regexp pattern to search/replace British words endings

/#{Regexp.union(ENDINGS.keys)}(?=_|\?|\!|=|$)/
AMERICAN_ENDING_PATTERN =

Public: Regexp pattern to search/replace American words endings

/#{Regexp.union(ENDINGS.values)}(?=_|\?|\!|=|$)/

Class Method Summary collapse

Class Method Details

.extended(host_object) ⇒ Object

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# 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) ⇒ Object

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# 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