Module: Estate::ClassMethods

Defined in:
lib/estate/estate.rb

Instance Method Summary collapse

Instance Method Details

#estate(column: Estate::Configuration::Defaults::COLUMN_NAME, empty_initial_state: Estate::Configuration::Defaults::ALLOW_EMPTY_INITIAL_STATE, raise_on_error: Estate::Configuration::Defaults::RAISE_ON_ERROR) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/estate/estate.rb', line 9

def estate(column: Estate::Configuration::Defaults::COLUMN_NAME,
           empty_initial_state: Estate::Configuration::Defaults::ALLOW_EMPTY_INITIAL_STATE,
           raise_on_error: Estate::Configuration::Defaults::RAISE_ON_ERROR)
  Estate::StateMachine.init(name, column, empty_initial_state, raise_on_error)
  Estate::Setup.call(self)

  yield if block_given?
end

#state(state_name = nil) ⇒ Object

Raises:

  • (StandardError)


18
19
20
21
22
23
24
25
26
# File 'lib/estate/estate.rb', line 18

def state(state_name = nil)
  raise(StandardError, 'state must be a Symbol or a String') unless Estate::StateMachine.argument_valid?(state_name)

  if Estate::StateMachine.state_exists?(name, state_name)
    raise(StandardError, "state `:#{state_name}` is already defined")
  else
    Estate::StateMachine.register_state(name, state_name)
  end
end

#transition(from: nil, to: nil) ⇒ Object

Raises:

  • (StandardError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/estate/estate.rb', line 28

def transition(from: nil, to: nil)
  unless Estate::StateMachine.argument_valid?(from)
    raise(StandardError, 'argument `from` must be a Symbol or a String')
  end

  raise(StandardError, 'argument `to` must be a Symbol or a String') unless Estate::StateMachine.argument_valid?(to)

  raise(StandardError, "state `#{from}` is not defined") unless Estate::StateMachine.state_exists?(name, from)

  raise(StandardError, "state `#{to}` is not defined") unless Estate::StateMachine.state_exists?(name, to)

  if Estate::StateMachine.transition_exists?(name, from, to)
    raise(StandardError, "`transition from: :#{from}, to: :#{to}` already defined")
  end

  Estate::StateMachine.register_transition(name, from, to)
end