Class: Tivoli
- Inherits:
-
Object
- Object
- Tivoli
- Defined in:
- lib/tivoli.rb
Instance Attribute Summary collapse
-
#aspects ⇒ Object
Returns the value of attribute aspects.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#method ⇒ Object
Returns the value of attribute method.
Class Method Summary collapse
Instance Method Summary collapse
- #aspect(state, &block) ⇒ Object
- #filter(state, &block) ⇒ Object
-
#initialize(method) ⇒ Tivoli
constructor
A new instance of Tivoli.
- #run(fallback, &block) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(method) ⇒ Tivoli
Returns a new instance of Tivoli.
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 53 54 55 56 57 58 59 |
# File 'lib/tivoli.rb', line 22 def initialize(method) @method = method default_to_array = proc { |h,k| h[k] = [] } self.aspects = Hash.new(&default_to_array) self.filters = Hash.new(&default_to_array) tivoli = self method.owner.send(:define_method, method.name) do |*args, &blk| result = nil call_original_method = proc { method.bind(self)[*args, &blk] } tivoli.run(call_original_method) do time = Tivoli.bench do |time_passed| execute = proc { |state, &block| tivoli.aspects[state].each do |a| a.call(time_passed.call, args, &blk) end if tivoli.filters[state].empty? block.call else tivoli.filters[state].reduce(result) do |prev, f| f.call(prev, time_passed.call, args, &blk) end end } result = execute.call :before, &call_original_method result = execute.call :after do result end end end result end end |
Instance Attribute Details
#aspects ⇒ Object
Returns the value of attribute aspects.
2 3 4 |
# File 'lib/tivoli.rb', line 2 def aspects @aspects end |
#filters ⇒ Object
Returns the value of attribute filters.
2 3 4 |
# File 'lib/tivoli.rb', line 2 def filters @filters end |
#method ⇒ Object
Returns the value of attribute method.
2 3 4 |
# File 'lib/tivoli.rb', line 2 def method @method end |
Class Method Details
.bench {|time_passed| ... } ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/tivoli.rb', line 4 def self.bench start = Time.now.to_f time_passed = proc { time_taken = ((Time.now.to_f - start)*1000).to_i } yield time_passed time_passed.call end |
Instance Method Details
#aspect(state, &block) ⇒ Object
61 62 63 |
# File 'lib/tivoli.rb', line 61 def aspect(state, &block) aspects[state].push(block) end |
#filter(state, &block) ⇒ Object
65 66 67 |
# File 'lib/tivoli.rb', line 65 def filter(state, &block) filters[state].push(block) end |
#run(fallback, &block) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/tivoli.rb', line 13 def run(fallback, &block) key = :"tivoli_#{object_id}" return fallback.call if Thread.current[key] Thread.current[key] = true result = block.call Thread.current[key] = false result end |
#stop ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/tivoli.rb', line 69 def stop aspects.clear filters.clear method.owner.send(:define_method, method.name) do |*args, &blk| method.bind(self)[*args, &blk] end end |