a_clockwork_ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ gem "rcov", ">= 0"
12
+ end
13
+
14
+ group :test do
15
+ gem "shoulda"
16
+ gem "timecop"
17
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+ timecop (0.3.5)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.0.0)
19
+ jeweler (~> 1.5.2)
20
+ rcov
21
+ shoulda
22
+ timecop
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Mike Taylor, Michael Pearce
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # A Clockwork Ruby - A clock class for use with time of day.
2
+
3
+ # Credits
4
+
5
+ Shared Shoulda is maintained by [Mike Taylor](http://github.com/sealabcore) and is funded by [BookRenter.com](http://www.bookrenter.com "BookRenter.com"). Help provided by [Michael Pearce](http://github.com/michaelgpearce), and [Miguel Huibonhoa](http://github.com/phuibonhoa).
6
+
7
+ ![BookRenter.com Logo](http://assets0.bookrenter.com/images/header/bookrenter_logo.gif "BookRenter.com")
8
+
9
+
10
+ # Copyright
11
+
12
+ Copyright (c) 2012 Bookrenter.com. See LICENSE.txt for further details.
13
+
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "a_clockwork_ruby"
16
+ gem.homepage = "http://github.com/sealabcore/a_clockwork_ruby"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A clock class for use with time of day}
19
+ gem.description = %Q{A clock class for use with time of day}
20
+ gem.email = "michael.taylor@bookrenter.com"
21
+ gem.authors = ["Mike Taylor", "Michael Pearce"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ require 'rcov/rcovtask'
35
+ Rcov::RcovTask.new do |test|
36
+ test.libs << 'test'
37
+ test.pattern = 'test/**/test_*.rb'
38
+ test.verbose = true
39
+ end
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+
47
+ rdoc.rdoc_dir = 'rdoc'
48
+ rdoc.title = "a_clockwork_ruby #{version}"
49
+ rdoc.rdoc_files.include('README*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "a_clockwork_ruby"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Mike Taylor", "Michael Pearce"]
12
+ s.date = "2012-02-03"
13
+ s.description = "A clock class for use with time of day"
14
+ s.email = "michael.taylor@bookrenter.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "a_clockwork_ruby.gemspec",
28
+ "lib/a_clockwork_ruby.rb",
29
+ "lib/a_clockwork_ruby/clock.rb",
30
+ "test/clock_test.rb",
31
+ "test/helper.rb"
32
+ ]
33
+ s.homepage = "http://github.com/sealabcore/a_clockwork_ruby"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.10"
37
+ s.summary = "A clock class for use with time of day"
38
+ s.test_files = [
39
+ "test/clock_test.rb",
40
+ "test/helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
49
+ s.add_development_dependency(%q<rcov>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
53
+ s.add_dependency(%q<rcov>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_dependency(%q<rcov>, [">= 0"])
59
+ end
60
+ end
61
+
@@ -0,0 +1,79 @@
1
+ class Clock
2
+ include Comparable
3
+ attr_accessor :hour, :min, :sec, :usec
4
+
5
+ def initialize(hour = nil, min = nil, sec = nil, usec = nil)
6
+ if hour.nil?
7
+ time = Time.now
8
+ @hour = time.hour
9
+ @min = time.min
10
+ @sec = time.sec
11
+ @usec = time.usec
12
+ else
13
+ @hour = hour
14
+ @min = min || 0
15
+ @sec = sec || 0
16
+ @usec = usec || 0
17
+ end
18
+ end
19
+
20
+ def self.now
21
+ self.new
22
+ end
23
+
24
+ def self.from_i seconds
25
+ self.new(seconds / 60**2 % 60**2, seconds / 60 % 60, seconds % 60)
26
+ end
27
+
28
+ def hour= hour
29
+ raise ArgumentError if hour < 0 or hour > 23
30
+ @hour = hour
31
+ end
32
+
33
+ def min= min
34
+ raise ArgumentError if min < 0 or min > 59
35
+ @min = min
36
+ end
37
+
38
+ def sec= sec
39
+ raise ArgumentError if sec < 0 or sec > 59
40
+ @sec = sec
41
+ end
42
+
43
+ def usec= usec
44
+ raise ArgumentError if usec < 0 or usec > 999999
45
+ @usec = usec
46
+ end
47
+
48
+ def strfclock format
49
+ to_time.strftime(format)
50
+ end
51
+
52
+ def to_time
53
+ to_time_helper(Time.now)
54
+ end
55
+
56
+ def to_s
57
+ strfclock("%l:%M %p")
58
+ end
59
+
60
+ def to_i
61
+ @sec + (@min * 60) + (@hour * 60**2)
62
+ end
63
+
64
+ def == other
65
+ time = Time.now
66
+ to_time_helper(time) == other.send(:to_time_helper, time)
67
+ end
68
+
69
+ def <=> other
70
+ time = Time.now
71
+ to_time_helper(time) <=> other.send(:to_time_helper, time)
72
+ end
73
+
74
+ private
75
+
76
+ def to_time_helper(time)
77
+ Time.mktime(time.year, time.month, time.day, @hour, @min, @sec, @usec)
78
+ end
79
+ end
@@ -0,0 +1,3 @@
1
+ $: << File.expand_path(File.join(File.dirname(__FILE__), 'a_clockwork_ruby'))
2
+
3
+ require 'clock'
@@ -0,0 +1,139 @@
1
+ require 'helper'
2
+ require 'a_clockwork_ruby'
3
+ require 'timecop'
4
+
5
+ class ClockTest < Test::Unit::TestCase
6
+
7
+ context ".new" do
8
+ should "return a clock using the current time when no args are passed" do
9
+ Timecop.freeze do
10
+ time = Time.now
11
+ clock = Clock.new
12
+ assert_equal time.hour, clock.hour
13
+ assert_equal time.min, clock.min
14
+ assert_equal time.sec, clock.sec
15
+ assert_equal time.usec, clock.usec
16
+ end
17
+ end
18
+
19
+ should "allow clocky arguments" do
20
+ clock = Clock.new(1, 2, 3, 4)
21
+ assert_equal 1, clock.hour
22
+ assert_equal 2, clock.min
23
+ assert_equal 3, clock.sec
24
+ assert_equal 4, clock.usec
25
+ end
26
+
27
+ should "allow optional clocky arguments" do
28
+ clock = Clock.new(1)
29
+ assert_equal 1, clock.hour
30
+ assert_equal 0, clock.min
31
+ assert_equal 0, clock.sec
32
+ assert_equal 0, clock.usec
33
+ end
34
+ end
35
+
36
+ context ".strfclock" do
37
+ should "allow formatting using strftime args" do
38
+ clock = Clock.new(14, 2, 3)
39
+ assert_equal " 2:02:03 PM", clock.strfclock("%l:%M:%S %p")
40
+ end
41
+ end
42
+
43
+ context ".to_s" do
44
+ should "return clock as hours:minutes" do
45
+ clock = Clock.new(13, 20)
46
+ assert_equal " 1:20 PM", clock.to_s
47
+ end
48
+ end
49
+
50
+ context ".==" do
51
+ should "check equality" do
52
+ assert Clock.new(3, 24) == Clock.new(3, 24)
53
+ assert Clock.new(3, 24) != Clock.new(3, 23)
54
+ end
55
+ end
56
+
57
+ context ".<=>" do
58
+ should "compare clocks" do
59
+ assert Clock.new(3, 23) < Clock.new(3, 24)
60
+ assert Clock.new(3, 24) > Clock.new(3, 23)
61
+ end
62
+ end
63
+
64
+ context ".to_i" do
65
+ should "return the number of seconds" do
66
+ clock = Clock.new(1, 2, 3, 4)
67
+ assert_equal 3 + (2 * 60) + (1 * 60**2), clock.to_i
68
+ end
69
+ end
70
+
71
+ context ".from_i" do
72
+ should "create a clock from the number of seconds" do
73
+ seconds = 3 + (2 * 60) + (1 * 60**2)
74
+ clock = Clock.from_i(seconds)
75
+ assert_equal 3, clock.sec
76
+ assert_equal 2, clock.min
77
+ assert_equal 1, clock.hour
78
+ end
79
+ end
80
+
81
+ context ".now" do
82
+ should "return a clock using the current time" do
83
+ Timecop.freeze do
84
+ time = Time.now
85
+ clock = Clock.now
86
+ assert_equal time.hour, clock.hour
87
+ assert_equal time.min, clock.min
88
+ assert_equal time.sec, clock.sec
89
+ assert_equal time.usec, clock.usec
90
+ end
91
+ end
92
+ end
93
+
94
+ context "setters" do
95
+ setup do
96
+ @clock = Clock.new
97
+ end
98
+
99
+ should "set the hour" do
100
+ @clock.hour = 14
101
+ assert_equal 14, @clock.hour
102
+ end
103
+
104
+ should "raise when hour is set to value out of range" do
105
+ assert_raise(ArgumentError) { @clock.hour = 24 }
106
+ assert_raise(ArgumentError) { @clock.hour = -1 }
107
+ end
108
+
109
+ should "set the min" do
110
+ @clock.min = 14
111
+ assert_equal 14, @clock.min
112
+ end
113
+
114
+ should "raise when minute is set to value out of range" do
115
+ assert_raise(ArgumentError) { @clock.min = 60 }
116
+ assert_raise(ArgumentError) { @clock.min = -1 }
117
+ end
118
+
119
+ should "set the sec" do
120
+ @clock.sec = 14
121
+ assert_equal 14, @clock.sec
122
+ end
123
+
124
+ should "raise when seconds is set to value out of range" do
125
+ assert_raise(ArgumentError) { @clock.sec = 60 }
126
+ assert_raise(ArgumentError) { @clock.sec = -1 }
127
+ end
128
+
129
+ should "set the usec" do
130
+ @clock.usec = 14
131
+ assert_equal 14, @clock.usec
132
+ end
133
+
134
+ should "raise when usec is set to value out of range" do
135
+ assert_raise(ArgumentError) { @clock.sec = 1000000 }
136
+ assert_raise(ArgumentError) { @clock.sec = -1 }
137
+ end
138
+ end
139
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development, :test)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: a_clockwork_ruby
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mike Taylor
14
+ - Michael Pearce
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2012-02-03 00:00:00 Z
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 23
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 0
32
+ version: 1.0.0
33
+ type: :development
34
+ requirement: *id001
35
+ prerelease: false
36
+ name: bundler
37
+ - !ruby/object:Gem::Dependency
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 1
46
+ - 5
47
+ - 2
48
+ version: 1.5.2
49
+ type: :development
50
+ requirement: *id002
51
+ prerelease: false
52
+ name: jeweler
53
+ - !ruby/object:Gem::Dependency
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :development
64
+ requirement: *id003
65
+ prerelease: false
66
+ name: rcov
67
+ description: A clock class for use with time of day
68
+ email: michael.taylor@bookrenter.com
69
+ executables: []
70
+
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.md
76
+ files:
77
+ - .document
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - VERSION
84
+ - a_clockwork_ruby.gemspec
85
+ - lib/a_clockwork_ruby.rb
86
+ - lib/a_clockwork_ruby/clock.rb
87
+ - test/clock_test.rb
88
+ - test/helper.rb
89
+ homepage: http://github.com/sealabcore/a_clockwork_ruby
90
+ licenses:
91
+ - MIT
92
+ post_install_message:
93
+ rdoc_options: []
94
+
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 3
112
+ segments:
113
+ - 0
114
+ version: "0"
115
+ requirements: []
116
+
117
+ rubyforge_project:
118
+ rubygems_version: 1.8.10
119
+ signing_key:
120
+ specification_version: 3
121
+ summary: A clock class for use with time of day
122
+ test_files:
123
+ - test/clock_test.rb
124
+ - test/helper.rb