tick_tacker 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/examples/cowntdown.rb +26 -0
- data/examples/cowntdown_respawn.rb +42 -0
- data/lib/tick_tacker/ticker.rb +16 -1
- data/lib/tick_tacker/timer.rb +20 -12
- data/lib/tick_tacker/version.rb +1 -1
- data/spec/tick_tacker/ticker_spec.rb +54 -2
- data/spec/tick_tacker/timer_spec.rb +28 -9
- metadata +5 -11
| @@ -0,0 +1,26 @@ | |
| 1 | 
            +
            $: << "../lib"
         | 
| 2 | 
            +
            require 'tick_tacker'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            class Cowntdown
         | 
| 5 | 
            +
              def initialize
         | 
| 6 | 
            +
                @timer = TickTacker::Timer.new.with :total_time => 5.seconds
         | 
| 7 | 
            +
                @timer.add_observer self
         | 
| 8 | 
            +
                Thread.new {
         | 
| 9 | 
            +
                  @timer.start
         | 
| 10 | 
            +
                }
         | 
| 11 | 
            +
                6.times do
         | 
| 12 | 
            +
                  do_nothing
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              def do_nothing
         | 
| 17 | 
            +
                puts "this is the cowntdown doing another things, nothing to be more precisely."
         | 
| 18 | 
            +
                sleep(1)
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              def update(options)
         | 
| 22 | 
            +
                puts "time remaining: #{options[:time_remaining]}"
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
            end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            Cowntdown.new
         | 
| @@ -0,0 +1,42 @@ | |
| 1 | 
            +
            $: << "../lib"
         | 
| 2 | 
            +
            require 'tick_tacker'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            class Cowntdown
         | 
| 5 | 
            +
              def timer
         | 
| 6 | 
            +
                @timer = TickTacker::Timer.new.with :total_time => 5.seconds
         | 
| 7 | 
            +
                @timer.add_observer self
         | 
| 8 | 
            +
              end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
              def update(options)
         | 
| 11 | 
            +
                @time_remaining = options[:time_remaining]
         | 
| 12 | 
            +
                @respawn = true if options[:time_remaining] == 0
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
              def forever
         | 
| 16 | 
            +
                timer
         | 
| 17 | 
            +
                @timer.add_observer self
         | 
| 18 | 
            +
                Thread.new {
         | 
| 19 | 
            +
                  @timer.start
         | 
| 20 | 
            +
                }
         | 
| 21 | 
            +
                while true
         | 
| 22 | 
            +
                  puts "."
         | 
| 23 | 
            +
                  puts "remaining: #{@time_remaining}"
         | 
| 24 | 
            +
                  sleep(0.3)
         | 
| 25 | 
            +
                  if @respawn
         | 
| 26 | 
            +
                    @respawn = false
         | 
| 27 | 
            +
                    respawn
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
                end
         | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
              def respawn
         | 
| 33 | 
            +
                @timer.stop
         | 
| 34 | 
            +
                timer
         | 
| 35 | 
            +
                puts "respawning..."
         | 
| 36 | 
            +
                Thread.new {
         | 
| 37 | 
            +
                  @timer.start
         | 
| 38 | 
            +
                }
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
            end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            Cowntdown.new.forever
         | 
    
        data/lib/tick_tacker/ticker.rb
    CHANGED
    
    | @@ -1,7 +1,18 @@ | |
| 1 1 | 
             
            module TickTacker
         | 
| 2 2 | 
             
              class Ticker
         | 
| 3 | 
            +
                attr_accessor :interval
         | 
| 4 | 
            +
             | 
| 3 5 | 
             
                def initialize
         | 
| 4 6 | 
             
                  @can_run = true
         | 
| 7 | 
            +
                  with :interval => 1.second
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def with(options={})
         | 
| 11 | 
            +
                  default = {:interval => 1.second}
         | 
| 12 | 
            +
                  config = default.merge(options)
         | 
| 13 | 
            +
                  @interval = config[:interval]
         | 
| 14 | 
            +
                  repeat_every(@interval) { yield } if block_given?
         | 
| 15 | 
            +
                  self
         | 
| 5 16 | 
             
                end
         | 
| 6 17 |  | 
| 7 18 | 
             
                def time_block 
         | 
| @@ -10,7 +21,7 @@ module TickTacker | |
| 10 21 | 
             
                  Time.now - start_time 
         | 
| 11 22 | 
             
                end 
         | 
| 12 23 |  | 
| 13 | 
            -
                def repeat_every(seconds) | 
