Module: British

Defined in:
lib/british.rb

Overview

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

Examples

# Create classes with `initialise` constructor (British::Initialisable)
# And magic British methods and attributes (British)
class BritishObject
  require 'british'

  # use within your objects only *1 …
  include British
  include British::Initialisable

  attr_reader :color

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

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

  def magnetize(test)
    @test
  end
end

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

# Use British endings with any method or attribute
british_object.colour    # => "red"
british_object.magnetise # => "Hello UK!"

# *1 … patch third party or all the system Objects
String.include British
'cheers!'.capitalise # => "Cheers!"

require 'active_support/inflector'
include British

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

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

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
BRITISH_ENDING_PATTERN =

Public: Regexp pattern to search/replace British words endings

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(host_class) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/british.rb', line 106

def self.included(host_class)
  host_class.extend ClassMethods
  host_class.overwrite_method_missing

  host_class.instance_eval do
    def method_added(name)
      return if name != :method_missing
      overwrite_method_missing
    end
  end
end

Instance Method Details

#is_an?(*args) ⇒ Boolean Also known as: an?

Public: British alias of native is_a? method Returns the original method’s result

Returns:

  • (Boolean)


99
100
101
# File 'lib/british.rb', line 99

def is_an?(*args)
  is_a?(*args) if respond_to?(:is_a?)
end