Module: Sapphire::Pluggable
- Included in:
- WebAbstractions::RubySeleniumWebDriver
- Defined in:
- lib/sapphire/Pluggable.rb
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sapphire/Pluggable.rb', line 54 def self.included(base) before = proc { |name, inst, args| observers = Plugins::PluginRepository.instance.Find(name.to_sym, inst.class) observers.each do |x| x.Before(inst, name, args) if x.respond_to? :Before end } after = proc { |name, inst, args| observers = Plugins::PluginRepository.instance.Find(name.to_sym, inst.class) observers.each do |x| x.After(inst, name, args) if x.respond_to? :After end } success = proc { |name, inst, args| observers = Plugins::PluginRepository.instance.Find(name.to_sym, inst.class) observers.each do |x| x.OnSuccess(inst, name, args) if x.respond_to? :OnSuccess end } failure = proc { |name, inst, exception, args| observers = Plugins::PluginRepository.instance.Find(name.to_sym, inst.class) observers.each do |x| x.OnFailure(inst, name, exception, args) if x.respond_to? :OnFailure end } hash = {} hash.merge! :before => before, :after => after, :failure => failure, :success => success intercept(base, hash, *base.instance_methods(false)) end |
.intercept(base, hash, *method_names) ⇒ Object
3 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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sapphire/Pluggable.rb', line 3 def self.intercept(base, hash, *method_names) do_before = proc { |name, inst, args| begin hash[:before].call(name, inst, args) rescue => observer_exception end } do_on_success = proc { |name, inst, args| begin hash[:success].call(name, inst, args) rescue => raised_exception raise raised_exception end } do_on_failure = proc { |name, inst, raised_exception, args| begin hash[:failure].call(name, inst, raised_exception, args) rescue => observer_exception end } do_after = proc { |name, inst, args| begin hash[:after].call(name, inst, args) rescue => observer_exception end } method_names.each do |method_name| method = base.instance_method(method_name) base.send :define_method, method_name.to_sym do |*args, &method_block| do_before.call(method_name, self, args) begin result = method.bind(self).(*args, &method_block) do_on_success.call(method_name, self, args) return result rescue => raised_exception do_on_failure.call(method_name, self, raised_exception, args) raise raised_exception ensure do_after.call(method_name, self, args) end end end end |