Class: RailsStateMachine::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_state_machine/state_machine.rb

Constant Summary collapse

StateAlreadyDefinedError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, state_attribute, prefix: '') ⇒ StateMachine

Returns a new instance of StateMachine.



6
7
8
9
10
11
12
13
14
15
# File 'lib/rails_state_machine/state_machine.rb', line 6

def initialize(model, state_attribute, prefix: '')
  @model = model
  @state_attribute = state_attribute
  @prefix = prefix
  @prefix_for_constant_definition = "#{@prefix.upcase}_" if @prefix.present?
  @prefix_for_method_definition = "#{@prefix.downcase}_" if @prefix.present?
  @states_by_name = {}
  @events_by_name = {}
  build_model_module
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



17
18
19
# File 'lib/rails_state_machine/state_machine.rb', line 17

def model
  @model
end

#prefixObject (readonly)

Returns the value of attribute prefix.



17
18
19
# File 'lib/rails_state_machine/state_machine.rb', line 17

def prefix
  @prefix
end

#prefix_for_constant_definitionObject (readonly)

Returns the value of attribute prefix_for_constant_definition.



17
18
19
# File 'lib/rails_state_machine/state_machine.rb', line 17

def prefix_for_constant_definition
  @prefix_for_constant_definition
end

#prefix_for_method_definitionObject (readonly)

Returns the value of attribute prefix_for_method_definition.



17
18
19
# File 'lib/rails_state_machine/state_machine.rb', line 17

def prefix_for_method_definition
  @prefix_for_method_definition
end

#state_attributeObject (readonly)

Returns the value of attribute state_attribute.



17
18
19
# File 'lib/rails_state_machine/state_machine.rb', line 17

def state_attribute
  @state_attribute
end

Instance Method Details

#configure(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails_state_machine/state_machine.rb', line 19

def configure(&block)
  instance_eval(&block)

  check_if_states_already_defined

  define_state_methods
  define_state_constants
  register_initial_state

  define_event_methods
  define_model_methods
end

#event_namesObject



44
45
46
# File 'lib/rails_state_machine/state_machine.rb', line 44

def event_names
  @events_by_name.keys
end

#eventsObject



40
41
42
# File 'lib/rails_state_machine/state_machine.rb', line 40

def events
  @events_by_name.values
end

#find_event(name) ⇒ Object



48
49
50
# File 'lib/rails_state_machine/state_machine.rb', line 48

def find_event(name)
  @events_by_name.fetch(name.to_sym)
end

#has_state?(name) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/rails_state_machine/state_machine.rb', line 52

def has_state?(name)
  @states_by_name.key?(name.to_sym)
end

#state_namesObject



36
37
38
# File 'lib/rails_state_machine/state_machine.rb', line 36

def state_names
  @states_by_name.keys
end

#statesObject



32
33
34
# File 'lib/rails_state_machine/state_machine.rb', line 32

def states
  @states_by_name.values
end