Class: AngryMob::Act::Scheduler
- Defined in:
- lib/angry_mob/act/scheduler.rb
Instance Attribute Summary collapse
-
#acted ⇒ Object
readonly
Returns the value of attribute acted.
-
#mob ⇒ Object
readonly
Returns the value of attribute mob.
-
#node ⇒ Object
writeonly
Sets the attribute node.
Instance Method Summary collapse
- #act_missing!(name) ⇒ Object
- #act_names ⇒ Object
- #act_now(act_name, *arguments) ⇒ Object
- #acts ⇒ Object
- #add_act(name, act) ⇒ Object
- #each_act ⇒ Object
- #finalise_acts! ⇒ Object
-
#initialize(mob) ⇒ Scheduler
constructor
A new instance of Scheduler.
- #next_act ⇒ Object
- #raise_on_missing_act? ⇒ Boolean
- #reset! ⇒ Object
- #run! ⇒ Object
- #schedule_act(*acts) ⇒ Object
- #schedule_acts_matching(regex = nil, &block) ⇒ Object
- #start_iterating!(with = act_names) ⇒ Object
- #ui ⇒ Object
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
#acted ⇒ Object (readonly)
Returns the value of attribute acted.
5 6 7 |
# File 'lib/angry_mob/act/scheduler.rb', line 5 def acted @acted end |
#mob ⇒ Object (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
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
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_names ⇒ Object
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 |
#acts ⇒ Object
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_act ⇒ Object
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 = 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}" (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}" (to_finalise) each_act {|act| act_now(act)} end end |
#next_act ⇒ Object
61 62 63 64 |
# File 'lib/angry_mob/act/scheduler.rb', line 61 def next_act .shift end |
#raise_on_missing_act? ⇒ 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
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 ui.info "scheduling #{acts.inspect}" += acts end |
#schedule_acts_matching(regex = nil, &block) ⇒ Object
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 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 (with=act_names) unless = with.dup = true end end |
#ui ⇒ Object
12 |
# File 'lib/angry_mob/act/scheduler.rb', line 12 def ui; @mob.ui end |