spanner-lfittl 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.rdoc +11 -0
- data/VERSION +1 -1
- data/lib/spanner.rb +10 -6
- data/spec/spanner_spec.rb +9 -0
- metadata +62 -29
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
|
3
3
|
Easy way to parse natural language time spans as periods expressed in seconds. Supports float point notions of spans as well.
|
4
4
|
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install spanner-lfittl
|
8
|
+
|
9
|
+
or add this to your Gemfile:
|
10
|
+
|
11
|
+
gem 'spanner-lfittl', :require => 'spanner'
|
12
|
+
|
5
13
|
== Usage
|
6
14
|
|
7
15
|
require 'spanner'
|
@@ -15,6 +23,9 @@ Easy way to parse natural language time spans as periods expressed in seconds. S
|
|
15
23
|
Spanner.format(83520)
|
16
24
|
=> '23 hours 12 minutes'
|
17
25
|
|
26
|
+
Spanner.format(83520, :biggest_unit => :minutes)
|
27
|
+
=> '1392 minutes'
|
28
|
+
|
18
29
|
== Authors
|
19
30
|
|
20
31
|
Original author:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/lib/spanner.rb
CHANGED
@@ -19,6 +19,7 @@ class Spanner
|
|
19
19
|
@value = value
|
20
20
|
@on_error = opts && opts.key?(:on_error) ? opts[:on_error] : :raise
|
21
21
|
@length_of_month = opts && opts[:length_of_month]
|
22
|
+
@biggest_unit = ((opts && opts[:biggest_unit]) || "years").to_s.downcase.to_sym
|
22
23
|
|
23
24
|
@from = if opts && opts.key?(:from)
|
24
25
|
case opts[:from]
|
@@ -98,13 +99,16 @@ class Spanner
|
|
98
99
|
def format(distance)
|
99
100
|
distance = distance.to_i
|
100
101
|
|
102
|
+
units = [:years, :months, :weeks, :days, :hours, :minutes]
|
103
|
+
biggest_unit = units.index(@biggest_unit) || 0
|
104
|
+
|
101
105
|
parts = {}
|
102
|
-
parts[:years], distance = distance.divmod(31_556_926)
|
103
|
-
parts[:months], distance = distance.divmod(length_of_month)
|
104
|
-
parts[:weeks], distance = distance.divmod(604_800)
|
105
|
-
parts[:days], distance = distance.divmod(86_400)
|
106
|
-
parts[:hours], distance = distance.divmod(3600)
|
107
|
-
parts[:minutes], parts[:seconds] = distance.divmod(60)
|
106
|
+
parts[:years], distance = distance.divmod(31_556_926) unless biggest_unit > units.index(:years)
|
107
|
+
parts[:months], distance = distance.divmod(length_of_month) unless biggest_unit > units.index(:months)
|
108
|
+
parts[:weeks], distance = distance.divmod(604_800) unless biggest_unit > units.index(:weeks)
|
109
|
+
parts[:days], distance = distance.divmod(86_400) unless biggest_unit > units.index(:days)
|
110
|
+
parts[:hours], distance = distance.divmod(3600) unless biggest_unit > units.index(:hours)
|
111
|
+
parts[:minutes], parts[:seconds] = distance.divmod(60) unless biggest_unit > units.index(:minutes)
|
108
112
|
|
109
113
|
output = []
|
110
114
|
parts.each do |name, value|
|
data/spec/spanner_spec.rb
CHANGED
@@ -55,4 +55,13 @@ describe Spanner, "format" do
|
|
55
55
|
Spanner.format(input).should == output
|
56
56
|
end
|
57
57
|
end
|
58
|
+
|
59
|
+
it "should be possible to restrict the biggest unit" do
|
60
|
+
Spanner.format(3600*24*50).should == "1 month 2 weeks 5 days"
|
61
|
+
Spanner.format(3600*24*50, :biggest_unit => :days).should == "50 days"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should default to no restriction if invalid biggest unit is supplied" do
|
65
|
+
Spanner.format(3600*24*14, :biggest_unit => "LALA").should == "2 weeks"
|
66
|
+
end
|
58
67
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spanner-lfittl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ date: 2012-08-07 00:00:00.000000000Z
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: spanner-lfittl
|
17
|
-
requirement: &
|
17
|
+
requirement: &2156930980 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ! '>='
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *2156930980
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: jeweler
|
28
|
-
requirement: &
|
28
|
+
requirement: &2156930480 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ! '>='
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2156930480
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: rake
|
39
|
-
requirement: &
|
39
|
+
requirement: &2156930000 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -44,10 +44,10 @@ dependencies:
|
|
44
44
|
version: '0'
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *2156930000
|
48
48
|
- !ruby/object:Gem::Dependency
|
49
49
|
name: rspec
|
50
|
-
requirement: &
|
50
|
+
requirement: &2156929520 !ruby/object:Gem::Requirement
|
51
51
|
none: false
|
52
52
|
requirements:
|
53
53
|
- - ! '>='
|
@@ -55,10 +55,10 @@ dependencies:
|
|
55
55
|
version: '0'
|
56
56
|
type: :development
|
57
57
|
prerelease: false
|
58
|
-
version_requirements: *
|
58
|
+
version_requirements: *2156929520
|
59
59
|
- !ruby/object:Gem::Dependency
|
60
60
|
name: jeweler
|
61
|
-
requirement: &
|
61
|
+
requirement: &2156929040 !ruby/object:Gem::Requirement
|
62
62
|
none: false
|
63
63
|
requirements:
|
64
64
|
- - ! '>='
|
@@ -66,10 +66,10 @@ dependencies:
|
|
66
66
|
version: '0'
|
67
67
|
type: :development
|
68
68
|
prerelease: false
|
69
|
-
version_requirements: *
|
69
|
+
version_requirements: *2156929040
|
70
70
|
- !ruby/object:Gem::Dependency
|
71
71
|
name: rake
|
72
|
-
requirement: &
|
72
|
+
requirement: &2156928540 !ruby/object:Gem::Requirement
|
73
73
|
none: false
|
74
74
|
requirements:
|
75
75
|
- - ! '>='
|
@@ -77,10 +77,10 @@ dependencies:
|
|
77
77
|
version: '0'
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
|
-
version_requirements: *
|
80
|
+
version_requirements: *2156928540
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
82
|
name: rspec
|
83
|
-
requirement: &
|
83
|
+
requirement: &2156847620 !ruby/object:Gem::Requirement
|
84
84
|
none: false
|
85
85
|
requirements:
|
86
86
|
- - ! '>='
|
@@ -88,10 +88,10 @@ dependencies:
|
|
88
88
|
version: '0'
|
89
89
|
type: :development
|
90
90
|
prerelease: false
|
91
|
-
version_requirements: *
|
91
|
+
version_requirements: *2156847620
|
92
92
|
- !ruby/object:Gem::Dependency
|
93
93
|
name: jeweler
|
94
|
-
requirement: &
|
94
|
+
requirement: &2156847020 !ruby/object:Gem::Requirement
|
95
95
|
none: false
|
96
96
|
requirements:
|
97
97
|
- - ! '>='
|
@@ -99,10 +99,10 @@ dependencies:
|
|
99
99
|
version: '0'
|
100
100
|
type: :development
|
101
101
|
prerelease: false
|
102
|
-
version_requirements: *
|
102
|
+
version_requirements: *2156847020
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
104
|
name: rake
|
105
|
-
requirement: &
|
105
|
+
requirement: &2156846420 !ruby/object:Gem::Requirement
|
106
106
|
none: false
|
107
107
|
requirements:
|
108
108
|
- - ! '>='
|
@@ -110,10 +110,10 @@ dependencies:
|
|
110
110
|
version: '0'
|
111
111
|
type: :development
|
112
112
|
prerelease: false
|
113
|
-
version_requirements: *
|
113
|
+
version_requirements: *2156846420
|
114
114
|
- !ruby/object:Gem::Dependency
|
115
115
|
name: rspec
|
116
|
-
requirement: &
|
116
|
+
requirement: &2156845820 !ruby/object:Gem::Requirement
|
117
117
|
none: false
|
118
118
|
requirements:
|
119
119
|
- - ! '>='
|
@@ -121,10 +121,43 @@ dependencies:
|
|
121
121
|
version: '0'
|
122
122
|
type: :development
|
123
123
|
prerelease: false
|
124
|
-
version_requirements: *
|
124
|
+
version_requirements: *2156845820
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: jeweler
|
127
|
+
requirement: &2156845220 !ruby/object:Gem::Requirement
|
128
|
+
none: false
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: *2156845220
|
136
|
+
- !ruby/object:Gem::Dependency
|
137
|
+
name: rake
|
138
|
+
requirement: &2156844640 !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ! '>='
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: '0'
|
144
|
+
type: :development
|
145
|
+
prerelease: false
|
146
|
+
version_requirements: *2156844640
|
147
|
+
- !ruby/object:Gem::Dependency
|
148
|
+
name: rspec
|
149
|
+
requirement: &2156844040 !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ! '>='
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
type: :development
|
156
|
+
prerelease: false
|
157
|
+
version_requirements: *2156844040
|
125
158
|
- !ruby/object:Gem::Dependency
|
126
159
|
name: activesupport
|
127
|
-
requirement: &
|
160
|
+
requirement: &2156843500 !ruby/object:Gem::Requirement
|
128
161
|
none: false
|
129
162
|
requirements:
|
130
163
|
- - ! '>='
|
@@ -132,10 +165,10 @@ dependencies:
|
|
132
165
|
version: '0'
|
133
166
|
type: :runtime
|
134
167
|
prerelease: false
|
135
|
-
version_requirements: *
|
168
|
+
version_requirements: *2156843500
|
136
169
|
- !ruby/object:Gem::Dependency
|
137
170
|
name: jeweler
|
138
|
-
requirement: &
|
171
|
+
requirement: &2156842980 !ruby/object:Gem::Requirement
|
139
172
|
none: false
|
140
173
|
requirements:
|
141
174
|
- - ! '>='
|
@@ -143,10 +176,10 @@ dependencies:
|
|
143
176
|
version: '0'
|
144
177
|
type: :development
|
145
178
|
prerelease: false
|
146
|
-
version_requirements: *
|
179
|
+
version_requirements: *2156842980
|
147
180
|
- !ruby/object:Gem::Dependency
|
148
181
|
name: rake
|
149
|
-
requirement: &
|
182
|
+
requirement: &2156842380 !ruby/object:Gem::Requirement
|
150
183
|
none: false
|
151
184
|
requirements:
|
152
185
|
- - ! '>='
|
@@ -154,10 +187,10 @@ dependencies:
|
|
154
187
|
version: '0'
|
155
188
|
type: :development
|
156
189
|
prerelease: false
|
157
|
-
version_requirements: *
|
190
|
+
version_requirements: *2156842380
|
158
191
|
- !ruby/object:Gem::Dependency
|
159
192
|
name: rspec
|
160
|
-
requirement: &
|
193
|
+
requirement: &2156841800 !ruby/object:Gem::Requirement
|
161
194
|
none: false
|
162
195
|
requirements:
|
163
196
|
- - ! '>='
|
@@ -165,7 +198,7 @@ dependencies:
|
|
165
198
|
version: '0'
|
166
199
|
type: :development
|
167
200
|
prerelease: false
|
168
|
-
version_requirements: *
|
201
|
+
version_requirements: *2156841800
|
169
202
|
description: Natural language time span parsing & formatting
|
170
203
|
email: lukas@fittl.com
|
171
204
|
executables: []
|