Module: Actor::Mixins

Defined in:
lib/Olib/actor/actor.rb

Instance Method Summary collapse

Instance Method Details

#add(state, &callback) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/Olib/actor/actor.rb', line 89

def add(state, &callback)
  ref = self

  states << state.to_sym
  supervisor.add(state) do
    Fiber.yield until ref.is?(state)
    ref.history << state
    garbage_collect!
    pre_hooks.each do |hook| hook.call(ref) end
    # did a pre_hook prempt this op?
    if ref.is?(state)
      callback.call(ref)
      post_hooks.each do |hook| hook.call(ref) end
    end
  end
  self
end

#after_every(&hook) ⇒ Object



84
85
86
87
# File 'lib/Olib/actor/actor.rb', line 84

def after_every(&hook)
  post_hooks << hook
  self
end

#before_every(&hook) ⇒ Object



79
80
81
82
# File 'lib/Olib/actor/actor.rb', line 79

def before_every(&hook)
  pre_hooks << hook
  self
end

#emit(state) ⇒ Object



46
47
48
49
50
# File 'lib/Olib/actor/actor.rb', line 46

def emit(state)
  state = state.to_sym
  validate! state
  self
end

#garbage_collect!Object



75
76
77
# File 'lib/Olib/actor/actor.rb', line 75

def garbage_collect!
  history.shift while history.size > states.size
end

#historyObject



30
31
32
# File 'lib/Olib/actor/actor.rb', line 30

def history
  self.class_variable_get(:@@history)
end

#is?(state) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/Olib/actor/actor.rb', line 61

def is?(state)
  validate!(state)
  state == state
end

#lastObject



66
67
68
# File 'lib/Olib/actor/actor.rb', line 66

def last
  history[-2]
end

#link!Object



107
108
109
# File 'lib/Olib/actor/actor.rb', line 107

def link!
  supervisor.link!
end

#post_hooksObject



22
23
24
# File 'lib/Olib/actor/actor.rb', line 22

def post_hooks
  self.class_variable_get(:@@post_hooks)
end

#pre_hooksObject



18
19
20
# File 'lib/Olib/actor/actor.rb', line 18

def pre_hooks
  self.class_variable_get(:@@pre_hooks)
end

#start(state) ⇒ Object



42
43
44
# File 'lib/Olib/actor/actor.rb', line 42

def start(state)
  class_variable_set(:@@state, state.to_sym)
end

#stateObject



26
27
28
# File 'lib/Olib/actor/actor.rb', line 26

def state
  self.class_variable_get(:@@state)
end

#state=(state) ⇒ Object



38
39
40
# File 'lib/Olib/actor/actor.rb', line 38

def state=(state)
  class_variable_set(:@@state, state.to_sym)
end

#statesObject



14
15
16
# File 'lib/Olib/actor/actor.rb', line 14

def states
  self.class_variable_get(:@@states)
end

#supervisorObject



34
35
36
# File 'lib/Olib/actor/actor.rb', line 34

def supervisor
  self.class_variable_get(:@@supervisor)
end

#to_sObject



52
53
54
# File 'lib/Olib/actor/actor.rb', line 52

def to_s
  "#{self.name}<state=#{state} last=#{last} history=#{history} states=#{states}>"
end

#validate!(state) ⇒ Object



70
71
72
73
# File 'lib/Olib/actor/actor.rb', line 70

def validate!(state)
  state = state.to_sym
  raise Actor::InvalidState.new(state, self) unless states.include?(state)
end

#yield(state) ⇒ Object



56
57
58
59
# File 'lib/Olib/actor/actor.rb', line 56

def yield(state)
  emit(state)
  Fiber.yield
end