hours 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 presskey
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.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ = Hours
2
+
3
+ This gem calculates time difference within 24 hour limit.
4
+
5
+ == Install
6
+
7
+ gem install hours
8
+
9
+ == Usage
10
+
11
+ '23:00'.hr - '21:30'.hr # => 1.5
12
+ '02:15'.hr - '22:00'.hr # => 4.25
13
+
14
+ # mixin:
15
+ include Hours
16
+ hr('22:00', '23:00') # => 1.0
17
+ hr('22:00'..'23:00') # => 1.0
18
+ hr('22:00' => '22:30', '23:00' => '00:00') # => 1.5
19
+
20
+ # or module method:
21
+ Hours::hr('22:00', '23:00') # => 1.0
22
+
data/hours.gemspec ADDED
@@ -0,0 +1,16 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'hours/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'hours'
7
+ s.version = Hours::VERSION.dup
8
+ s.summary = 'Handy time difference calculation.'
9
+ s.description = 'Calculates time difference within 24 hour limit.'
10
+ s.author = 'presskey'
11
+ s.email = 'hello.from.code@gmail.com'
12
+ s.homepage = 'http://github.com/presskey/hours'
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.require_paths = ["lib"]
16
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def hr
3
+ Hours::Time.new(self)
4
+ end
5
+ end
data/lib/hours.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'hours/time'
2
+ require 'core_ext/string'
data/lib/hours/time.rb ADDED
@@ -0,0 +1,39 @@
1
+ module Hours
2
+ class Time
3
+
4
+ attr_accessor :hours, :minutes
5
+
6
+ def initialize(str_time)
7
+ if (str_time =~ /(\d{1,2}):(\d{2})/)
8
+ self.hours = $1.to_i
9
+ self.minutes = $2.to_i
10
+ else
11
+ raise ArgumentError, 'argument must be in format "hh:mm"'
12
+ end
13
+ end
14
+
15
+ def -(subtrahend)
16
+ result = self.to_hours - subtrahend.to_hours
17
+ (result < 0) ? (24 + result) : result
18
+ end
19
+
20
+ def to_hours
21
+ hours + minutes / 60.0
22
+ end
23
+ end
24
+
25
+ def self.hr(*args)
26
+ if args.size == 2
27
+ Hours::Time.new(args[1]) - Hours::Time.new(args[0])
28
+ elsif args.size == 1
29
+ return self.hr(args[0].begin, args[0].end) if args[0].kind_of?(Range)
30
+ return (args[0].inject(0) {|r,e| r + self.hr(e.first, e.last)}) if args[0].kind_of?(Hash)
31
+ else
32
+ raise ArgumentError, 'incorrect argument type'
33
+ end
34
+ end
35
+
36
+ def hr(*args)
37
+ Hours::hr(*args)
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Hours
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hours
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - presskey
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-11-17 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Calculates time difference within 24 hour limit.
22
+ email: hello.from.code@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - LICENSE
31
+ - README.rdoc
32
+ - hours.gemspec
33
+ - lib/core_ext/string.rb
34
+ - lib/hours.rb
35
+ - lib/hours/time.rb
36
+ - lib/hours/version.rb
37
+ has_rdoc: true
38
+ homepage: http://github.com/presskey/hours
39
+ licenses: []
40
+
41
+ post_install_message:
42
+ rdoc_options: []
43
+
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.6
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Handy time difference calculation.
67
+ test_files: []
68
+