Class: SpeedGun::Hook
- Inherits:
-
Object
- Object
- SpeedGun::Hook
- Defined in:
- lib/speed_gun/hook.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #available? ⇒ Boolean
- #depends(&block) ⇒ Object
- #execute(&block) ⇒ Object
-
#initialize(name, &block) ⇒ Hook
constructor
A new instance of Hook.
- #install! ⇒ Object
Constructor Details
#initialize(name, &block) ⇒ Hook
Returns a new instance of Hook.
16 17 18 19 20 21 22 23 |
# File 'lib/speed_gun/hook.rb', line 16 def initialize(name, &block) @name = name @installed = false @available_checker = -> { true } @execute = -> { true } self.class.hooks.push(self) instance_eval(&block) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/speed_gun/hook.rb', line 14 def name @name end |
Class Method Details
.hooks ⇒ Object
4 5 6 |
# File 'lib/speed_gun/hook.rb', line 4 def self.hooks @hooks ||= [] end |
.install! ⇒ Object
8 9 10 11 12 |
# File 'lib/speed_gun/hook.rb', line 8 def self.install! hooks.each do |hook| hook.available? && hook.install! end end |
Instance Method Details
#available? ⇒ Boolean
33 34 35 |
# File 'lib/speed_gun/hook.rb', line 33 def available? @available_checker.is_a?(Proc) && @available_checker.call end |
#depends(&block) ⇒ Object
25 26 27 |
# File 'lib/speed_gun/hook.rb', line 25 def depends(&block) @available_checker = block end |
#execute(&block) ⇒ Object
29 30 31 |
# File 'lib/speed_gun/hook.rb', line 29 def execute(&block) @execute = block end |
#install! ⇒ Object
37 38 39 40 41 |
# File 'lib/speed_gun/hook.rb', line 37 def install! return if @installed @execute.is_a?(Proc) && @execute.call @installed = true end |