tick_tacker 0.1.1 → 0.1.2

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/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gem "rspec"
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ rspec (2.6.0)
6
+ rspec-core (~> 2.6.0)
7
+ rspec-expectations (~> 2.6.0)
8
+ rspec-mocks (~> 2.6.0)
9
+ rspec-core (2.6.3)
10
+ rspec-expectations (2.6.0)
11
+ diff-lcs (~> 1.1.2)
12
+ rspec-mocks (2.6.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ rspec
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new 'rspec' do |t|
5
+ t.pattern = 'spec/tick_tacker/*_spec.rb'
6
+ t.rspec_opts = ['--tty', '--color', '--format documentation']
7
+ end
8
+
9
+ task :default => [:rspec]
@@ -0,0 +1,24 @@
1
+ module TickTacker
2
+ class Ticker
3
+ def initialize
4
+ @can_run = true
5
+ end
6
+
7
+ def time_block
8
+ start_time = Time.now
9
+ Thread.new { yield }
10
+ Time.now - start_time
11
+ end
12
+
13
+ def repeat_every(seconds)
14
+ while @can_run do
15
+ time_spent = time_block { yield } # To handle -ve sleep interaval
16
+ sleep(seconds - time_spent) if time_spent < seconds
17
+ end
18
+ end
19
+
20
+ def stop
21
+ @can_run = false
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,47 @@
1
+ require 'observer'
2
+
3
+ module TickTacker
4
+ class Timer
5
+ include Observable
6
+
7
+ attr_reader :total_minutes, :ticker_time
8
+ attr_accessor :elapsed_time
9
+
10
+ def initialize(options = {:with => 25.minutes})
11
+ @total_minutes = options[:with]
12
+ @ticker_time = 1.second
13
+ @ticker = Ticker.new
14
+ end
15
+
16
+ def notify
17
+ time_remaining = total_minutes - elapsed_time
18
+ notification_options = {:time_remaining => time_remaining}
19
+ notify_observers(notification_options)
20
+ end
21
+
22
+ def update_time_elapsed(time)
23
+ @elapsed_time += time
24
+ if @elapsed_time >= @total_minutes
25
+ @ticker.stop
26
+ end
27
+ end
28
+
29
+ def update(options)
30
+ @elapsed_time ||= 0
31
+ options[:time_elapsed] = 1 if options[:time_elapsed].nil?
32
+ update_time_elapsed(options[:time_elapsed])
33
+ changed
34
+ notify
35
+ end
36
+
37
+ def start
38
+ @ticker.repeat_every 1.second do
39
+ self.update({})
40
+ end
41
+ end
42
+
43
+ def stop
44
+ @ticker.stop
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module TickTacker
2
+ VERSION = '0.1.2'
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'tick_tacker/timer'
2
+ require 'tick_tacker/ticker'
3
+
4
+ class Fixnum
5
+ def minutes
6
+ self * 60
7
+ end
8
+
9
+ def minute
10
+ self.minutes
11
+ end
12
+
13
+ def seconds
14
+ self
15
+ end
16
+
17
+ def second
18
+ self.seconds
19
+ end
20
+ end
@@ -0,0 +1 @@
1
+ require 'tick_tacker'
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ module TickTacker
4
+ describe Ticker do
5
+ it 'should test ticker' do
6
+ pending
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ module TickTacker
4
+ describe Timer do
5
+ let(:timer) { Timer.new :with => 15.minutes }
6
+ let(:observer) { double('TimerObserver').as_null_object }
7
+
8
+ it 'should be configured with 25 minutes by default' do
9
+ Timer.new.total_minutes.should == 25.minutes
10
+ end
11
+
12
+ it 'should have a default ticker configured with 1.second' do
13
+ timer.ticker_time.should == 1.second
14
+ end
15
+
16
+ it 'should receive a number of minutes as parameter to configure a count down' do
17
+ timer.total_minutes.should == 15.minutes
18
+ end
19
+
20
+ it 'should reduce by one second the remaining time when receives an update' do
21
+ timer.update :elapsed_time => 1.second
22
+ timer.elapsed_time.should == 1.second
23
+ end
24
+
25
+ it 'should notify the observer about the second elapsed' do
26
+ timer = Timer.new :with => 1.minutes
27
+ timer.add_observer observer
28
+ observer.should_receive(:update).with({:time_remaining => 59.seconds}).once
29
+ timer.update :elapsed_time => 1.second
30
+ end
31
+
32
+ it 'should notify an observer when the time is over' do
33
+ timer.add_observer observer
34
+ timer.elapsed_time = 14.minutes
35
+ observer.should_receive(:update).with({:time_remaining => 0}).once
36
+ 60.times do
37
+ timer.update :elapsed_time => 1.second
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib/', __FILE__)
3
+ $:.unshift lib unless $:.include?(lib)
4
+
5
+ require "tick_tacker/version"
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "tick_tacker"
9
+ s.version = TickTacker::VERSION
10
+ s.platform = Gem::Platform::RUBY
11
+ s.authors = ["Ricardo Valeriano"]
12
+ s.email = ["ricardo@backslashes.net"]
13
+ s.homepage = "https://github.com/ricardovaleriano/tick_tacker"
14
+ s.summary = %q{A timer wich accepts subscribers and use a ticker}
15
+ s.description = %q{From time to time, the timer notifies the subscribers}
16
+
17
+ s.rubyforge_project = "tick_tacker"
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
23
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ricardo Valeriano
@@ -27,8 +27,19 @@ extensions: []
27
27
 
28
28
  extra_rdoc_files: []
29
29
 
30
- files: []
31
-
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - Gemfile.lock
34
+ - Rakefile
35
+ - lib/tick_tacker.rb
36
+ - lib/tick_tacker/ticker.rb
37
+ - lib/tick_tacker/timer.rb
38
+ - lib/tick_tacker/version.rb
39
+ - spec/spec_helper.rb
40
+ - spec/tick_tacker/ticker_spec.rb
41
+ - spec/tick_tacker/timer_spec.rb
42
+ - tick_tacker.gemspec
32
43
  has_rdoc: true
33
44
  homepage: https://github.com/ricardovaleriano/tick_tacker
34
45
  licenses: []
@@ -61,5 +72,7 @@ rubygems_version: 1.3.7
61
72
  signing_key:
62
73
  specification_version: 3
63
74
  summary: A timer wich accepts subscribers and use a ticker
64
- test_files: []
65
-
75
+ test_files:
76
+ - spec/spec_helper.rb
77
+ - spec/tick_tacker/ticker_spec.rb
78
+ - spec/tick_tacker/timer_spec.rb