booster-time_parser 0.0.4

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,19 @@
1
+ Copyright (c) 2009 Cameron Cox
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,3 @@
1
+ === TimeParser ===
2
+
3
+ A simple little "plugin" to scan a string for timespans and compute the total time.
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ $:.unshift << File.dirname(__FILE__) + '/lib/'
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'rake/gempackagetask'
5
+ require 'time_parser'
6
+
7
+ gem_spec = Gem::Specification.new do |s|
8
+ s.name = %q{time_parser}
9
+ s.version = TimeParser::VERSION
10
+
11
+ s.specification_version = 2 if s.respond_to? :specification_version=
12
+
13
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
14
+ s.authors = ["Cameron Cox"]
15
+ s.date = %q{2008-04-18}
16
+ s.summary = %q{simple little time parser}
17
+ s.email = %q{cameroncox@gmail.com}
18
+ s.executables = []
19
+ s.extra_rdoc_files = ["README", "LICENSE"]
20
+ s.files = Dir['**/**'].reject{ |f| f =~ /pkg/i }
21
+ s.has_rdoc = false
22
+ s.homepage = %q{http://github.com/booster/time_parser/}
23
+ s.require_paths = ["lib"]
24
+ s.rubygems_version = %q{1.1.1}
25
+ end
26
+
27
+ Rake::GemPackageTask.new(gem_spec) do |p|
28
+ p.gem_spec = gem_spec
29
+ p.need_tar = true
30
+ p.need_zip = true
31
+ end
32
+
33
+ namespace :gem do
34
+ namespace :spec do
35
+ desc "Update gemspec"
36
+ task :generate do
37
+ File.open("time_parser.gemspec", "w") do |f|
38
+ f.puts(gem_spec.to_ruby)
39
+ end
40
+ end
41
+ end
42
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ $:.unshift << File.dirname(__FILE__) + '/lib'
2
+ require 'time_parser'
@@ -0,0 +1,43 @@
1
+ class Float
2
+ def to_seconds
3
+ self
4
+ end
5
+
6
+ def to_minutes
7
+ self / 60.0
8
+ end
9
+
10
+ def to_hours
11
+ self / 3600.0
12
+ end
13
+
14
+ def to_days
15
+ self / 86_400
16
+ end
17
+
18
+ def to_weeks
19
+ self / 604_800
20
+ end
21
+ end
22
+
23
+ class String
24
+ def to_seconds
25
+ TimeParser.parse(self)
26
+ end
27
+
28
+ def to_minutes
29
+ TimeParser.parse(self).to_minutes
30
+ end
31
+
32
+ def to_hours
33
+ TimeParser.parse(self).to_hours
34
+ end
35
+
36
+ def to_days
37
+ TimeParser.parse(self).to_days
38
+ end
39
+
40
+ def to_weeks
41
+ TimeParser.parse(self).to_weeks
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ require 'time_parser/core_ext'
2
+
3
+ module TimeParser
4
+ VERSION = '0.0.4'
5
+
6
+ MULTIPLIERS = [['seconds', 1],
7
+ ['minutes', 60],
8
+ ['hours', 3600],
9
+ ['days', 86_400],
10
+ ['weeks', 604_800]]
11
+
12
+ def self.parse(string)
13
+ string.scan(/([\d.]+)\s+(hour|min|sec|day|week)/).collect {
14
+ |amt, multiplier| multiply(amt, multiplier) }.inject(0.0) { |total, val| total += val; total }
15
+ end
16
+
17
+ protected
18
+ def self.multiply(amount, multiplier)
19
+ multiplier = MULTIPLIERS.select { |x,y| x =~ /#{multiplier}/ }.shift
20
+ (amount.to_f * multiplier[1])
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ $:.unshift << File.dirname(__FILE__) + '/../lib'
2
+ require 'test/unit'
3
+ require 'time_parser'
4
+
5
+ class TimeParserTest < Test::Unit::TestCase
6
+ def test_passing_nothing_returns_zero
7
+ assert_equal 0.0, TimeParser.parse('')
8
+ end
9
+
10
+ def test_adding_an_incorrect_value_and_a_correct_value_returns_a_value
11
+ assert_equal 3600.0, TimeParser.parse('2 coconuts and 1 hour')
12
+ end
13
+
14
+ def test_adding_timespans_returns_correct_amount
15
+ assert_equal 5400.0, TimeParser.parse('1 hour and 30 minutes')
16
+ end
17
+
18
+ def test_two_coconuts_equals_zero_seconds
19
+ assert_equal 0.0, TimeParser.parse('2 coconuts')
20
+ end
21
+
22
+ def test_two_hours_equals_7200_seconds
23
+ assert_equal 7200.0, TimeParser.parse('2 hours')
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: booster-time_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Cameron Cox
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-04-18 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: cameroncox@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ files:
26
+ - init.rb
27
+ - lib
28
+ - lib/time_parser
29
+ - lib/time_parser/core_ext.rb
30
+ - lib/time_parser.rb
31
+ - LICENSE
32
+ - Rakefile
33
+ - README
34
+ - tests
35
+ - tests/time_parser_test.rb
36
+ has_rdoc: false
37
+ homepage: http://github.com/booster/time_parser/
38
+ post_install_message:
39
+ rdoc_options: []
40
+
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: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: simple little time parser
62
+ test_files: []
63
+