Module: Remnant::Discover::ClassMethods
- Included in:
- Remnant::Discover
- Defined in:
- lib/remnant/discover.rb
Instance Method Summary collapse
- #_inject(key, klass, method, instance) ⇒ Object
- #find(key, klass, method, instance = true) ⇒ Object
- #find_with(klass, &block) ⇒ Object
- #measure(key, &block) ⇒ Object
- #rediscover(*args) ⇒ Object
- #rediscover! ⇒ Object
- #remnants_to_rediscover ⇒ Object
- #results ⇒ Object
- #running ⇒ Object
Instance Method Details
#_inject(key, klass, method, instance) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/remnant/discover.rb', line 9 def _inject(key, klass, method, instance) klass.class_eval <<-EOL, __FILE__, __LINE__ #{"class << self" unless instance} alias_method :#{method}_without_remnant, :#{method} def #{method}(*args, &block) ::Remnant::Discover.measure(#{key.inspect}) do #{method}_without_remnant(*args, &block) end end #{"end" unless instance} EOL end |
#find(key, klass, method, instance = true) ⇒ Object
4 5 6 7 |
# File 'lib/remnant/discover.rb', line 4 def find(key, klass, method, instance = true) rediscover(key, klass, method, instance) if ActiveSupport::Dependencies.will_unload?(klass) _inject(key, klass, method, instance) end |
#find_with(klass, &block) ⇒ Object
24 25 26 27 |
# File 'lib/remnant/discover.rb', line 24 def find_with(klass, &block) rediscover(klass, block) if ActiveSupport::Dependencies.will_unload?(klass) block.call end |
#measure(key, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/remnant/discover.rb', line 29 def measure(key, &block) if Remnant::Discover.running.include?(key) yield else result = nil Remnant::Discover.running << key begin Remnant::Discover.results[key] += Benchmark.ms { result = yield }.to_i rescue raise ensure Remnant::Discover.running.delete(key) end result end end |
#rediscover(*args) ⇒ Object
58 59 60 |
# File 'lib/remnant/discover.rb', line 58 def rediscover(*args) remnants_to_rediscover << args unless remnants_to_rediscover.include?(args) end |
#rediscover! ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/remnant/discover.rb', line 62 def rediscover! remnants_to_rediscover.map do |remnant| if remnant.size == 4 key, klass_name, method, instance = remnant _inject(key, klass_name.constantize, method, instance) else remant.first.call end end end |
#remnants_to_rediscover ⇒ Object
54 55 56 |
# File 'lib/remnant/discover.rb', line 54 def remnants_to_rediscover @remnants_to_rediscover ||= [] end |
#results ⇒ Object
46 47 48 |
# File 'lib/remnant/discover.rb', line 46 def results Thread.current[:result] ||= Hash.new(0) end |
#running ⇒ Object
50 51 52 |
# File 'lib/remnant/discover.rb', line 50 def running Thread.current[:running] ||= [] end |