booster-time_parser 0.0.4 → 0.0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,6 +2,7 @@ $:.unshift << File.dirname(__FILE__) + '/lib/'
2
2
  require 'rubygems'
3
3
  require 'rake'
4
4
  require 'rake/gempackagetask'
5
+ require 'rake/testtask'
5
6
  require 'time_parser'
6
7
 
7
8
  gem_spec = Gem::Specification.new do |s|
@@ -30,6 +31,13 @@ Rake::GemPackageTask.new(gem_spec) do |p|
30
31
  p.need_zip = true
31
32
  end
32
33
 
34
+ Rake::TestTask.new do |t|
35
+ t.test_files = FileList["tests/**/*_test.rb"]
36
+ end
37
+
38
+ desc "Default Task"
39
+ task :default => :test
40
+
33
41
  namespace :gem do
34
42
  namespace :spec do
35
43
  desc "Update gemspec"
data/lib/time_parser.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'time_parser/core_ext'
2
2
 
3
3
  module TimeParser
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5.1'
5
5
 
6
6
  MULTIPLIERS = [['seconds', 1],
7
7
  ['minutes', 60],
@@ -9,7 +9,28 @@ module TimeParser
9
9
  ['days', 86_400],
10
10
  ['weeks', 604_800]]
11
11
 
12
+ def self.to_seconds(string = nil)
13
+ parse(string)
14
+ end
15
+
16
+ def self.to_minutes(string = nil)
17
+ parse(string).to_minutes
18
+ end
19
+
20
+ def self.to_hours(string = nil)
21
+ parse(string).to_hours
22
+ end
23
+
24
+ def self.to_days(string = nil)
25
+ parse(string).to_hours
26
+ end
27
+
28
+ def self.to_weeks(string = nil)
29
+ parse(string).to_weeks
30
+ end
31
+
12
32
  def self.parse(string)
33
+ string ||= ''
13
34
  string.scan(/([\d.]+)\s+(hour|min|sec|day|week)/).collect {
14
35
  |amt, multiplier| multiply(amt, multiplier) }.inject(0.0) { |total, val| total += val; total }
15
36
  end
@@ -19,4 +40,4 @@ module TimeParser
19
40
  multiplier = MULTIPLIERS.select { |x,y| x =~ /#{multiplier}/ }.shift
20
41
  (amount.to_f * multiplier[1])
21
42
  end
22
- end
43
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift << File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ gem 'jeremymcanally-matchy'
6
+ require 'matchy'
7
+ gem 'jeremymcanally-context'
8
+ require 'context'
9
+ require 'time_parser'
@@ -1,25 +1,69 @@
1
- $:.unshift << File.dirname(__FILE__) + '/../lib'
2
- require 'test/unit'
3
- require 'time_parser'
1
+ require File.dirname(__FILE__) + '/test_helper'
4
2
 
5
3
  class TimeParserTest < Test::Unit::TestCase
6
- def test_passing_nothing_returns_zero
7
- assert_equal 0.0, TimeParser.parse('')
4
+ context "TimeParser Module" do
5
+ test "respond to #to_seconds" do
6
+ TimeParser.respond_to?(:to_seconds) == true
7
+ end
8
+
9
+ test "respond to #to_minutes" do
10
+ TimeParser.respond_to?(:to_minutes) == true
11
+ end
12
+
13
+ test "respond to #to_hours" do
14
+ TimeParser.respond_to?(:to_hours) == true
15
+ end
16
+
17
+ test "respond to #to_days" do
18
+ TimeParser.respond_to?(:to_days) == true
19
+ end
20
+
21
+ test "respond to #to_weeks" do
22
+ TimeParser.respond_to?(:to_weeks) == true
23
+ end
8
24
  end
9
25
 
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
26
+ context "Invalid/Blank Data" do
27
+ test "invalid should return zero seconds as a float" do
28
+ TimeParser.to_seconds('2 coconuts') == 0.0
29
+ end
13
30
 
14
- def test_adding_timespans_returns_correct_amount
15
- assert_equal 5400.0, TimeParser.parse('1 hour and 30 minutes')
16
- end
31
+ test "mix of invalid and valid input should return 3600 seconds as a float" do
32
+ TimeParser.to_seconds('1 hour and 30 coconuts')
33
+ end
17
34
 
18
- def test_two_coconuts_equals_zero_seconds
19
- assert_equal 0.0, TimeParser.parse('2 coconuts')
35
+ test "no input should return zero seconds as a float" do
36
+ TimeParser.to_seconds() == 0.0
37
+ end
20
38
  end
21
39
 
22
- def test_two_hours_equals_7200_seconds
23
- assert_equal 7200.0, TimeParser.parse('2 hours')
40
+ context "Conversions" do
41
+ test "'2 hours' should equal '120.0'" do
42
+ TimeParser.to_minutes('2 hours') == 120.0
43
+ end
44
+
45
+ test "'2 hours' should equal '0.08'" do
46
+ TimeParser.to_days('2 hours') == 0.08
47
+ end
48
+
49
+ test "'2 hours' should equal '0.01'" do
50
+ TimeParser.to_weeks('2 hours') == 0.01
51
+ end
52
+
53
+ test "'2 hours' should equal '3600.0'" do
54
+ TimeParser.to_seconds('2 hours') == 3600.0
55
+ end
56
+
57
+ test "'2 hours' should equal '2.0'" do
58
+ TimeParser.to_hours('2 hours') == 2.0
59
+ end
60
+
61
+ test "'1 hour and 30 minutes' should equal '5400.0'" do
62
+ TimeParser.to_seconds('1 hour and 30 minutes') == 5400.0
63
+ end
64
+
65
+ test "'1 hour and 30 minutes' should equal '1.5'" do
66
+ TimeParser.to_hours('1 hour and 30 minutes') == 1.5
67
+ end
24
68
  end
25
69
  end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{time_parser}
5
+ s.version = "0.0.5.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Cameron Cox"]
9
+ s.date = %q{2008-04-18}
10
+ s.email = %q{cameroncox@gmail.com}
11
+ s.extra_rdoc_files = ["README", "LICENSE"]
12
+ s.files = ["init.rb", "lib", "lib/time_parser", "lib/time_parser/core_ext.rb", "lib/time_parser.rb", "LICENSE", "Rakefile", "README", "tests", "tests/test_helper.rb", "tests/time_parser_test.rb", "time_parser.gemspec"]
13
+ s.homepage = %q{http://github.com/booster/time_parser/}
14
+ s.require_paths = ["lib"]
15
+ s.rubygems_version = %q{1.3.1}
16
+ s.summary = %q{simple little time parser}
17
+
18
+ if s.respond_to? :specification_version then
19
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
20
+ s.specification_version = 2
21
+
22
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
23
+ else
24
+ end
25
+ else
26
+ end
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: booster-time_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Cox
@@ -32,7 +32,9 @@ files:
32
32
  - Rakefile
33
33
  - README
34
34
  - tests
35
+ - tests/test_helper.rb
35
36
  - tests/time_parser_test.rb
37
+ - time_parser.gemspec
36
38
  has_rdoc: false
37
39
  homepage: http://github.com/booster/time_parser/
38
40
  post_install_message: