Class: EnumMachine::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/enum_machine/machine.rb

Defined Under Namespace

Classes: AnyEnumValues

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum_values, base_klass = nil, enum_const_name = nil) ⇒ Machine

rubocop:disable Gp/OptArgParameters



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

def initialize(enum_values, base_klass = nil, enum_const_name = nil) # rubocop:disable Gp/OptArgParameters
  @enum_values = enum_values
  @base_klass = base_klass
  @enum_const_name = enum_const_name
  @transitions = {}
  @before_transition = {}
  @after_transition = {}
  @aliases = {}
end

Instance Attribute Details

#base_klassObject (readonly)

Returns the value of attribute base_klass.



6
7
8
# File 'lib/enum_machine/machine.rb', line 6

def base_klass
  @base_klass
end

#enum_const_nameObject (readonly)

Returns the value of attribute enum_const_name.



6
7
8
# File 'lib/enum_machine/machine.rb', line 6

def enum_const_name
  @enum_const_name
end

#enum_valuesObject (readonly)

Returns the value of attribute enum_values.



6
7
8
# File 'lib/enum_machine/machine.rb', line 6

def enum_values
  @enum_values
end

Instance Method Details

#after_transition(from__to_hash, &block) ⇒ Object

public api after_transition(‘s1’ => ‘s4’) after_transition(%w[s1 s2] => %w[s3 s4])



46
47
48
49
50
51
52
53
# File 'lib/enum_machine/machine.rb', line 46

def after_transition(from__to_hash, &block)
  validate_state!(from__to_hash)

  filter_transitions(from__to_hash).each do |from_pair_to|
    @after_transition[from_pair_to] ||= []
    @after_transition[from_pair_to] << block
  end
end

#aliases(hash) ⇒ Object



60
61
62
# File 'lib/enum_machine/machine.rb', line 60

def aliases(hash)
  @aliases = hash
end

#anyObject

public api



56
57
58
# File 'lib/enum_machine/machine.rb', line 56

def any
  @any ||= AnyEnumValues.new(enum_values + [nil])
end

#before_transition(from__to_hash, &block) ⇒ Object

public api before_transition(‘s1’ => ‘s4’) before_transition(%w[s1 s2] => %w[s3 s4])



34
35
36
37
38
39
40
41
# File 'lib/enum_machine/machine.rb', line 34

def before_transition(from__to_hash, &block)
  validate_state!(from__to_hash)

  filter_transitions(from__to_hash).each do |from_pair_to|
    @before_transition[from_pair_to] ||= []
    @before_transition[from_pair_to] << block
  end
end

#fetch_after_transitions(from__to) ⇒ Object

internal api



83
84
85
# File 'lib/enum_machine/machine.rb', line 83

def fetch_after_transitions(from__to)
  @after_transition.fetch(from__to, [])
end

#fetch_alias(alias_key) ⇒ Object

internal api



88
89
90
# File 'lib/enum_machine/machine.rb', line 88

def fetch_alias(alias_key)
  array_wrap(@aliases.fetch(alias_key))
end

#fetch_aliasesObject



64
65
66
# File 'lib/enum_machine/machine.rb', line 64

def fetch_aliases
  @aliases
end

#fetch_before_transitions(from__to) ⇒ Object

internal api



77
78
79
80
# File 'lib/enum_machine/machine.rb', line 77

def fetch_before_transitions(from__to)
  validate_transition!(from__to)
  @before_transition.fetch(from__to, [])
end

#fetch_transitionsObject



72
73
74
# File 'lib/enum_machine/machine.rb', line 72

def fetch_transitions
  @transitions
end

#possible_transitions(from) ⇒ Object

internal api



93
94
95
# File 'lib/enum_machine/machine.rb', line 93

def possible_transitions(from)
  @transitions.fetch(from, [])
end

#transitions(from__to_hash) ⇒ Object

public api transitions(‘s1’ => ‘s2’, %w[s3 s3] => ‘s4’)



20
21
22
23
24
25
26
27
28
29
# File 'lib/enum_machine/machine.rb', line 20

def transitions(from__to_hash)
  validate_state!(from__to_hash)

  from__to_hash.each do |from_arr, to_arr|
    array_wrap(from_arr).product(array_wrap(to_arr)).each do |from, to|
      @transitions[from] ||= []
      @transitions[from] << to
    end
  end
end

#transitions?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/enum_machine/machine.rb', line 68

def transitions?
  @transitions.present?
end