Class: Loverload::Method
- Inherits:
-
Object
- Object
- Loverload::Method
- Defined in:
- lib/loverload/method.rb
Instance Method Summary collapse
-
#initialize(klass, method_name, &with_params_block) ⇒ Method
constructor
A new instance of Method.
- #overload(instance, *args) ⇒ Object
- #with_params(*pars, &block) ⇒ Object
Constructor Details
#initialize(klass, method_name, &with_params_block) ⇒ Method
Returns a new instance of Method.
3 4 5 6 7 |
# File 'lib/loverload/method.rb', line 3 def initialize klass, method_name, &with_params_block @klass = klass @method_name = method_name instance_eval(&with_params_block) end |
Instance Method Details
#overload(instance, *args) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/loverload/method.rb', line 24 def overload instance, *args default = "__#{ @method_name }_#{ args.size }" method_name = "__name_#{ default }_#{ type_signature(args.map(&:class)) }" method_alias = "__alias_#{ default }_#{ alias_signature(args.map(&:class)) }" if instance.respond_to? method_name, true instance.send method_name, *args elsif instance.respond_to? method_alias, true instance.send method_alias, *args elsif instance.respond_to? default, true instance.send default, *args else raise NoMethodError, "Undefined method '#{ @method_name }' for #{ type_signature(args.map(&:class)) }" end end |
#with_params(*pars, &block) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/loverload/method.rb', line 9 def with_params *pars, &block default = "__#{ @method_name }_#{ block.arity }" method_name = "__name_#{ default }_#{ type_signature(pars) }" method_alias = "__alias_#{ default }_#{ alias_signature(pars) }" @klass.define_method method_name do |*args| instance_exec(*args, &block) end @klass.send :alias_method, method_alias, method_name @klass.send :alias_method, default, method_name [default, method_name, method_alias].each{ |m| @klass.send :private, m } end |