timeparser 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,5 +3,7 @@ source "http://rubygems.org"
3
3
  # Specify your gem's dependencies in timeparser.gemspec
4
4
  gemspec
5
5
 
6
- gem 'rspec'
7
- gem 'simplecov'
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'simplecov'
9
+ end
data/Rakefile CHANGED
@@ -5,3 +5,6 @@ desc "Run all examples"
5
5
  RSpec::Core::RakeTask.new(:spec) do |t|
6
6
  t.rspec_opts = %w[--color]
7
7
  end
8
+
9
+
10
+ task :default => :spec
data/Readme.md ADDED
@@ -0,0 +1,56 @@
1
+ Timeparser
2
+ ==========
3
+
4
+ Allows you to parse times from a string using a treetop grammar. It's pretty basic.
5
+
6
+ Example
7
+ -------
8
+
9
+ Timeparser.parse("1h20m").to_i # => 80
10
+ Timeparser.parse("1h20m").hours # => 1
11
+ Timeparser.parse("1h20m").hours(:up) # => 2
12
+ Timeparser.parse("1h40m").hours(:down) # => 1
13
+ Timeparser.parse("1h20m").minutes # => 80
14
+
15
+ Installation
16
+ ------------
17
+
18
+ Before installing this or another Gem I highly recommend using RVM and that you create a .rvmrc file for your project.
19
+
20
+ touch .rvmrc && echo "rvm use 1.9.2-p270@yourproject --create" > .rvmrc
21
+
22
+ Install the gem:
23
+
24
+ gem install timeparser
25
+
26
+ or add it to your Gemfile if you are using Bundler (recommended)
27
+
28
+ gem 'timeparser'
29
+
30
+ and then run
31
+
32
+ bundle
33
+
34
+
35
+ Usage
36
+ -----
37
+
38
+ Timeparser.parse('your time information')
39
+
40
+ will return a Timeparser::Time instance. If you need the accumulated minutes that were parsed out of the string call the #to_i or #minutes methods.
41
+
42
+ The Time instance also knows about hours, which you can access through the #hours method. #hours takes an argument which allows you to round up the hours based on the minutes. Default is rounding down.
43
+
44
+
45
+ Run the specs
46
+ -------------
47
+
48
+ git clone git://github.com/the-architect/timeparser.git
49
+ cd timeparser
50
+ bundle install
51
+ rake
52
+
53
+ You can view the coverage info generated by simplecov by opening the generated files after you ran the tests.
54
+
55
+ open coverage/index.html
56
+
@@ -0,0 +1,19 @@
1
+ require 'polyglot'
2
+ require 'treetop'
3
+ Treetop.load File.join(File.dirname(__FILE__), 'timeparsing.treetop')
4
+
5
+ module Timeparser
6
+ class Parser
7
+ def parser
8
+ @parser ||= TimeparsingParser.new
9
+ end
10
+
11
+ def parse(string)
12
+ if result = parser.parse(string.strip)
13
+ Time.new(result.value)
14
+ else
15
+ Time.new(0)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ module Timeparser
2
+ class Time
3
+ def initialize(minutes)
4
+ @minutes = minutes.to_i
5
+ end
6
+
7
+ def to_i
8
+ minutes
9
+ end
10
+
11
+ def hours(round = nil)
12
+ case round
13
+ when :up
14
+ (minutes.to_f / 60.0).ceil
15
+ else
16
+ (minutes.to_f / 60.0).to_i
17
+ end
18
+ end
19
+
20
+ def minutes
21
+ @minutes
22
+ end
23
+ end
24
+ end
@@ -1,15 +1,31 @@
1
1
  grammar Timeparsing
2
+
2
3
  rule root
3
- (dot_seperated / hours / minutes)*
4
+ (times / whitespace)*
5
+ {
6
+ def value
7
+ elements.inject(0) do |akk, e|
8
+ akk += e.value
9
+ akk
10
+ end
11
+ end
12
+ }
13
+ end
14
+
15
+ rule times
16
+ dot_seperated / hours / minutes / other_stuff
4
17
  {
5
18
  def value
6
- self.elements.inject(0) {|akk, e| akk += e.value; akk }
19
+ elements.inject(0) do |akk, e|
20
+ akk += e.value
21
+ akk
22
+ end
7
23
  end
8
24
  }
9
25
  end
10
26
 
11
27
  rule dot_seperated
