Module: FFWD::Lifecycle

Instance Method Summary collapse

Instance Method Details

#depend_on(other_lifecycle) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ffwd/lifecycle.rb', line 71

def depend_on other_lifecycle
  if other_lifecycle.nil?
    raise "Other lifecycle must not be nil"
  end

  if (@depends ||= nil)
    raise "This component already depends on #{@depends}"
  end

  @depends = other_lifecycle

  other_lifecycle.starting do
    start
  end

  other_lifecycle.stopping do
    stop
    @depends = nil
  end
end

#startObject



49
50
51
52
53
54
# File 'lib/ffwd/lifecycle.rb', line 49

def start
  return if started?
  starting_hooks.each(&:call)
  starting_hooks.clear
  @state = :started
end

#started?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ffwd/lifecycle.rb', line 63

def started?
  (@state ||= :none) == :started
end

#starting(&block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/ffwd/lifecycle.rb', line 41

def starting &block
  if started?
    block.call
  else
    starting_hooks << block
  end
end

#starting_hooksObject



26
27
28
# File 'lib/ffwd/lifecycle.rb', line 26

def starting_hooks
  @starting_hooks ||= []
end

#stopObject



56
57
58
59
60
61
# File 'lib/ffwd/lifecycle.rb', line 56

def stop
  return if stopped?
  stopping_hooks.each(&:call)
  stopping_hooks.clear
  @state = :stopped
end

#stopped?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/ffwd/lifecycle.rb', line 67

def stopped?
  (@state ||= :none) == :stopped
end

#stopping(&block) ⇒ Object

Register a callback to be executed when the Stoppable is to be stopped.

This will only be called once.



33
34
35
36
37
38
39
# File 'lib/ffwd/lifecycle.rb', line 33

def stopping &block
  if stopped?
    block.call
  else
    stopping_hooks << block
  end
end

#stopping_hooksObject



22
23
24
# File 'lib/ffwd/lifecycle.rb', line 22

def stopping_hooks
  @stopping_hooks ||= []
end