Class: Ick::Advisor
Class Method Summary collapse
-
.after(*args, &proc) ⇒ Object
{ |receiver, sym, *args| … }.
-
.around(*args, &proc) ⇒ Object
{ |callback, receiver, sym, *args| … }.
-
.before(*args, &proc) ⇒ Object
{ |receiver, sym, *args| … }.
Instance Method Summary collapse
Methods inherited from Wrapper
#__invocation__, #__respond_to?, #__rewrap__, #__value__, #initialize, is_contagious, is_not_contagious, #method_missing, #respond_to?
Constructor Details
This class inherits a constructor from Ick::Wrapper
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Ick::Wrapper
Class Method Details
.after(*args, &proc) ⇒ Object
{ |receiver, sym, *args| … }
42 43 44 45 46 47 |
# File 'lib/ick/advisor.rb', line 42 def self.after *args, &proc # { |receiver, sym, *args| ... } self.around(*args, &(lambda { |callback, receiver, sym, *args| callback.call() proc.call(receiver, sym, *args) })) end |
.around(*args, &proc) ⇒ Object
{ |callback, receiver, sym, *args| … }
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/ick/advisor.rb', line 4 def self.around *args, &proc # { |callback, receiver, sym, *args| ... } target, conditions = self.extract_parameters(args) old_arounds = self.around_chain if target.kind_of?(Symbol) proc = lambda { |callback, receiver, sym, *args| self.send(target, callback, receiver, sym, *args, &proc) } elsif target.kind_of?(Class) proc = lambda { |callback, receiver, sym, *args| target.filter(receiver, sym, *args, &proc) } end self.around_chain = if conditions[:only] lambda { |callback, receiver, sym, *args| new_callback = lambda { old_arounds.call(callback, receiver, sym, *args) } Array(conditions[:only]).include?(sym) ? proc.call(callback, receiver, sym, *args) : callback.call() } elsif conditions[:except] lambda { |callback, receiver, sym, *args| new_callback = lambda { old_arounds.call(callback, receiver, sym, *args) } Array(conditions[:except]).include?(sym) ? new_callback.call() : proc.call(new_callback, receiver, sym, *args) } else lambda { |callback, receiver, sym, *args| new_callback = lambda { old_arounds.call(callback, receiver, sym, *args) } proc.call(new_callback, receiver, sym, *args) } end end |
.before(*args, &proc) ⇒ Object
{ |receiver, sym, *args| … }
35 36 37 38 39 40 |
# File 'lib/ick/advisor.rb', line 35 def self.before *args, &proc # { |receiver, sym, *args| ... } self.around(*args, &(lambda { |callback, receiver, sym, *args| proc.call(receiver, sym, *args) callback.call() })) end |
Instance Method Details
#__invoke__(sym, *args, &block) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/ick/advisor.rb', line 49 def __invoke__(sym, *args, &block) self.__class.around_chain.call( lambda { @value.__send__(sym, *args, &block) }, @value, sym, *args ) end |