hmtime 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Manifest +4 -0
  2. data/README.rdoc +0 -0
  3. data/Rakefile +14 -0
  4. data/hmtime.gemspec +29 -0
  5. data/lib/hmtime.rb +121 -0
  6. metadata +59 -0
@@ -0,0 +1,4 @@
1
+ README.rdoc
2
+ Rakefile
3
+ lib/hmtime.rb
4
+ Manifest
File without changes
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('hmtime','0.1.0') do |p|
6
+ p.description = "A simple HMTime object for representing and performing math on hours and minutes. HHH:MM"
7
+ p.url = "http://github.com/advorak/hmtime"
8
+ p.author = "Andrew Dvorak"
9
+ p.email = "advorak@gmail.com"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.development_dependencies = []
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "hmtime"
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 = ["Andrew Dvorak"]
9
+ s.date = "2012-04-03"
10
+ s.description = "A simple HMTime object for representing and performing math on hours and minutes. HHH:MM"
11
+ s.email = "advorak@gmail.com"
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/hmtime.rb"]
13
+ s.files = ["README.rdoc", "Rakefile", "lib/hmtime.rb", "Manifest", "hmtime.gemspec"]
14
+ s.homepage = "http://github.com/advorak/hmtime"
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Hmtime", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = "hmtime"
18
+ s.rubygems_version = "1.8.11"
19
+ s.summary = "A simple HMTime object for representing and performing math on hours and minutes. HHH:MM"
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
@@ -0,0 +1,121 @@
1
+ class String
2
+ def to_hmtime
3
+ HMTime.new self
4
+ end
5
+ end
6
+
7
+ class HMTime
8
+ ERROR_MESSAGE_1 = 'Must specify String in format of: `hhh:mm`, where h=Hours and m=Minutes; or integer in the form of #####'
9
+
10
+ def initialize(value=0)
11
+ @minutes = check_arguments(value)
12
+ end
13
+
14
+ def inspect
15
+ time
16
+ end
17
+
18
+ def time
19
+ my_time = [hours, minutes.to_s.rjust(2,"0")].join(":")
20
+ if @minutes < 0
21
+ "-" + my_time
22
+ else
23
+ my_time
24
+ end
25
+ end
26
+
27
+ def hours
28
+ @minutes.abs / 60
29
+ end
30
+
31
+ def minutes
32
+ @minutes.abs % 60
33
+ end
34
+
35
+ def to_s
36
+ time
37
+ end
38
+
39
+ def to_i
40
+ @minutes
41
+ end
42
+
43
+ def +(value)
44
+ if value.is_a?(self.class)
45
+ self.class.new(@minutes + value.to_i)
46
+ else
47
+ self.class.new(@minutes + check_arguments(value))
48
+ end
49
+ end
50
+
51
+ # BUG: Doesn't work if value < 0
52
+ def -(value)
53
+ if value.is_a?(self.class)
54
+ self.class.new(@minutes - value.to_i)
55
+ else
56
+ self.class.new(@minutes - check_arguments(value))
57
+ end
58
+ end
59
+
60
+ def /(value)
61
+ if value.is_a?(self.class)
62
+ self.class.new(@minutes / value.to_i)
63
+ else
64
+ self.class.new(@minutes / check_arguments(value))
65
+ end
66
+ end
67
+
68
+
69
+ # BUG: Doesn't work if value < 0
70
+ def *(value)
71
+ if value.is_a?(self.class)
72
+ self.class.new(@minutes * value.to_i)
73
+ else
74
+ self.class.new(@minutes * check_arguments(value))
75
+ end
76
+ end
77
+
78
+ def ==(value)
79
+ @minutes == value.to_i
80
+ end
81
+
82
+ # For Comparison
83
+ def <=>(value)
84
+ if @minutes < value.to_i
85
+ -1
86
+ elsif @minutes > value.to_i
87
+ 1
88
+ elsif @minutes == value.to_i
89
+ 0
90
+ end
91
+ end
92
+
93
+ def -@
94
+ self.class.new(-@minutes)
95
+ end
96
+
97
+ private
98
+
99
+ def check_arguments(value)
100
+ if(value == nil)
101
+ value
102
+ else
103
+ if(value.is_a?(Fixnum))
104
+ arguments = [value / 60, value % 60]
105
+ elsif (!value.is_a?(String))
106
+ raise TypeError, ERROR_MESSAGE_1
107
+
108
+ # Is the argument a string of format "hhh:mm"? If not, raise an error.
109
+ elsif !(arguments = value.strip.match(/^(\d+|\s+)?:(\d{2})$/).to_a[1..2])
110
+ raise ArgumentError, ERROR_MESSAGE_1
111
+ end
112
+
113
+ ### How to do a warning?? Is this correct?
114
+ if arguments[1].to_i > 59
115
+ warn "Warning: The mm (minutes) field of `hhh:mm` should be no greater than 59."
116
+ end
117
+ hours, minutes = arguments.collect{|arg| arg.to_i}
118
+ return (hours * 60) + minutes
119
+ end
120
+ end
121
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hmtime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Dvorak
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-03 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple HMTime object for representing and performing math on hours
15
+ and minutes. HHH:MM
16
+ email: advorak@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files:
20
+ - README.rdoc
21
+ - lib/hmtime.rb
22
+ files:
23
+ - README.rdoc
24
+ - Rakefile
25
+ - lib/hmtime.rb
26
+ - Manifest
27
+ - hmtime.gemspec
28
+ homepage: http://github.com/advorak/hmtime
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --line-numbers
33
+ - --inline-source
34
+ - --title
35
+ - Hmtime
36
+ - --main
37
+ - README.rdoc
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '1.2'
52
+ requirements: []
53
+ rubyforge_project: hmtime
54
+ rubygems_version: 1.8.11
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A simple HMTime object for representing and performing math on hours and
58
+ minutes. HHH:MM
59
+ test_files: []