stamp 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,4 +5,3 @@ rvm:
5
5
  - ree
6
6
  - rbx
7
7
  - jruby
8
- - ruby-head
data/Gemfile CHANGED
@@ -1,3 +1,8 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
+
5
+ group :development do
6
+ gem 'cucumber'
7
+ gem 'rake'
8
+ end
@@ -1,19 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stamp (0.2.0)
4
+ stamp (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
- builder (3.0.0)
9
+ builder (3.1.4)
10
10
  cucumber (1.2.1)
11
11
  builder (>= 2.1.2)
12
12
  diff-lcs (>= 1.1.3)
13
13
  gherkin (~> 2.11.0)
14
14
  json (>= 1.4.6)
15
15
  diff-lcs (1.1.3)
16
- gherkin (2.11.2)
16
+ gherkin (2.11.5)
17
17
  json (>= 1.4.6)
18
18
  json (1.7.5)
19
19
  rake (0.9.2.2)
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  Format dates and times based on human-friendly examples, not arcane
4
4
  strftime directives.
5
5
 
6
- [![Build Status](http://travis-ci.org/jeremyw/stamp.png)](http://travis-ci.org/jeremyw/stamp)
6
+ [![Build Status](https://secure.travis-ci.org/jeremyw/stamp.png)](http://travis-ci.org/jeremyw/stamp)
7
7
 
8
8
  ## Installation
9
9
 
@@ -13,6 +13,13 @@ Just `gem install stamp`, or add stamp to your Gemfile and `bundle install`.
13
13
 
14
14
  Your Ruby dates and times get a powerful new method: `stamp`.
15
15
 
16
+ You might be concerned that "stamp" isn't descriptive enough for developers
17
+ reading your code who aren't familiar with this gem. If that's the case, the
18
+ following aliases are available:
19
+
20
+ * `stamp_like`
21
+ * `format_like`
22
+
16
23
  ### Dates
17
24
 
18
25
  Give `Date#stamp` an example date string with whatever month, day, year,
@@ -69,15 +76,6 @@ January 2009. More explicit examples include "12/31", "31/12", and "12/99".
69
76
  Using unambiguous values will also help people who read the code in the
70
77
  future understand your intent.
71
78
 
72
- ### Aliases
73
-
74
- You might be concerned that the method name "stamp" isn't descriptive enough
75
- for developers reading your code who aren't familiar with this gem. If that's
76
- the case, the following aliases are available:
77
-
78
- * `stamp_like`
79
- * `format_like`
80
-
81
79
  ### Rails Integration
82
80
 
83
81
  Stamp makes it easy to configure your application's common date and time
@@ -18,6 +18,8 @@ Feature: Stamping a date
18
18
  | Jan 01 | Sep 08 |
19
19
  | Jan 10 | Sep 08 |
20
20
  | Jan 1, 1999 | Sep 8, 2011 |
21
+ | Jan 12, 1999 | Sep 08, 2011 |
22
+ | 13 January 1999 | 08 September 2011 |
21
23
  | Monday | Thursday |
22
24
  | Tue, Jan 1 | Thu, Sep 8 |
23
25
  | Tuesday, January 1, 1999 | Thursday, September 8, 2011 |
@@ -19,22 +19,24 @@ module Stamp
19
19
  # Disambiguate based on value
20
20
  OBVIOUS_YEARS = 60..99
21
21
  OBVIOUS_MONTHS = 12
22
- OBVIOUS_DAYS = 24..31
22
+ OBVIOUS_DAYS = 13..31
23
23
  OBVIOUS_24_HOUR = 13..23
24
24
 
25
+ OBVIOUS_DATE_MAP = {
26
+ OBVIOUS_YEARS => '%y',
27
+ OBVIOUS_MONTHS => '%m',
28
+ OBVIOUS_DAYS => '%d'
29
+ }
30
+
25
31
  TWO_DIGIT_DATE_SUCCESSION = {
26
- '%m' => '%d',
27
- '%b' => '%d',
28
- '%B' => '%d',
29
- '%d' => '%y',
30
- '%e' => '%y'
32
+ :month => '%d',
33
+ :day => '%y',
34
+ :year => '%m'
31
35
  }
32
36
 
33
37
  TWO_DIGIT_TIME_SUCCESSION = {
34
- '%H' => '%M',
35
- '%I' => '%M',
36
- '%l' => '%M',
37
- '%M' => '%S'
38
+ :hour => '%M',
39
+ :minute => '%S'
38
40
  }
39
41
 
40
42
  def initialize(target_date_or_time)
@@ -60,15 +62,15 @@ module Stamp
60
62
  before, time_example, after = example.partition(TIME_REGEXP)
61
63
 
62
64
  # transform any date tokens to strftime directives
63
- words = strftime_directives(before.split(/\b/)) do |token, previous_directive|
64
- strftime_date_directive(token, previous_directive)
65
+ words = strftime_directives(before.split(/\b/)) do |token, previous_part|
66
+ strftime_date_directive(token, previous_part)
65
67
  end
66
68
 
67
69
  # transform the example time string to strftime directives
68
70
  unless time_example.empty?
69
71
  time_parts = time_example.scan(TIME_REGEXP).first
70
- words += strftime_directives(time_parts) do |token, previous_directive|
71
- strftime_time_directive(token, previous_directive)
72
+ words += strftime_directives(time_parts) do |token, previous_part|
73
+ strftime_time_directive(token, previous_part)
72
74
  end
73
75
  end
74
76
 
@@ -77,17 +79,35 @@ module Stamp
77
79
  words.join
78
80
  end
79
81
 
82
+ # Returns symbolic date part for given strftime directive.
83
+ def date_part(strftime_directive)
84
+ case strftime_directive
85
+ when '%b', '%B', '%m'
86
+ :month
87
+ when '%d', '%e'
88
+ :day
89
+ when '%y', '%Y'
90
+ :year
91
+ when '%H', '%I', '%l'
92
+ :hour
93
+ when '%M'
94
+ :minute
95
+ when '%S'
96
+ :second
97
+ end
98
+ end
99
+
80
100
  # Transforms tokens that look like date/time parts to strftime directives.
81
101
  def strftime_directives(tokens)
82
- previous_directive = nil
102
+ previous_part = nil
83
103
  tokens.map do |token|
84
- directive = yield(token, previous_directive)
85
- previous_directive = directive unless directive.nil?
104
+ directive = yield(token, previous_part)
105
+ previous_part = date_part(directive) unless directive.nil?
86
106
  directive || token
87
107
  end
88
108
  end
89
109
 
90
- def strftime_time_directive(token, previous_directive)
110
+ def strftime_time_directive(token, previous_part)
91
111
  case token
92
112
  when MERIDIAN_LOWER_REGEXP
93
113
  if RUBY_VERSION =~ /^1.8/ && @target.is_a?(Time)
@@ -101,7 +121,7 @@ module Stamp
101
121
  '%p'
102
122
 
103
123
  when TWO_DIGIT_REGEXP
104
- TWO_DIGIT_TIME_SUCCESSION[previous_directive] ||
124
+ TWO_DIGIT_TIME_SUCCESSION[previous_part] ||
105
125
  case token.to_i
106
126
  when OBVIOUS_24_HOUR
107
127
  '%H' # 24-hour clock
@@ -114,7 +134,7 @@ module Stamp
114
134
  end
115
135
  end
116
136
 
117
- def strftime_date_directive(token, previous_directive)
137
+ def strftime_date_directive(token, previous_part)
118
138
  case token
119
139
  when MONTHNAMES_REGEXP
120
140
  '%B'
@@ -135,20 +155,21 @@ module Stamp
135
155
  ordinalize(@target.day)
136
156
 
137
157
  when TWO_DIGIT_REGEXP
138
- # try to discern obvious intent based on the example value
139
- case token.to_i
140
- when OBVIOUS_YEARS
141
- '%y'
142
- when OBVIOUS_MONTHS
143
- '%m'
144
- when OBVIOUS_DAYS
145
- '%d'
146
- else
147
- # the intent isn't obvious based on the example value, so try to
148
- # disambiguate based on context
149
- TWO_DIGIT_DATE_SUCCESSION[previous_directive] || '%m'
158
+ value = token.to_i
159
+
160
+ obvious_mappings =
161
+ OBVIOUS_DATE_MAP.reject { |k,v| date_part(v) == previous_part }
162
+
163
+ obvious_directive = obvious_mappings.find do |range, directive|
164
+ break directive if range === value
150
165
  end
151
166
 
167
+ # if the intent isn't obvious based on the example value, try to
168
+ # disambiguate based on context
169
+ obvious_directive ||
170
+ TWO_DIGIT_DATE_SUCCESSION[previous_part] ||
171
+ '%m'
172
+
152
173
  when ONE_DIGIT_REGEXP
153
174
  '%e' # day without leading zero
154
175
  end
@@ -1,3 +1,3 @@
1
1
  module Stamp
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -12,10 +12,7 @@ Gem::Specification.new do |s|
12
12
  s.description = %Q{Format dates and times based on human-friendly examples, not arcane strftime directives.}
13
13
 
14
14
  s.files = `git ls-files`.split("\n")
15
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.test_files = `git ls-files -- features/*`.split("\n")
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
-
19
- s.add_development_dependency "cucumber"
20
- s.add_development_dependency "rake"
21
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,40 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-01 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: cucumber
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: rake
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
+ dependencies: []
46
14
  description: Format dates and times based on human-friendly examples, not arcane strftime
47
15
  directives.
48
16
  email:
@@ -79,15 +47,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
47
  - - ! '>='
80
48
  - !ruby/object:Gem::Version
81
49
  version: '0'
50
+ segments:
51
+ - 0
52
+ hash: -3229657831275162604
82
53
  required_rubygems_version: !ruby/object:Gem::Requirement
83
54
  none: false
84
55
  requirements:
85
56
  - - ! '>='
86
57
  - !ruby/object:Gem::Version
87
58
  version: '0'
59
+ segments:
60
+ - 0
61
+ hash: -3229657831275162604
88
62
  requirements: []
89
63
  rubyforge_project:
90
- rubygems_version: 1.8.24
64
+ rubygems_version: 1.8.23
91
65
  signing_key:
92
66
  specification_version: 3
93
67
  summary: Date and time formatting for humans.
@@ -95,4 +69,3 @@ test_files:
95
69
  - features/stamp.feature
96
70
  - features/step_definitions/stamp_steps.rb
97
71
  - features/support/env.rb
98
- has_rdoc: