stamp 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.lock +3 -1
- data/LICENSE.txt +19 -0
- data/README.md +40 -4
- data/cucumber.yml +2 -0
- data/features/stamp.feature +38 -9
- data/lib/stamp.rb +40 -10
- data/lib/stamp/version.rb +1 -1
- data/stamp.gemspec +1 -0
- metadata +18 -3
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
stamp (0.0.
|
4
|
+
stamp (0.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: http://rubygems.org/
|
@@ -17,6 +17,7 @@ GEM
|
|
17
17
|
gherkin (2.4.1)
|
18
18
|
json (>= 1.4.6)
|
19
19
|
json (1.5.3)
|
20
|
+
rake (0.9.2)
|
20
21
|
term-ansicolor (1.0.5)
|
21
22
|
|
22
23
|
PLATFORMS
|
@@ -24,4 +25,5 @@ PLATFORMS
|
|
24
25
|
|
25
26
|
DEPENDENCIES
|
26
27
|
cucumber
|
28
|
+
rake
|
27
29
|
stamp!
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Jeremy Weiskotten
|
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.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# stamp
|
2
2
|
|
3
|
-
Format dates and times based on examples, not arcane strftime directives.
|
3
|
+
Format dates (and times, soon) based on examples, not arcane strftime directives.
|
4
4
|
|
5
5
|
[![Build Status](http://travis-ci.org/jeremyw/stamp.png)](http://travis-ci.org/jeremyw/stamp)
|
6
6
|
|
@@ -10,13 +10,49 @@ Just `gem install stamp`, or add stamp to your Gemfile and `bundle install`.
|
|
10
10
|
|
11
11
|
## Usage
|
12
12
|
|
13
|
+
Date objects get a powerful new method: #stamp. Provide an example date string
|
14
|
+
with whatever month, day, year, and weekday parts you'd like, and your date
|
15
|
+
will be formatted accordingly:
|
16
|
+
|
13
17
|
```ruby
|
14
18
|
date = Date.new(2011, 6, 9)
|
15
|
-
date.stamp("
|
16
|
-
date.stamp("Jan
|
17
|
-
date.stamp("Jan
|
19
|
+
date.stamp("March 1, 1999") # "June 9, 2011"
|
20
|
+
date.stamp("Jan 1, 1999") # "Jun 9, 2011"
|
21
|
+
date.stamp("Jan 01") # "Jun 09"
|
22
|
+
date.stamp("Sunday, May 1, 2000") # "Monday, June 9, 2011"
|
23
|
+
date.stamp("Sun Aug 5") # "Mon Jun 9"
|
24
|
+
date.stamp("01/01/99") # "06/09/11"
|
25
|
+
date.stamp("DOB: 01/01/2000") # "DOB: 06/09/2011"
|
18
26
|
```
|
19
27
|
|
28
|
+
### Features
|
29
|
+
|
30
|
+
* Abbreviated and full names of months and weekdays are recognized.
|
31
|
+
* Days with or without a leading zero work instinctively.
|
32
|
+
* You can use whatever month, weekday, day, or year value makes sense in your
|
33
|
+
examples.
|
34
|
+
* Include any extraneous text you'd like, e.g. "DOB:".
|
35
|
+
|
36
|
+
### Limitations
|
37
|
+
|
38
|
+
* "01/09/99" is assumed to be January 9, not September 1. Patches to make this
|
39
|
+
configurable are welcome. Even better, make it smart enough to disambiguate
|
40
|
+
given an example like 31/01/99, where 31 is obviously not a month.
|
41
|
+
* "01-Jan-1999" doesn't work yet (see @wip cucumber scenario). Patches welcome!
|
42
|
+
* Support for time formatting by example is coming soon. Patches welcome!
|
43
|
+
|
44
|
+
Did I mention? Patches welcome!
|
45
|
+
|
46
|
+
If you need more obscure formatting options, you can include any valid
|
47
|
+
[strftime](http://strfti.me) directives in your example string, and they'll
|
48
|
+
just be passed along:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
date.stamp("Week #%U, %Y") # "Week #23, 2011"
|
52
|
+
````
|
53
|
+
|
54
|
+
[Check out http://strfti.me](http://strfti.me) for more ideas.
|
55
|
+
|
20
56
|
More coming soon, including time formats by example.
|
21
57
|
|
22
58
|
## Contributing to stamp
|
data/cucumber.yml
ADDED
data/features/stamp.feature
CHANGED
@@ -3,18 +3,47 @@ Feature: Stamping a date
|
|
3
3
|
the stamp method
|
4
4
|
formats a date given a human-readable example.
|
5
5
|
|
6
|
-
Scenario Outline:
|
6
|
+
Scenario Outline: Formatting dates by example
|
7
7
|
Given the date December 9, 2011
|
8
8
|
When I stamp the example "<example>"
|
9
9
|
Then I produce "<output>"
|
10
10
|
And I like turtles
|
11
11
|
|
12
12
|
Examples:
|
13
|
-
| example
|
14
|
-
|
|
15
|
-
|
|
16
|
-
| Jan
|
17
|
-
| Jan
|
18
|
-
| Jan
|
19
|
-
| Jan
|
20
|
-
|
|
13
|
+
| example | output |
|
14
|
+
| January | December |
|
15
|
+
| Jan | Dec |
|
16
|
+
| Jan 1 | Dec 9 |
|
17
|
+
| Jan 01 | Dec 09 |
|
18
|
+
| Jan 10 | Dec 09 |
|
19
|
+
| Jan 1, 1999 | Dec 9, 2011 |
|
20
|
+
| Sunday | Friday |
|
21
|
+
| Sun | Fri |
|
22
|
+
| Sun, Jan 1 | Fri, Dec 9 |
|
23
|
+
| Sunday, January 1, 1999 | Friday, December 9, 2011 |
|
24
|
+
| 01/1999 | 12/2011 |
|
25
|
+
| 01/01 | 12/09 |
|
26
|
+
| 01/31 | 12/09 |
|
27
|
+
| 01/01/1999 | 12/09/2011 |
|
28
|
+
| 01/01/99 | 12/09/11 |
|
29
|
+
| DOB: 01-31-1999 | DOB: 12-09-2011 |
|
30
|
+
|
31
|
+
@wip
|
32
|
+
Scenario Outline: Examples that aren't supported quite yet
|
33
|
+
Given the date December 9, 2011
|
34
|
+
When I stamp the example "<example>"
|
35
|
+
Then I produce "<output>"
|
36
|
+
|
37
|
+
Examples:
|
38
|
+
| example | output |
|
39
|
+
| 01-Jan-1999 | 09-Dec-2011 |
|
40
|
+
|
41
|
+
Scenario: strftime directives just get passed through
|
42
|
+
Given the date December 21, 2012
|
43
|
+
When I stamp the example "John Cusack was in a movie about %b %d, %Y, but it wasn't very good."
|
44
|
+
Then I produce "John Cusack was in a movie about Dec 21, 2012, but it wasn't very good."
|
45
|
+
|
46
|
+
Scenario: Plain text just gets passed through
|
47
|
+
Given the date December 9, 2011
|
48
|
+
When I stamp the example "Just some plain old text."
|
49
|
+
Then I produce "Just some plain old text."
|
data/lib/stamp.rb
CHANGED
@@ -13,8 +13,18 @@ module Stamp
|
|
13
13
|
# end
|
14
14
|
|
15
15
|
module InstanceMethods
|
16
|
-
|
16
|
+
|
17
|
+
MONTHNAMES_REGEXP = /(#{Date::MONTHNAMES.compact.join('|')})/i
|
17
18
|
ABBR_MONTHNAMES_REGEXP = /(#{Date::ABBR_MONTHNAMES.compact.join('|')})/i
|
19
|
+
DAYNAMES_REGEXP = /(#{Date::DAYNAMES.join('|')})/i
|
20
|
+
ABBR_DAYNAMES_REGEXP = /(#{Date::ABBR_DAYNAMES.join('|')})/i
|
21
|
+
|
22
|
+
ONE_DIGIT_REGEXP = /\d{1}/
|
23
|
+
TWO_DIGIT_REGEXP = /\d{2}/
|
24
|
+
FOUR_DIGIT_REGEXP = /\d{4}/
|
25
|
+
|
26
|
+
DATE_DELIMITER_REGEXP = /(\/|\-)/ # forward slash or dash
|
27
|
+
|
18
28
|
|
19
29
|
def stamp(example)
|
20
30
|
strftime(strftime_directives(example))
|
@@ -22,31 +32,51 @@ module Stamp
|
|
22
32
|
|
23
33
|
def strftime_directives(example)
|
24
34
|
directives = []
|
35
|
+
previous_directive = nil
|
36
|
+
|
25
37
|
terms = example.split(/\b/)
|
26
38
|
|
27
39
|
terms.each_with_index do |term, index|
|
28
|
-
|
29
|
-
|
40
|
+
directive = strftime_directive(term, previous_directive)
|
41
|
+
directives << (directive || term)
|
30
42
|
|
31
|
-
|
43
|
+
previous_term = term
|
44
|
+
previous_directive = directive unless directive.nil?
|
32
45
|
end
|
33
46
|
|
34
47
|
directives.join
|
35
48
|
end
|
36
49
|
private :strftime_directives
|
37
50
|
|
38
|
-
def strftime_directive(term,
|
51
|
+
def strftime_directive(term, previous_directive=nil)
|
39
52
|
case term
|
40
53
|
when MONTHNAMES_REGEXP
|
41
54
|
'%B'
|
55
|
+
|
42
56
|
when ABBR_MONTHNAMES_REGEXP
|
43
57
|
'%b'
|
44
|
-
|
58
|
+
|
59
|
+
when DAYNAMES_REGEXP
|
60
|
+
'%A'
|
61
|
+
|
62
|
+
when ABBR_DAYNAMES_REGEXP
|
63
|
+
'%a'
|
64
|
+
|
65
|
+
when FOUR_DIGIT_REGEXP
|
45
66
|
'%Y'
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
'%
|
67
|
+
|
68
|
+
when TWO_DIGIT_REGEXP
|
69
|
+
case previous_directive
|
70
|
+
when '%m', '%b', '%B' # a month
|
71
|
+
'%d' # day with leading zero
|
72
|
+
when '%d', '%e' # a day
|
73
|
+
'%y' # two-digit year
|
74
|
+
else
|
75
|
+
'%m' # month
|
76
|
+
end
|
77
|
+
|
78
|
+
when ONE_DIGIT_REGEXP
|
79
|
+
'%e' # day without leading zero
|
50
80
|
end
|
51
81
|
end
|
52
82
|
private :strftime_directive
|
data/lib/stamp/version.rb
CHANGED
data/stamp.gemspec
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Jeremy Weiskotten
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-06-
|
17
|
+
date: 2011-06-30 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -30,6 +30,19 @@ dependencies:
|
|
30
30
|
version: "0"
|
31
31
|
type: :development
|
32
32
|
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :development
|
45
|
+
version_requirements: *id002
|
33
46
|
description: Format dates and times based on examples, not arcane strftime directives.
|
34
47
|
email:
|
35
48
|
- jeremy@weiskotten.com
|
@@ -44,8 +57,10 @@ files:
|
|
44
57
|
- .rvmrc
|
45
58
|
- Gemfile
|
46
59
|
- Gemfile.lock
|
60
|
+
- LICENSE.txt
|
47
61
|
- README.md
|
48
62
|
- Rakefile
|
63
|
+
- cucumber.yml
|
49
64
|
- features/stamp.feature
|
50
65
|
- features/step_definitions/stamp_steps.rb
|
51
66
|
- features/step_definitions/turtle_steps.rb
|