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
-
#class_overwrite_method_missing ⇒ Object
Public: method to overwrite original method_missing with a magic one: this method_missing tries to translate British methods to American ones before throwing NoMethodError if neither method was found.
-
#object_overwrite_method_missing ⇒ Object
Public: method to overwrite original method_missing with a magic one: this method_missing tries to translate British methods to American ones before throwing NoMethodError if neither method was found.
Instance Method Details
#class_overwrite_method_missing ⇒ Object
Public: method to overwrite original method_missing with a magic one: this method_missing tries to translate British methods to American ones before throwing NoMethodError if neither method was found. Works on a class level.
name - original method name
*args - original method args
Example:
# with any British object
british_object.colour # will be translated into color
british_object.magnetise # will be translated into magnetize
# 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
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/british.rb', line 201 def class_overwrite_method_missing class_eval do unless method_defined?(:british_method_missing) define_method(:british_method_missing) do |name, *args| # do British magic americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS) 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) # call original parent's method_missing method = self.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_missing ⇒ Object
Public: method to overwrite original method_missing with a magic one: this method_missing tries to translate British methods to American ones before throwing NoMethodError if neither method was found. Works on an instance level.
name - original method name
*args - original method args
Example:
# with any British object
british_object.colour # will be translated into color
british_object.magnetise # will be translated into magnetize
# 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
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/british.rb', line 251 def object_overwrite_method_missing instance_eval do unless respond_to?(:british_method_missing) def british_method_missing(name, *args) $stdout.flush # do British magic americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS) 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 |