| 24 | 
            +
                def repeat_every(seconds)
         | 
| 14 25 | 
             
                  while @can_run do 
         | 
| 15 26 | 
             
                    time_spent = time_block { yield } # To handle -ve sleep interaval 
         | 
| 16 27 | 
             
                    sleep(seconds - time_spent) if time_spent < seconds 
         | 
| @@ -20,5 +31,9 @@ module TickTacker | |
| 20 31 | 
             
                def stop
         | 
| 21 32 | 
             
                  @can_run = false
         | 
| 22 33 | 
             
                end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                def repeat
         | 
| 36 | 
            +
                  repeat_every(@interval) { yield }
         | 
| 37 | 
            +
                end
         | 
| 23 38 | 
             
              end
         | 
| 24 39 | 
             
            end
         | 
    
        data/lib/tick_tacker/timer.rb
    CHANGED
    
    | @@ -4,39 +4,47 @@ module TickTacker | |
| 4 4 | 
             
              class Timer
         | 
| 5 5 | 
             
                include Observable
         | 
| 6 6 |  | 
| 7 | 
            -
                attr_reader : | 
| 7 | 
            +
                attr_reader :total_time
         | 
| 8 8 | 
             
                attr_accessor :elapsed_time
         | 
| 9 9 |  | 
| 10 | 
            -
                def initialize | 
| 11 | 
            -
                   | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 10 | 
            +
                def initialize
         | 
| 11 | 
            +
                  with(:total_time => 25.minutes)
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def with(options)
         | 
| 15 | 
            +
                  default = {:ticker => Ticker.new}
         | 
| 16 | 
            +
                  config = default.merge(options)
         | 
| 17 | 
            +
                  @ticker = config[:ticker]
         | 
| 18 | 
            +
                  @ticker_time = @ticker.interval
         | 
| 19 | 
            +
                  @total_time = config[:total_time]
         | 
| 20 | 
            +
                  self
         | 
| 14 21 | 
             
                end
         | 
| 15 22 |  | 
| 16 23 | 
             
                def notify
         | 
| 17 | 
            -
                  time_remaining =  | 
| 24 | 
            +
                  time_remaining = total_time - elapsed_time
         | 
| 18 25 | 
             
                  notification_options = {:time_remaining => time_remaining}
         | 
| 19 26 | 
             
                  notify_observers(notification_options)
         | 
| 20 27 | 
             
                end
         | 
| 21 28 |  | 
| 22 29 | 
             
                def update_time_elapsed(time)
         | 
| 23 30 | 
             
                  @elapsed_time += time
         | 
| 24 | 
            -
                  if @elapsed_time >= @ | 
| 31 | 
            +
                  if @elapsed_time >= @total_time
         | 
| 25 32 | 
             
                    @ticker.stop
         | 
| 26 33 | 
             
                  end
         | 
| 27 34 | 
             
                end
         | 
| 28 35 |  | 
| 29 | 
            -
                def update(options)
         | 
| 36 | 
            +
                def update(options = {})
         | 
| 30 37 | 
             
                  @elapsed_time ||= 0
         | 
| 31 | 
            -
                   | 
| 32 | 
            -
                   | 
| 38 | 
            +
                  default = {:time_elapsed => @ticker.interval}
         | 
| 39 | 
            +
                  config = default.merge(options)
         | 
| 40 | 
            +
                  update_time_elapsed(config[:time_elapsed])
         | 
| 33 41 | 
             
                  changed
         | 
| 34 42 | 
             
                  notify
         | 
| 35 43 | 
             
                end
         | 
| 36 44 |  | 
| 37 45 | 
             
                def start
         | 
| 38 | 
            -
                  @ticker. | 
| 39 | 
            -
                    self.update | 
| 46 | 
            +
                  @ticker.repeat do
         | 
| 47 | 
            +
                    self.update
         | 
| 40 48 | 
             
                  end
         | 
| 41 49 | 
             
                end
         | 
| 42 50 |  | 
    
        data/lib/tick_tacker/version.rb
    CHANGED
    
    
| @@ -1,9 +1,61 @@ | |
| 1 1 | 
             
            require 'spec_helper'
         | 
| 2 2 |  | 
| 3 | 
            +
            def loop_number
         | 
| 4 | 
            +
              2
         | 
| 5 | 
            +
            end
         | 
| 6 | 
            +
             | 
| 3 7 | 
             
            module TickTacker
         | 
| 4 8 | 
             
              describe Ticker do
         | 
| 5 | 
            -
                 | 
| 6 | 
            -
             | 
