Class: AngryMob::Act::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/angry_mob/act/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mob) ⇒ Scheduler

Returns a new instance of Scheduler.



7
8
9
10
# File 'lib/angry_mob/act/scheduler.rb', line 7

def initialize(mob)
  @mob = mob
  reset!
end

Instance Attribute Details

#actedObject (readonly)

Returns the value of attribute acted.



5
6
7
# File 'lib/angry_mob/act/scheduler.rb', line 5

def acted
  @acted
end

#mobObject (readonly)

Returns the value of attribute mob.



5
6
7
# File 'lib/angry_mob/act/scheduler.rb', line 5

def mob
  @mob
end

#node=(value) ⇒ Object (writeonly)

Sets the attribute node

Parameters:

  • value

    the value to set the attribute node to.



4
5
6
# File 'lib/angry_mob/act/scheduler.rb', line 4

def node=(value)
  @node = value
end

Instance Method Details

#act_missing!(name) ⇒ Object

Raises:



115
116
117
# File 'lib/angry_mob/act/scheduler.rb', line 115

def act_missing!(name)
  raise(AngryMob::MobError, "no act named '#{name}'") if raise_on_missing_act?
end

#act_namesObject



19
20
21
# File 'lib/angry_mob/act/scheduler.rb', line 19

def act_names
  @act_names ||= @node.acts || []
end

#act_now(act_name, *arguments) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/angry_mob/act/scheduler.rb', line 119

def act_now(act_name,*arguments)
  if AngryMob::Act === act_name
    act = act_name
    act_name = act.name
  else
    act = acts[act_name]
  end

  unless act
    act_missing!(act_name) 
    return
  end

  if !act.multi? && acted.include?(act_name)
    ui.skipped! "(not re-running act #{act_name} - already run)"
    return
  end

  acted << act_name

  act.run!(*arguments)
end

#actsObject



23
24
25
# File 'lib/angry_mob/act/scheduler.rb', line 23

def acts
  @acts ||= Dictionary.new
end

#add_act(name, act) ⇒ Object



27
28
29
# File 'lib/angry_mob/act/scheduler.rb', line 27

def add_act(name,act)
  acts[name.to_s] = act
end

#each_actObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/angry_mob/act/scheduler.rb', line 38

def each_act
  while act_name = next_act
    ui.debug "acting out #{act_name}"

    act = acts[act_name]

    unless act
      act_missing!(act_name)
      next
    end
    
    yield act
  end
  @iterating = false
end

#finalise_acts!Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/angry_mob/act/scheduler.rb', line 86

def finalise_acts!
  # notifications only act as necessary, so its safe to run them all.
  ui.task "running notifications"
  to_notify = acts.keys.select {|k| k[%r{^notifications_for/}]}

  unless to_notify.empty?
    ui.info "running notifiers #{to_notify.inspect}"

    start_iterating!(to_notify)
    each_act {|act| act_now(act)}
  end


  # finalisers should only run if the act of the same name has run first
  ui.task "running finalisations"

  to_finalise = acted.map {|name| "finalise/#{name}"} & acts.keys
  unless to_finalise.empty?
    ui.info "running acts finalisers #{to_finalise.inspect}"

    start_iterating!(to_finalise)
    each_act {|act| act_now(act)}
  end
end

#next_actObject



61
62
63
64
# File 'lib/angry_mob/act/scheduler.rb', line 61

def next_act
  start_iterating!
  @iterating_act_names.shift
end

#raise_on_missing_act?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/angry_mob/act/scheduler.rb', line 111

def raise_on_missing_act?
  !( FalseClass === mob.node.raise_on_missing_act )
end

#reset!Object



14
15
16
17
# File 'lib/angry_mob/act/scheduler.rb', line 14

def reset!
  @act_names = nil
  @acted = []
end

#run!Object



31
32
33
34
35
36
# File 'lib/angry_mob/act/scheduler.rb', line 31

def run!
  ui.task "running acts #{act_names.inspect}"
  each_act {|act| act_now(act)}
  ui.good "finished running acts"
  finalise_acts!
end

#schedule_act(*acts) ⇒ Object

Raises:

  • (CompilationError)


66
67
68
69
70
# File 'lib/angry_mob/act/scheduler.rb', line 66

def schedule_act(*acts)
  raise(CompilationError, "schedule_act called when not compiling") unless @iterating
  ui.info "scheduling #{acts.inspect}"
  @iterating_act_names += acts
end

#schedule_acts_matching(regex = nil, &block) ⇒ Object

Raises:

  • (CompilationError)


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/angry_mob/act/scheduler.rb', line 72

def schedule_acts_matching(regex=nil,&block)
  raise(CompilationError, "schedule_act called when not compiling") unless @iterating

  act_keys = acts.keys

  acts_to_schedule = if regex
                        act_keys.grep(regex)
                      else
                        act_keys.select(&block)
                      end

  schedule_act(*acts_to_schedule)
end

#start_iterating!(with = act_names) ⇒ Object



54
55
56
57
58
59
# File 'lib/angry_mob/act/scheduler.rb', line 54

def start_iterating!(with=act_names)
  unless @iterating
    @iterating_act_names = with.dup
    @iterating = true
  end
end

#uiObject



12
# File 'lib/angry_mob/act/scheduler.rb', line 12

def ui; @mob.ui end