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
-
.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.
-
.included(host_class) ⇒ Object
Hook to be called when British module being included Add method_missing method with all the British ‘magic’ behaviour Extends a class to make it British.
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
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/british.rb', line 162 def self.extended(host_object) host_object.extend ClassMethods host_object.object_overwrite_method_missing host_object.instance_eval do def singleton_method_added(name) object_overwrite_method_missing if name == :method_missing end end end |
.included(host_class) ⇒ Object
Hook to be called when British module being included 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
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/british.rb', line 135 def self.included(host_class) host_class.extend ClassMethods host_class.class_overwrite_method_missing host_class.instance_eval do def method_added(name) class_overwrite_method_missing if name == :method_missing end end end |