Class: Frequent::Probe
- Inherits:
-
Object
- Object
- Frequent::Probe
- Defined in:
- lib/frequent.rb
Instance Attribute Summary collapse
-
#calls ⇒ Object
readonly
Returns the value of attribute calls.
-
#class_name ⇒ Object
readonly
Returns the value of attribute class_name.
-
#method_name ⇒ Object
readonly
Returns the value of attribute method_name.
-
#name ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute name.
-
#original_implementation ⇒ Object
readonly
Returns the value of attribute original_implementation.
Instance Method Summary collapse
- #aliased_name ⇒ Object
- #disable! ⇒ Object
- #enable! ⇒ Object
- #enabled? ⇒ Boolean
- #increment ⇒ Object
-
#initialize(name) ⇒ Probe
constructor
A new instance of Probe.
- #method_owner ⇒ Object
- #parse_name(name) ⇒ Object
- #ready? ⇒ Boolean
- #target_defined? ⇒ Boolean
Constructor Details
#initialize(name) ⇒ Probe
Returns a new instance of Probe.
40 41 42 43 44 45 46 |
# File 'lib/frequent.rb', line 40 def initialize(name) @calls = 0 @name = name @enabled = false parse_name(name) enable! if ready? end |
Instance Attribute Details
#calls ⇒ Object (readonly)
Returns the value of attribute calls.
37 38 39 |
# File 'lib/frequent.rb', line 37 def calls @calls end |
#class_name ⇒ Object (readonly)
Returns the value of attribute class_name.
37 38 39 |
# File 'lib/frequent.rb', line 37 def class_name @class_name end |
#method_name ⇒ Object (readonly)
Returns the value of attribute method_name.
37 38 39 |
# File 'lib/frequent.rb', line 37 def method_name @method_name end |
#name ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute name.
37 38 39 |
# File 'lib/frequent.rb', line 37 def name @name end |
#original_implementation ⇒ Object (readonly)
Returns the value of attribute original_implementation.
37 38 39 |
# File 'lib/frequent.rb', line 37 def original_implementation @original_implementation end |
Instance Method Details
#aliased_name ⇒ Object
102 103 104 |
# File 'lib/frequent.rb', line 102 def aliased_name "__frequent_original_#{method_name}".to_sym end |
#disable! ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/frequent.rb', line 92 def disable! if enabled? probe = self method_owner.class_eval do define_method(probe.method_name, probe.original_implementation) remove_method(probe.aliased_name) end end end |
#enable! ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/frequent.rb', line 74 def enable! unless @enabling @enabling = true @original_implementation = method_owner.instance_method(method_name) probe = self aliased_name = self.aliased_name method_owner.class_eval do alias_method aliased_name, probe.method_name define_method(probe.method_name) do |*args, &blk| probe.increment send(aliased_name, *args, &blk) end end @enabled = true @enabling = false end end |
#enabled? ⇒ Boolean
52 53 54 |
# File 'lib/frequent.rb', line 52 def enabled? @enabled end |
#increment ⇒ Object
48 49 50 |
# File 'lib/frequent.rb', line 48 def increment @calls += 1 end |
#method_owner ⇒ Object
60 61 62 63 64 |
# File 'lib/frequent.rb', line 60 def method_owner owner = Frequent.constantize(class_name) return unless owner @type == :instance ? owner : owner.singleton_class end |
#parse_name(name) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/frequent.rb', line 106 def parse_name(name) md = name.match(/(.*)(\.|\#)(.*)/) raise ProbeNameError.new("Failed to parse probe name '#{name}'") unless md class_name, sep, method_name = md[1..3] @class_name = class_name @type = (sep == '#') ? :instance : :class @method_name = method_name.to_sym end |
#ready? ⇒ Boolean
56 57 58 |
# File 'lib/frequent.rb', line 56 def ready? !enabled? && target_defined? end |
#target_defined? ⇒ Boolean
66 67 68 69 70 71 72 |
# File 'lib/frequent.rb', line 66 def target_defined? owner = method_owner owner && ( owner.method_defined?(method_name) || owner.private_instance_methods.include?(method_name) ) end |