Class: SpeedGun::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/speed_gun/hook.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject (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

.hooksObject



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

Returns:

  • (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