12
- [0-9]* ':' [0-9]*
28
+ (([0-9]* ':' [0-9]*) / (hours ':' minutes))
13
29
  {
14
30
  def value
15
31
  elements[0].text_value.to_i*60 + elements[2].text_value.to_i
@@ -18,7 +34,7 @@ grammar Timeparsing
18
34
  end
19
35
 
20
36
  rule hours
21
- ([0-9]* 'h' / 'hours')
37
+ [0-9]+ ('hours' / 'hrs' / 'h')
22
38
  {
23
39
  def value
24
40
  self.text_value.to_i * 60
@@ -27,7 +43,7 @@ grammar Timeparsing
27
43
  end
28
44
 
29
45
  rule minutes
30
- ( [0-9]* "m" / "min" / "minutes" )
46
+ [0-9]+ ("minutes" / "min" / "m")
31
47
  {
32
48
  def value
33
49
  self.text_value.to_i
@@ -35,4 +51,13 @@ grammar Timeparsing
35
51
  }
36
52
  end
37
53
 
54
+ rule whitespace
55
+ " "
56
+ { def value; 0; end }
57
+ end
58
+
59
+ rule other_stuff
60
+ [a-zA-Z]+
61
+ { def value; 0; end }
62
+ end
38
63
  end
@@ -1,3 +1,3 @@
1
1
  module Timeparser
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/timeparser.rb CHANGED
@@ -1,16 +1,15 @@
1
1
  require "timeparser/version"
2
- require 'polyglot'
3
- require 'treetop'
4
- Treetop.load File.join(File.dirname(__FILE__), 'timeparser/timeparsing.treetop')
2
+ require "timeparser/parser"
3
+ require "timeparser/time"
5
4
 
6
5
  module Timeparser
7
- class Parser
6
+ class << self
8
7
  def parser
9
- @parser ||= TimeparsingParser.new
8
+ @parser ||= Parser.new
10
9
  end
11
10
 
12
11
  def parse(string)
13
- parser.parse(string).value
12
+ parser.parse string
14
13
  end
15
14
  end
16
15
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Timeparser::Time do
4
+ it "creates instance" do
5
+ Timeparser::Time.new(0).should_not be_nil
6
+ end
7
+
8
+ it "should not round hours by default" do
9
+ Timeparser::Time.new(130).hours.should eql 2
10
+ end
11
+
12
+ it "should round down" do
13
+ Timeparser::Time.new(130).hours(:down).should eql 2
14
+ end
15
+
16
+ it "should round up" do
17
+ Timeparser::Time.new(130).hours(:up).should eql 3
18
+ end
19
+ end
@@ -5,21 +5,60 @@ describe Timeparser do
5
5
  Timeparser::Parser.new.should_not be_nil
6
6
  end
7
7
 
8
- let(:parser){ Timeparser::Parser.new }
8
+ context "instance methods" do
9
+ let(:parser){ Timeparser::Parser.new }
9
10
 
10
- it "parses hours" do
11
- parser.parse('1h').should eql 60
12
- end
11
+ it "ignores whitespaces" do
12
+ parser.parse(" 1h ").to_i.should eql 60
13
+ end
13
14
 
14
- it "parses minutes" do
15
- parser.parse('1m').should eql 1
16
- end
15
+ it "handles whitespaces in between" do
16
+ parser.parse('1h 20m').to_i.should eql 80
17
+ end
18
+
19
+ it "parses hours" do
20
+ parser.parse('1h').to_i.should eql 60
21
+ parser.parse('2hrs').to_i.should eql 120
22
+ parser.parse('2hours').to_i.should eql 120
23
+ end
24
+
25
+ it "parses minutes" do
26
+ parser.parse('1m').to_i.should eql 1
27
+ parser.parse('20min').to_i.should eql 20
28
+ parser.parse('120minutes').to_i.should eql 120
29
+ end
30
+
31
+ it "parses hours and minutes" do
32
+ parser.parse('1h20m').to_i.should eql 80
33
+ end
34
+
35
+ it "parses colon notation" do
36
+ parser.parse('1:20').to_i.should eql 80
37
+ end
17
38
 
18
- it "parses hours and minutes" do
19
- parser.parse('1h20m').should eql 80
39
+ it "parses enhanced colon notation" do
40
+ parser.parse('1h:20m').to_i.should eql 80
41
+ end
42
+
43
+ it "parses empty string" do
44
+ parser.parse('').to_i.should eql 0
45
+ end
46
+
47
+ it "parses times within a text" do
48
+ parser.parse(%Q{The movie was 1h long but the truck was 20m long.}).to_i.should eql 0
49
+ end
20
50
  end
21
51
 
22
- it "parses dot notation" do
23
- parser.parse('1:20').should eql 80
52
+
53
+ context "module methods" do
54
+ it "has instance of parser" do
55
+ Timeparser.parser.should be_instance_of(Timeparser::Parser)
56
+ end
57
+
58
+ it "should delegate to parser instance" do
59
+ Timeparser.parser.should_receive(:parse).with('1:20')
60
+ Timeparser.parse('1:20')
61
+ end
24
62
  end
63
+
25
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timeparser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: treetop
16
- requirement: &70191347829680 !ruby/object:Gem::Requirement
16
+ requirement: &70111894166920 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70191347829680
24
+ version_requirements: *70111894166920
25
25
  description: Parses time information from strings.
26
26
  email:
27
27
  - marcel.scherf@epicteams.com
@@ -33,9 +33,13 @@ files:
33
33
  - .rvmrc
34
34
  - Gemfile
35
35
  - Rakefile
36
+ - Readme.md
36
37
  - lib/timeparser.rb
38
+ - lib/timeparser/parser.rb
39
+ - lib/timeparser/time.rb
37
40
  - lib/timeparser/timeparsing.treetop
38
41
  - lib/timeparser/version.rb
42
+ - spec/lib/time_spec.rb
39
43
  - spec/lib/timeparser_spec.rb
40
44
  - spec/spec_helper.rb
41
45
  - timeparser.gemspec
@@ -64,5 +68,6 @@ signing_key:
64
68
  specification_version: 3
65
69
  summary: Parses time information from strings.
66
70
  test_files:
71
+ - spec/lib/time_spec.rb
67
72
  - spec/lib/timeparser_spec.rb
68
73
  - spec/spec_helper.rb