| 9 | 
            +
                let(:ticker) { Ticker.new }
         | 
| 10 | 
            +
                let(:ticker_execute) do
         | 
| 11 | 
            +
                  start = Time.now
         | 
| 12 | 
            +
                  x = 0;
         | 
| 13 | 
            +
                  ticker.with :interval => 1.second do 
         | 
| 14 | 
            +
                    ticker.stop if x == loop_number-1
         | 
| 15 | 
            +
                    x += 1
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                  elapsed = Time.now - start
         | 
| 18 | 
            +
                  {:counter => x, :elapsed => elapsed.round}
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                it "should starts with default interval of 1.second" do
         | 
| 22 | 
            +
                  ticker.interval.should == 1.second
         | 
| 7 23 | 
             
                end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                describe "execution loop" do
         | 
| 26 | 
            +
                  it "execute #{loop_number} times in #{loop_number} seconds if the interval is of 1 second" do
         | 
| 27 | 
            +
                    result = ticker_execute
         | 
| 28 | 
            +
                    result[:elapsed].should == loop_number
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                describe "#with" do
         | 
| 33 | 
            +
                  it "returns a default ticker" do
         | 
| 34 | 
            +
                    ticker = Ticker.new.with
         | 
| 35 | 
            +
                    ticker.should be_a Ticker
         | 
| 36 | 
            +
                  end
         | 
| 37 | 
            +
                end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                describe '#stop' do
         | 
| 40 | 
            +
                  it 'stops Ticker loops' do
         | 
| 41 | 
            +
                    result = ticker_execute
         | 
| 42 | 
            +
                    result[:counter].should == loop_number
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                describe "#repeat" do
         | 
| 47 | 
            +
                  it "starts loop with default interval" do
         | 
| 48 | 
            +
                    ticker = Ticker.new
         | 
| 49 | 
            +
                    x = 0
         | 
| 50 | 
            +
                    start = Time.now
         | 
| 51 | 
            +
                    ticker.repeat do 
         | 
| 52 | 
            +
                      ticker.stop if x == loop_number-1
         | 
| 53 | 
            +
                      x += 1
         | 
| 54 | 
            +
                    end
         | 
| 55 | 
            +
                    elapsed = Time.now - start
         | 
| 56 | 
            +
                    elapsed.round.should == loop_number
         | 
| 57 | 
            +
                  end
         | 
| 58 | 
            +
                end
         | 
| 59 | 
            +
             | 
| 8 60 | 
             
              end
         | 
| 9 61 | 
             
            end
         | 
| @@ -1,29 +1,30 @@ | |
| 1 1 | 
             
            require 'spec_helper'
         | 
| 2 2 |  | 
| 3 | 
            +
            def tick
         | 
| 4 | 
            +
              2
         | 
| 5 | 
            +
            end
         | 
| 6 | 
            +
             | 
| 3 7 | 
             
            module TickTacker
         | 
| 4 8 | 
             
              describe Timer do
         | 
