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 (or vice versa) 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 (or vice versa) 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 (or vice versa) before throwing NoMethodError if neither method was found. Works on a class level.
name - original method name
*args - original method args
Example:
# with any American object
british_object.colour # will be translated into color
british_object.magnetise # will be translated into magnetize
# with any British object
british_object.color # will be translated into colour
british_object.magnetize # will be translated into magnetise
# 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
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/british.rb', line 250 def class_overwrite_method_missing class_eval do unless method_defined?(:british_method_missing) define_method(:british_method_missing) do |name, *args| # When in our own British class if self.class.include?(British::Initialisable) # do American magic britanised_name = name.to_s.gsub(AMERICAN_ENDING_PATTERN, ENDINGS.invert) return send(britanised_name, *args) if respond_to?(britanised_name) else # do British magic americanised_name = name.to_s.gsub(BRITISH_ENDING_PATTERN, ENDINGS) return send(americanised_name, *args) if respond_to?(americanised_name) end # 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 (or vice versa) before throwing NoMethodError if neither method was found. Works on an instance level.
name - original method name
*args - original method args
Example:
# with any American object
british_object.colour # will be translated into color
british_object.magnetise # will be translated into magnetize
# with any British object
british_object.color # will be translated into colour
british_object.magnetize # will be translated into magnetise
# 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
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/british.rb', line 310 def object_overwrite_method_missing instance_eval do unless respond_to?(:british_method_missing) def british_method_missing(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) 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 |