Module: Habits::Whip
Constant Summary collapse
- @@transitions =
{}
- @@ticks =
[]
Instance Method Summary collapse
-
#on(status, &blk) ⇒ Object
Add block to be executed when status changes to given status.
-
#on_tick(&blk) ⇒ Object
Add a block to be called each time before whip is used.
-
#use ⇒ Object
Use whip, i.e.
Instance Method Details
#on(status, &blk) ⇒ Object
Add block to be executed when status changes to given status. A habit which changes to this status is passed to the block.
12 13 14 15 |
# File 'lib/habits/whip.rb', line 12 def on(status, &blk) raise "No block given" if blk.nil? @@transitions[status] = blk end |
#on_tick(&blk) ⇒ Object
Add a block to be called each time before whip is used.
18 19 20 21 |
# File 'lib/habits/whip.rb', line 18 def on_tick(&blk) raise "No block given" if blk.nil? @@ticks << blk end |
#use ⇒ Object
Use whip, i.e. update status of each habit and call blocks associated with state changes.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/habits/whip.rb', line 25 def use @@ticks.each {|tick| tick.call} Habit.all.each do |habit| habit.update_status do st = habit.status.value @@transitions[st].call(habit) if @@transitions[st] end end end |