spanner 0.0.1 → 0.0.2

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.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/spanner.rb +16 -6
  3. data/spec/spanner_spec.rb +5 -1
  4. metadata +12 -5
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -1,17 +1,19 @@
1
+ require 'date'
2
+
1
3
  class Spanner
2
4
 
3
5
  ParseError = Class.new(RuntimeError)
4
6
 
5
7
  def self.parse(str, opts = nil)
6
- Spanner.new(str, opts).parse
8
+ Spanner.new(opts).parse(str)
7
9
  end
8
10
 
9
11
  attr_reader :value, :raise_on_error, :from
10
12
 
11
- def initialize(value, opts)
13
+ def initialize(opts)
12
14
  @value = value
13
15
  @on_error = opts && opts.key?(:on_error) ? opts[:on_error] : :raise
14
-
16
+ @length_of_month = opts && opts[:length_of_month]
15
17
 
16
18
  @from = if opts && opts.key?(:from)
17
19
  case opts[:from]
@@ -25,13 +27,21 @@ class Spanner
25
27
  end
26
28
  end
27
29
 
30
+ def self.days_in_month(year, month)
31
+ (Date.new(year, 12, 31) << (12-month)).day
32
+ end
33
+
34
+ def length_of_month
35
+ @length_of_month ||= Spanner.parse("#{Spanner.days_in_month(Time.new.year, Time.new.month)} days")
36
+ end
37
+
28
38
  def error(err)
29
39
  if on_error == :raise
30
40
  raise ParseError.new(err)
31
41
  end
32
42
  end
33
43
 
34
- def parse
44
+ def parse(value)
35
45
  parts = []
36
46
  part_contextualized = nil
37
47
  value.scan(/[\+\-]?(?:\d*\.\d+|\d+)|[a-z]+/i).each do |part|
@@ -56,13 +66,13 @@ class Spanner
56
66
  when 'm', 'min', 'minutes' then 60
57
67
  when 'd', 'days' then 86_400
58
68
  when 'w', 'wks', 'weeks' then 604_800
59
- when 'months', 'month', 'm' then 2_629_743.83
69
+ when 'months', 'month', 'M' then length_of_month
60
70
  when 'years', 'y' then 31_556_926
61
71
  when /\As/ then 1
62
72
  when /\Am/ then 60
63
73
  when /\Ah/ then 86_400
64
74
  when /\Aw/ then 604_800
65
- when /\Am/ then 2_629_743.83
75
+ when /\AM/ then length_of_month
66
76
  when /\Ay/ then 31_556_926
67
77
  end
68
78
 
@@ -11,7 +11,7 @@ describe Spanner do
11
11
  end
12
12
 
13
13
  #simple
14
- { '.5s' => 0.5, '1s' => 1, '1.5s' => 1.5, '1m' => 60, '1.5m' => 90, '1d' => 86400, '1.7233312d' => 148895.81568 }.each do |input, output|
14
+ { '.5s' => 0.5, '1s' => 1, '1.5s' => 1.5, '1m' => 60, '1.5m' => 90, '1d' => 86400, '1.7233312d' => 148895.81568, '1M' => Spanner.days_in_month(Time.new.year, Time.new.month) * 24 * 60 * 60 }.each do |input, output|
15
15
  it "should parse #{input} and return #{output}" do
16
16
  Spanner.parse(input).should == output
17
17
  end
@@ -24,6 +24,10 @@ describe Spanner do
24
24
  end
25
25
  end
26
26
 
27
+ it "should let you set the length of a month" do
28
+ Spanner.parse("4 months", :length_of_month => 1234).should == 4936
29
+ end
30
+
27
31
  it "should accept time as from option" do
28
32
  now = Time.new
29
33
  Spanner.parse('23s', :from => now).should == now.to_i + 23
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spanner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
5
10
  platform: ruby
6
11
  authors:
7
12
  - Joshua Hull
@@ -9,7 +14,7 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-16 00:00:00 -04:00
17
+ date: 2010-05-03 00:00:00 -04:00
13
18
  default_executable:
14
19
  dependencies: []
15
20
 
@@ -42,18 +47,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
42
47
  requirements:
43
48
  - - ">="
44
49
  - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
45
52
  version: "0"
46
- version:
47
53
  required_rubygems_version: !ruby/object:Gem::Requirement
48
54
  requirements:
49
55
  - - ">="
50
56
  - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
51
59
  version: "0"
52
- version:
53
60
  requirements: []
54
61
 
55
62
  rubyforge_project:
56
- rubygems_version: 1.3.5
63
+ rubygems_version: 1.3.6
57
64
  signing_key:
58
65
  specification_version: 3
59
66
  summary: Natural language time span parsing