meskyanichi-timer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +68 -0
  2. data/Rakefile +15 -0
  3. data/lib/timer.rb +89 -0
  4. data/timer.gemspec +31 -0
  5. metadata +63 -0
data/README.rdoc ADDED
@@ -0,0 +1,68 @@
1
+ Timer
2
+ =====
3
+
4
+ Timer, a simple plugin that calculates the time between a "start time" and a "end time".
5
+ Currently Timer has the ability to take a Time object or a timestamp for the start and end times and will calculate the
6
+ time between these values in days, hours (and days in hours), minutes and seconds!
7
+ There is also the option to force it to display 2 digits at all times when you are creating a 'non-words' timer,
8
+ though would display for example "09:34:02" instead of "9:34:2". However, when you aren't forcing the two digits to be displayed
9
+ you could create a string such as: "7 days, 5 hours, 11 minutes and 56 seconds remaining..".
10
+
11
+ The output it quite limited, but it also enables you to either extend the class to create your own custom outputs.
12
+ You can of course choose not to create a whole new class to do this and just generate the strings inside of a specific helper/module,
13
+ whatever you think is best!
14
+
15
+ Example #1
16
+ =======
17
+
18
+ # New Instance of Timer
19
+ t = Timer.new
20
+
21
+ # Set the start time to the current time
22
+ t.start_time = Time.now
23
+
24
+ # Set the end time to begin 2010
25
+ t.end_time = Time.parse('2010-01-01 00:00:00')
26
+
27
+ # Output Timer
28
+ <%= "#{t.days} days, #{t.hours} hours, #{t.minutes} minutes, #{t.seconds} seconds" %>
29
+
30
+ # Outputs
31
+ 313 days, 0 hours, 12 minutes, 29 seconds
32
+
33
+
34
+ Example #2
35
+ =======
36
+
37
+ # New Instance of Timer
38
+ t = Timer.new
39
+
40
+ # Set the start time to the current time
41
+ t.start_time = Time.now
42
+
43
+ # Set the end time to begin 2010
44
+ t.end_time = Time.parse('2010-01-01 00:00:00')
45
+
46
+ # Force Timer to atleast output two digits (the zero's)
47
+ t.force_two_digits = true
48
+
49
+
50
+ # Output Timer
51
+ <%= "#{t.days_in_hours}:#{t.minutes}:#{t.seconds}" %>
52
+
53
+ #Outputs (Hours:Minutes:Seconds)
54
+ 7512:09:43
55
+
56
+
57
+ OR
58
+
59
+
60
+ # Output Timer
61
+ <%= "#{t.hours}:#{t.minutes}:#{t.seconds} in #{t.days} days" %>
62
+
63
+ #Outputs (Hours:Minutes:Seconds)
64
+ 00:06:51 in 313 days
65
+
66
+
67
+
68
+ Copyright (c) 2009 [Michael van Rooijen], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ # Rakefile
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'echoe'
5
+
6
+ Echoe.new('timer', '0.1.0') do |p|
7
+ p.description = "This is a Timer class that calculates the time between two moments."
8
+ p.url = "http://github.com/meskyanichi/timer"
9
+ p.author = "Michael van Rooijen"
10
+ p.email = "meskyan@gmail.com"
11
+ p.ignore_pattern = ["tmp/*", "script/*"]
12
+ p.development_dependencies = []
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
data/lib/timer.rb ADDED
@@ -0,0 +1,89 @@
1
+ class Timer
2
+
3
+ # Initial Values
4
+ attr_accessor :start_time
5
+ attr_accessor :end_time
6
+
7
+ # Params
8
+ attr_accessor :timer
9
+ attr_accessor :difference_in_seconds
10
+
11
+ # Booleans
12
+ attr_accessor :calculated
13
+ attr_accessor :force_two_digits
14
+
15
+
16
+
17
+ def days
18
+ initialze_parse_settings
19
+ return timer[:days]
20
+ end
21
+
22
+ def hours
23
+ initialze_parse_settings
24
+ return timer[:hours]
25
+ end
26
+
27
+ def days_in_hours
28
+ initialze_parse_settings
29
+ return timer[:days_in_hours]
30
+ end
31
+
32
+ def minutes
33
+ initialze_parse_settings
34
+ return timer[:minutes]
35
+ end
36
+
37
+ def seconds
38
+ initialze_parse_settings
39
+ return timer[:seconds]
40
+ end
41
+
42
+ def exceeded?
43
+ return true if start_time > end_time
44
+ end
45
+
46
+
47
+ private
48
+
49
+ def initialze_parse_settings
50
+ update_calculations unless already_calculated?
51
+ make_2_digits if forces_two_digits?
52
+ end
53
+
54
+ def forces_two_digits?
55
+ return true if self.force_two_digits
56
+ end
57
+
58
+ def already_calculated?
59
+ if self.calculated
60
+ return true
61
+ else
62
+ self.calculated = true
63
+ return false
64
+ end
65
+ end
66
+
67
+ def update_calculations
68
+ if start_time and end_time
69
+ difference_in_seconds = end_time.to_i - start_time.to_i
70
+ end
71
+
72
+ if difference_in_seconds
73
+ self.timer = {}
74
+ self.timer[:days] = ( difference_in_seconds / 60 / 60 / 24 ).to_i
75
+ self.timer[:hours] = ( ( difference_in_seconds - ( self.timer[:days] * 60 * 60 * 24 ) ) / 60 / 60 ).to_i
76
+ self.timer[:minutes] = ( ( difference_in_seconds - ( self.timer[:days] * 60 * 60 * 24 ) - ( self.timer[:hours] * 60 * 60 ) ) / 60 ).to_i
77
+ self.timer[:seconds] = ( ( ( difference_in_seconds - ( self.timer[:days] * 60 * 60 * 24 ) - ( self.timer[:hours] * 60 * 60 ) ) - ( self.timer[:minutes] * 60 ) ) ).to_i
78
+ self.timer[:days_in_hours] = ( ( self.timer[:days] * 24) + self.timer[:hours] ).to_i
79
+ else
80
+ raise "To update calculations you need atleast a start_time and end_time."
81
+ end
82
+ end
83
+
84
+ def make_2_digits
85
+ timers = [:hours, :minutes, :seconds, :days_in_hours]
86
+ timers.each {|t| self.timer[t] = sprintf('%02d', self.timer[t]) }
87
+ end
88
+
89
+ end
data/timer.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{timer}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Michael van Rooijen"]
9
+ s.date = %q{2009-03-30}
10
+ s.description = %q{This is a Timer class that calculates the time between two moments.}
11
+ s.email = %q{meskyan@gmail.com}
12
+ s.extra_rdoc_files = ["lib/timer.rb", "README.rdoc"]
13
+ s.files = ["lib/timer.rb", "Manifest", "Rakefile", "README.rdoc", "timer.gemspec"]
14
+ s.has_rdoc = true
15
+ s.homepage = %q{http://github.com/meskyanichi/timer}
16
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Timer", "--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{timer}
19
+ s.rubygems_version = %q{1.3.1}
20
+ s.summary = %q{This is a Timer class that calculates the time between two moments.}
21
+
22
+ if s.respond_to? :specification_version then
23
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
+ s.specification_version = 2
25
+
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
27
+ else
28
+ end
29
+ else
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: meskyanichi-timer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael van Rooijen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-30 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This is a Timer class that calculates the time between two moments.
17
+ email: meskyan@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - lib/timer.rb
24
+ - README.rdoc
25
+ files:
26
+ - lib/timer.rb
27
+ - Manifest
28
+ - Rakefile
29
+ - README.rdoc
30
+ - timer.gemspec
31
+ has_rdoc: true
32
+ homepage: http://github.com/meskyanichi/timer
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --line-numbers
36
+ - --inline-source
37
+ - --title
38
+ - Timer
39
+ - --main
40
+ - README.rdoc
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "1.2"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project: timer
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: This is a Timer class that calculates the time between two moments.
62
+ test_files: []
63
+