hmtime 0.1.1 → 0.2.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 26115554c68d76501a954c09f822ad0d2e06909e157c49eba2a2ab50fa8612cc
4
+ data.tar.gz: 42f74999f738324ff5d34fd9410b2177673abcbc37b6dd9ad84ea9b9bcdf738d
5
+ SHA512:
6
+ metadata.gz: '059ea557563ed3db743a51d81b22128b789e5474083b9155b05562c75fd8fcd4b06afd68d5f0fc3f98dedd122590e7df5df39a811addfa88721e6219fc6a1dbc'
7
+ data.tar.gz: 8eb2e324b51e6342799c9f94c15790e1b875926f1439cdcf79f4c00b6c77d8873b023168078c743f0f2ee675452d96d54120c8efb481e44b009f42380a22455b
data/CHANGELOG.md ADDED
@@ -0,0 +1,28 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [0.1.2] - 2024-03-14
9
+
10
+ ### Added
11
+
12
+ - Packaged this gem as a proper gem using a proper gemspec
13
+ - Added unit tests using minitest
14
+ - LICENSE.txt MIT License file
15
+ - README.md file
16
+
17
+ ### Removed
18
+
19
+ - The ability to multiply and divide -- in future, must be thoughtful about how these functions work if reimplemented
20
+
21
+ ### Changed
22
+
23
+ - HMTime#== only compares two HMTime objects based upon their @minutes variable
24
+ - Released as version 0.1.2
25
+
26
+ ### Fixed
27
+
28
+ - Handle negative time in HMTime#hours, HMTime#minutes, and HMTime#time
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Andrew Dvorak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # HMTime
2
+
3
+ A simple HMTime object for representing and performing simple math on hours and minutes. HHH:MM
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add hmtime
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install hmtime
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require 'hmtime'
19
+
20
+ HMTime.new #=> 0:00
21
+ HMTime.new('') #=> 0:00
22
+ HMTime.new(0) #=> 0:00
23
+ HMTime.new('123:35') #=> 123:35
24
+ HMTime.new(61) #=> 1:01
25
+ HMTime.new(-61) #=> -1:01
26
+ '1:33'.to_hmtime #=> 1:33
27
+ '-1:33'.to_hmtime #=> -1:33
28
+ HMTime.new(61) + 1 #=> 1:02
29
+ HMTime.new('-2:01') + '2:00' #=> -0:01
30
+ HMTime.new(61) == '1:01'.to_hmtime #=> true
31
+ HMTime.new(-61) == '1:01'.to_hmtime #=> false
32
+ ```
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ Bug reports and pull requests are welcome on GitHub at https://github.com/advorak/hmtime.
43
+
44
+ ## License
45
+
46
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,14 +1,8 @@
1
- require 'rubygems'
2
- require 'rake'
3
- require 'echoe'
1
+ # frozen_string_literal: true
4
2
 
5
- Echoe.new('hmtime','0.1.1') 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
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
13
5
 
14
- Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
6
+ Minitest::TestTask.create
7
+
8
+ task default: :test
data/hmtime.gemspec CHANGED
@@ -1,29 +1,39 @@
1
- # -*- encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- Gem::Specification.new do |s|
4
- s.name = "hmtime"
5
- s.version = "0.1.1"
3
+ require_relative "lib/hmtime/version"
4
+ require 'uri'
6
5
 
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-09"
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", "README.rdoc", "lib/hmtime.rb"]
13
- s.files = ["Manifest", "README", "README.rdoc", "Rakefile", "lib/hmtime.rb", "hmtime.gemspec"]
14
- s.homepage = "http://github.com/advorak/hmtime"
15
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Hmtime", "--main", "README"]
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"
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hmtime"
8
+ spec.version = HMTime::VERSION
9
+ spec.authors = ["Andrew Dvorak"]
10
+ spec.email = ["advorak@gmail.com"]
20
11
 
21
- if s.respond_to? :specification_version then
22
- s.specification_version = 3
12
+ spec.summary = "A simple HMTime object for representing and performing simple math on hours and minutes. HHH:MM"
13
+ # spec.description = "TODO: Write a longer description or delete this line."
14
+ spec.homepage = "http://github.com/advorak/hmtime"
15
+ spec.license = "MIT"
16
+ spec.required_ruby_version = ">= 2.6.0"
23
17
 
24
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
- else
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = spec.homepage
20
+ spec.metadata["changelog_uri"] = URI.join(spec.homepage, "CHANGELOG.md").to_s
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
26
28
  end
27
- else
28
29
  end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # Uncomment to register a new dependency of your gem
35
+ # spec.add_dependency "example-gem", "~> 1.0"
36
+
37
+ # For more information and examples about making a new gem, check out our
38
+ # guide at: https://bundler.io/guides/creating_gem.html
29
39
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+ def to_hmtime
5
+ HMTime.new self
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HMTime
4
+ VERSION = "0.2.0"
5
+ end
data/lib/hmtime.rb CHANGED
@@ -1,10 +1,13 @@
1
- class String
2
- def to_hmtime
3
- HMTime.new self
4
- end
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'hmtime/version'
4
+ require_relative 'hmtime/string'
6
5
 
7
6
  class HMTime
7
+ class Error < StandardError; end
8
+ class ArgumentError < Error; end
9
+ class TypeError < Error; end
10
+
8
11
  ERROR_MESSAGE_1 = 'Must specify String in format of: `hhh:mm`, where h=Hours and m=Minutes; or integer in the form of #####'
9
12
 
10
13
  def initialize(value=0)
@@ -15,21 +18,12 @@ class HMTime
15
18
  time
16
19
  end
17
20
 
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
21
  def hours
28
- @minutes.abs / 60
22
+ (@minutes.negative? ? -1 : 1) * (@minutes.abs / 60)
29
23
  end
30
24
 
31
25
  def minutes
32
- @minutes.abs % 60
26
+ (@minutes.negative? ? -1 : 1) * (@minutes.abs % 60)
33
27
  end
34
28
 
35
29
  def to_s
@@ -48,7 +42,6 @@ class HMTime
48
42
  end
49
43
  end
50
44
 
51
- # BUG: Doesn't work if value < 0
52
45
  def -(value)
53
46
  if value.is_a?(self.class)
54
47
  self.class.new(@minutes - value.to_i)
@@ -57,26 +50,25 @@ class HMTime
57
50
  end
58
51
  end
59
52
 
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
53
+ # def /(value)
54
+ # if value.is_a?(self.class)
55
+ # self.class.new(@minutes / value.to_i)
56
+ # else
57
+ # self.class.new(@minutes / check_arguments(value))
58
+ # end
59
+ # end
67
60
 
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
61
+ # # BUG: Doesn't work if value < 0
62
+ # def *(value)
63
+ # if value.is_a?(self.class)
64
+ # self.class.new(@minutes * value.to_i)
65
+ # else
66
+ # self.class.new(@minutes * check_arguments(value))
67
+ # end
68
+ # end
77
69
 
78
70
  def ==(value)
79
- @minutes == value.to_i
71
+ value.is_a?(HMTime) && @minutes == value.to_i
80
72
  end
81
73
 
82
74
  # For Comparison
@@ -96,28 +88,36 @@ class HMTime
96
88
 
97
89
  private
98
90
 
99
- def check_arguments(value)
100
- if(value == nil)
101
- value
91
+ def time
92
+ my_time = [hours.abs, minutes.abs.to_s.rjust(2,"0")].join(":")
93
+ if @minutes < 0
94
+ "-" + my_time
102
95
  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
96
+ my_time
97
+ end
98
+ end
99
+
100
+ def check_arguments(value)
101
+ if(value.is_a?(Integer))
102
+ arguments = ['', value / 60, value % 60]
103
+ elsif (!value.is_a?(String))
104
+ raise TypeError, ERROR_MESSAGE_1
107
105
 
108
- # Is the argument a string of format ("hhh:mm" or " ".strip.empty?)? If not, raise an error.
109
- elsif !value.strip.empty? && !(arguments = value.strip.match(/^(\d+|\s+)?:(\d{2})$/).to_a[1..2])
110
- raise ArgumentError, ERROR_MESSAGE_1
111
- elsif value.strip.empty?
112
- arguments = [0,0]
113
- end
106
+ # Is the argument a string of format ("hhh:mm" or " ".strip.empty?)? If not, raise an error.
107
+ elsif !value.strip.empty? && !(arguments = value.strip.match(/^(-?)(\d+|\s+)?:(\d{2})$/).to_a[1..3])
108
+ raise ArgumentError, ERROR_MESSAGE_1
109
+ elsif value.strip.empty?
110
+ arguments = ['',0,0]
111
+ end
114
112
 
115
- ### How to do a warning?? Is this correct?
116
- if arguments[1].to_i > 59
117
- warn "Warning: The mm (minutes) field of `hhh:mm` should be no greater than 59."
118
- end
119
- hours, minutes = arguments.collect{|arg| arg.to_i}
120
- return (hours * 60) + minutes
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."
121
116
  end
117
+
118
+ is_negative = arguments[0] == '-'
119
+ hours, minutes = arguments[1,2].collect{|arg| arg.to_i}
120
+
121
+ return (is_negative ? -1 : 1) * ((hours * 60) + minutes)
122
122
  end
123
- end
123
+ end
data/sig/hmtime.rbs ADDED
@@ -0,0 +1,4 @@
1
+ class HMTime
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata CHANGED
@@ -1,61 +1,56 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hmtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Andrew Dvorak
9
8
  autorequire:
10
- bindir: bin
9
+ bindir: exe
11
10
  cert_chain: []
12
- date: 2012-04-09 00:00:00.000000000 Z
11
+ date: 2024-03-14 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: A simple HMTime object for representing and performing math on hours
15
- and minutes. HHH:MM
16
- email: advorak@gmail.com
13
+ description:
14
+ email:
15
+ - advorak@gmail.com
17
16
  executables: []
18
17
  extensions: []
19
- extra_rdoc_files:
20
- - README
21
- - README.rdoc
22
- - lib/hmtime.rb
18
+ extra_rdoc_files: []
23
19
  files:
24
- - Manifest
25
- - README
26
- - README.rdoc
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - README.md
27
23
  - Rakefile
28
- - lib/hmtime.rb
29
24
  - hmtime.gemspec
25
+ - lib/hmtime.rb
26
+ - lib/hmtime/string.rb
27
+ - lib/hmtime/version.rb
28
+ - sig/hmtime.rbs
30
29
  homepage: http://github.com/advorak/hmtime
31
- licenses: []
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: http://github.com/advorak/hmtime
34
+ source_code_uri: http://github.com/advorak/hmtime
35
+ changelog_uri: http://github.com/advorak/CHANGELOG.md
32
36
  post_install_message:
33
- rdoc_options:
34
- - --line-numbers
35
- - --inline-source
36
- - --title
37
- - Hmtime
38
- - --main
39
- - README
37
+ rdoc_options: []
40
38
  require_paths:
41
39
  - lib
42
40
  required_ruby_version: !ruby/object:Gem::Requirement
43
- none: false
44
41
  requirements:
45
- - - ! '>='
42
+ - - ">="
46
43
  - !ruby/object:Gem::Version
47
- version: '0'
44
+ version: 2.6.0
48
45
  required_rubygems_version: !ruby/object:Gem::Requirement
49
- none: false
50
46
  requirements:
51
- - - ! '>='
47
+ - - ">="
52
48
  - !ruby/object:Gem::Version
53
- version: '1.2'
49
+ version: '0'
54
50
  requirements: []
55
- rubyforge_project: hmtime
56
- rubygems_version: 1.8.11
51
+ rubygems_version: 3.5.3
57
52
  signing_key:
58
- specification_version: 3
59
- summary: A simple HMTime object for representing and performing math on hours and
60
- minutes. HHH:MM
53
+ specification_version: 4
54
+ summary: A simple HMTime object for representing and performing simple math on hours
55
+ and minutes. HHH:MM
61
56
  test_files: []
data/Manifest DELETED
@@ -1,5 +0,0 @@
1
- Manifest
2
- README
3
- README.rdoc
4
- Rakefile
5
- lib/hmtime.rb
data/README DELETED
File without changes
data/README.rdoc DELETED
File without changes