Class: RubyJard::ReplProxy::ReplState

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_jard/repl_proxy.rb

Overview

A class to store the state with multi-thread guarding Ready => Processing/Exiting Processing => Ready again Exiting => Exited Exited => Ready

Constant Summary collapse

STATES =
[
  STATE_READY      = 0,
  STATE_EXITING    = 1,
  STATE_PROCESSING = 2,
  STATE_EXITED     = 3
].freeze

Instance Method Summary collapse

Constructor Details

#initializeReplState

Returns a new instance of ReplState.



80
81
82
83
# File 'lib/ruby_jard/repl_proxy.rb', line 80

def initialize
  @state = STATE_EXITED
  @mutex = Mutex.new
end

Instance Method Details

#check(method_name) ⇒ Object



85
86
87
# File 'lib/ruby_jard/repl_proxy.rb', line 85

def check(method_name)
  @mutex.synchronize { yield if send(method_name) }
end

#exited!Object



121
122
123
# File 'lib/ruby_jard/repl_proxy.rb', line 121

def exited!
  @mutex.synchronize { @state = STATE_EXITED }
end

#exited?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/ruby_jard/repl_proxy.rb', line 117

def exited?
  @state == STATE_EXITED
end

#exiting!Object



113
114
115
# File 'lib/ruby_jard/repl_proxy.rb', line 113

def exiting!
  @mutex.synchronize { @state = STATE_EXITING }
end

#exiting?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/ruby_jard/repl_proxy.rb', line 109

def exiting?
  @state == STATE_EXITING
end

#processing!Object



103
104
105
106
107
# File 'lib/ruby_jard/repl_proxy.rb', line 103

def processing!
  return unless ready?

  @mutex.synchronize { @state = STATE_PROCESSING }
end

#processing?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/ruby_jard/repl_proxy.rb', line 99

def processing?
  @state == STATE_PROCESSING
end

#ready!Object



93
94
95
96
97
# File 'lib/ruby_jard/repl_proxy.rb', line 93

def ready!
  if ready? || processing? || exited?
    @mutex.synchronize { @state = STATE_READY }
  end
end

#ready?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/ruby_jard/repl_proxy.rb', line 89

def ready?
  @state == STATE_READY
end