Module: Stateful::Poro::ClassMethods

Defined in:
lib/Stateful/Poro/ClassMethods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/Stateful/Poro/ClassMethods.rb', line 10

def extended(klass)
  klass.define_stateful_variable_name_setter_method
  klass.define_stateful_variable_name_getter_method
  klass.define_next_state_method
  klass.define_transitions_method
  klass.define_initial_stateQ_method
  klass.define_final_stateQ_method
end

Instance Method Details

#define_event_method(transition) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/Stateful/Poro/ClassMethods.rb', line 29

def define_event_method(transition)
  stateful_variable_name = self.instance_variable_get(:@stateful_variable_name)
  define_method(transition.event_name) do
    next_state_name = self.send(stateful_variable_name).next_state_name(transition.event_name)
    next_state = self.class.stateful_states.find(next_state_name)
    self.send("#{stateful_variable_name}=", next_state)
  end
end

#define_final_stateQ_methodObject



87
88
89
90
91
92
# File 'lib/Stateful/Poro/ClassMethods.rb', line 87

def define_final_stateQ_method
  stateful_variable_name = self.instance_variable_get(:@stateful_variable_name)
  define_method(:final_state?) do
    final_states.include?(self.send(stateful_variable_name))
  end
end

#define_initial_stateQ_methodObject



80
81
82
83
84
85
# File 'lib/Stateful/Poro/ClassMethods.rb', line 80

def define_initial_stateQ_method
  stateful_variable_name = self.instance_variable_get(:@stateful_variable_name)
  define_method(:initial_state?) do
    self.send(stateful_variable_name) == initial_state
  end
end

#define_next_state_methodObject



65
66
67
68
69
70
71
# File 'lib/Stateful/Poro/ClassMethods.rb', line 65

def define_next_state_method
  stateful_variable_name = self.instance_variable_get(:@stateful_variable_name)
  define_method(:next_state) do |event_name|
    next_state_name = self.send(stateful_variable_name).next_state_name(event_name)
    all_states.find(next_state_name)
  end
end

#define_stateful_variable_name_getter_methodObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/Stateful/Poro/ClassMethods.rb', line 52

def define_stateful_variable_name_getter_method
  stateful_variable_name = self.instance_variable_get(:@stateful_variable_name)
  define_method(stateful_variable_name) do
    if state = instance_variable_get("@#{stateful_variable_name}")
      state
    else
      initial_state = self.class.stateful_states.initial_state
      self.send("#{stateful_variable_name}=", initial_state.name)
      initial_state
    end
  end
end

#define_stateful_variable_name_setter_methodObject



45
46
47
48
49
50
# File 'lib/Stateful/Poro/ClassMethods.rb', line 45

def define_stateful_variable_name_setter_method
  stateful_variable_name = self.instance_variable_get(:@stateful_variable_name)
  define_method("#{stateful_variable_name}=") do |state|
    instance_variable_set("@#{stateful_variable_name}", self.class.stateful_states.find(state))
  end
end

#define_status_predicate_method(state_name) ⇒ Object



38
39
40
41
42
43
# File 'lib/Stateful/Poro/ClassMethods.rb', line 38

def define_status_predicate_method(state_name)
  stateful_variable_name = self.instance_variable_get(:@stateful_variable_name)
  define_method("#{state_name}?") do
    self.send(stateful_variable_name).name == state_name
  end
end

#define_transitions_methodObject



73
74
75
76
77
78
# File 'lib/Stateful/Poro/ClassMethods.rb', line 73

def define_transitions_method
  stateful_variable_name = self.instance_variable_get(:@stateful_variable_name)
  define_method(:transitions) do
    self.send(stateful_variable_name).transitions
  end
end

#stateful_variable_nameObject



25
26
27
# File 'lib/Stateful/Poro/ClassMethods.rb', line 25

def stateful_variable_name
  @stateful_variable_name
end

#stateful_variable_name=(stateful_variable_name) ⇒ Object

class << self



21
22
23
# File 'lib/Stateful/Poro/ClassMethods.rb', line 21

def stateful_variable_name=(stateful_variable_name)
  @stateful_variable_name = stateful_variable_name
end