| 5 | 
            -
                let(: | 
| 9 | 
            +
                let(:ticker) { Ticker.new.with :interval => 1.second }
         | 
| 10 | 
            +
                let(:timer) { Timer.new.with(:ticker => ticker, :total_time => 15.minutes) }
         | 
| 6 11 | 
             
                let(:observer) { double('TimerObserver').as_null_object }
         | 
| 7 12 |  | 
| 8 13 | 
             
                it 'should be configured with 25 minutes by default' do
         | 
| 9 | 
            -
                  Timer.new. | 
| 10 | 
            -
                end
         | 
| 11 | 
            -
             | 
| 12 | 
            -
                it 'should have a default ticker configured with 1.second' do
         | 
| 13 | 
            -
                  timer.ticker_time.should == 1.second
         | 
| 14 | 
            +
                  Timer.new.total_time.should == 25.minutes
         | 
| 14 15 | 
             
                end
         | 
| 15 16 |  | 
| 16 17 | 
             
                it 'should receive a number of minutes as parameter to configure a count down' do
         | 
| 17 | 
            -
                  timer. | 
| 18 | 
            +
                  timer.total_time.should == 15.minutes
         | 
| 18 19 | 
             
                end
         | 
| 19 20 |  | 
| 20 | 
            -
                it 'should reduce by  | 
| 21 | 
            +
                it 'should reduce by 1.second the remaining time when receives an update' do
         | 
| 21 22 | 
             
                  timer.update :elapsed_time => 1.second
         | 
| 22 23 | 
             
                  timer.elapsed_time.should == 1.second
         | 
| 23 24 | 
             
                end
         | 
| 24 25 |  | 
| 25 26 | 
             
                it 'should notify the observer about the second elapsed' do
         | 
| 26 | 
            -
                  timer = Timer.new : | 
| 27 | 
            +
                  timer = Timer.new.with :total_time => 1.minute
         | 
| 27 28 | 
             
                  timer.add_observer observer
         | 
| 28 29 | 
             
                  observer.should_receive(:update).with({:time_remaining => 59.seconds}).once
         | 
| 29 30 | 
             
                  timer.update :elapsed_time => 1.second
         | 
| @@ -37,5 +38,23 @@ module TickTacker | |
| 37 38 | 
             
                    timer.update :elapsed_time => 1.second
         | 
| 38 39 | 
             
                  end
         | 
| 39 40 | 
             
                end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                describe "#start" do
         | 
| 43 | 
            +
                  it "calls Ticker#repeat" do
         | 
| 44 | 
            +
                    tickr = double("Ticker").as_null_object
         | 
| 45 | 
            +
                    t = Timer.new.with :ticker => tickr
         | 
| 46 | 
            +
                    tickr.should_receive(:repeat).once
         | 
| 47 | 
            +
                    t.start
         | 
| 48 | 
            +
                  end
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                describe "#stop" do
         | 
| 52 | 
            +
                  it "stops the ticker" do
         | 
| 53 | 
            +
                    tickr = double("Ticker").as_null_object
         | 
| 54 | 
            +
                    t = Timer.new.with :ticker => tickr
         | 
| 55 | 
            +
                    tickr.should_receive(:stop).once
         | 
| 56 | 
            +
                    t.stop
         | 
| 57 | 
            +
                  end
         | 
| 58 | 
            +
                end
         | 
| 40 59 | 
             
              end
         | 
| 41 60 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,12 +1,8 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification 
         | 
| 2 2 | 
             
            name: tick_tacker
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version 
         | 
| 4 | 
            -
              prerelease:  | 
| 5 | 
            -
               | 
| 6 | 
            -
              - 0
         | 
| 7 | 
            -
              - 1
         | 
| 8 | 
            -
              - 2
         | 
| 9 | 
            -
              version: 0.1.2
         | 
| 4 | 
            +
              prerelease: 
         | 
| 5 | 
            +
              version: 0.1.3
         | 
| 10 6 | 
             
            platform: ruby
         | 
| 11 7 | 
             
            authors: 
         | 
| 12 8 | 
             
            - Ricardo Valeriano
         | 
| @@ -32,6 +28,8 @@ files: | |
| 32 28 | 
             
            - Gemfile
         | 
| 33 29 | 
             
            - Gemfile.lock
         | 
| 34 30 | 
             
            - Rakefile
         | 
| 31 | 
            +
            - examples/cowntdown.rb
         | 
| 32 | 
            +
            - examples/cowntdown_respawn.rb
         | 
| 35 33 | 
             
            - lib/tick_tacker.rb
         | 
| 36 34 | 
             
            - lib/tick_tacker/ticker.rb
         | 
| 37 35 | 
             
            - lib/tick_tacker/timer.rb
         | 
| @@ -54,21 +52,17 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 54 52 | 
             
              requirements: 
         | 
| 55 53 | 
             
              - - ">="
         | 
| 56 54 | 
             
                - !ruby/object:Gem::Version 
         | 
| 57 | 
            -
                  segments: 
         | 
| 58 | 
            -
                  - 0
         | 
| 59 55 | 
             
                  version: "0"
         | 
| 60 56 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement 
         | 
| 61 57 | 
             
              none: false
         | 
| 62 58 | 
             
              requirements: 
         | 
| 63 59 | 
             
              - - ">="
         | 
| 64 60 | 
             
                - !ruby/object:Gem::Version 
         | 
| 65 | 
            -
                  segments: 
         | 
| 66 | 
            -
                  - 0
         | 
| 67 61 | 
             
                  version: "0"
         | 
| 68 62 | 
             
            requirements: []
         | 
| 69 63 |  | 
| 70 64 | 
             
            rubyforge_project: tick_tacker
         | 
| 71 | 
            -
            rubygems_version: 1. | 
| 65 | 
            +
            rubygems_version: 1.6.2
         | 
| 72 66 | 
             
            signing_key: 
         | 
| 73 67 | 
             
            specification_version: 3
         | 
| 74 68 | 
             
            summary: A timer wich accepts subscribers and use a ticker
         |