dater 0.2.3 → 0.2.4
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.
- data/Gemfile.lock +1 -1
- data/dater.gemspec +1 -1
- data/lib/dater/version.rb +1 -1
- data/lib/dater.rb +107 -23
- metadata +3 -3
data/Gemfile.lock
CHANGED
data/dater.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["roman.g.rodriguez@gmail.com"]
|
11
11
|
spec.description = %q{This gem aims to help you with dates treatment, specially in regression test where you have to use future dates}
|
12
12
|
spec.summary = %q{Converts periods of time expresed in literal mode to a formatted date from the date of today}
|
13
|
-
spec.homepage = "http://
|
13
|
+
spec.homepage = "http://romantestdev.github.io/dater/"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/lib/dater/version.rb
CHANGED
data/lib/dater.rb
CHANGED
@@ -20,8 +20,8 @@ module Dater
|
|
20
20
|
|
21
21
|
# Creates a Dater::Resolver object
|
22
22
|
#
|
23
|
-
#
|
24
|
-
#
|
23
|
+
# @param [String] format = date format
|
24
|
+
# @param [String] lang = languaje for matching (en=english, es=spanish, pt=portuguese)
|
25
25
|
def initialize(format='%Y-%m-%d', lang="en")
|
26
26
|
@format=format
|
27
27
|
@lang=lang if ["en","es","pt"].include? lang
|
@@ -29,8 +29,8 @@ module Dater
|
|
29
29
|
|
30
30
|
# Convert the period of time passed as argument to the configured format
|
31
31
|
#
|
32
|
-
#
|
33
|
-
#
|
32
|
+
# @param [String] period = a period of time like "in 3 days" or "in 10 months" or "in 2 years". It could be a formatted date to convert to the wanted format
|
33
|
+
# @return [String] converted date to the configured format. If period is nil, returns date for tomorrow
|
34
34
|
def for(period=nil)
|
35
35
|
return (Time.now).strftime(@format) if period.nil?
|
36
36
|
@date=case period.downcase
|
@@ -51,91 +51,175 @@ module Dater
|
|
51
51
|
return @date.strftime(@format)
|
52
52
|
end
|
53
53
|
|
54
|
+
# Spanish for 'for' method
|
55
|
+
#
|
56
|
+
#
|
54
57
|
def para(period)
|
55
58
|
self.for(period)
|
56
59
|
end
|
57
60
|
|
61
|
+
# Returns the time for today
|
62
|
+
#
|
63
|
+
# @param [Boolean] formatted = indicates if has to return today time in configured format
|
64
|
+
# @return [Time] today's time (formatted or not)
|
58
65
|
def today(formatted=true)
|
59
66
|
time=Time.now
|
60
67
|
time=time.strftime(@format) if formatted
|
61
68
|
time
|
62
69
|
end
|
63
70
|
|
71
|
+
# Spanish for today method
|
64
72
|
def hoy
|
65
73
|
self.today(true)
|
66
74
|
end
|
67
75
|
|
76
|
+
# Portuguese for today method
|
77
|
+
#
|
78
|
+
#
|
68
79
|
def hoje
|
69
80
|
self.today(true)
|
70
81
|
end
|
71
82
|
|
83
|
+
# Returns the time for yesterday.
|
84
|
+
#
|
85
|
+
# @param [Boolean] formatted = indicates if has to return the value in configured format
|
86
|
+
# @return [Time] time for yesterday (formatted or not)
|
72
87
|
def yesterday(formatted=true)
|
73
|
-
time=
|
74
|
-
time=time.strftime(@format) if formatted
|
88
|
+
time = one_day_diff(false)
|
89
|
+
time = time.strftime(@format) if formatted
|
75
90
|
time
|
76
91
|
end
|
77
92
|
|
93
|
+
# Spanish for yesterday method
|
94
|
+
#
|
95
|
+
#
|
78
96
|
def ayer
|
79
97
|
self.yesterday(true)
|
80
98
|
end
|
81
99
|
|
100
|
+
# Portuges for yesterday method
|
101
|
+
#
|
102
|
+
#
|
82
103
|
def ontem
|
83
104
|
self.yesterday(true)
|
84
105
|
end
|
85
106
|
|
107
|
+
|
108
|
+
# Returns time value for tomorrow. Formated or not according to formatted param
|
109
|
+
#
|
110
|
+
# @param [Boolean] formatted = if true time is returned with format
|
111
|
+
#
|
86
112
|
def tomorrow(formatted=true)
|
87
|
-
time=
|
113
|
+
time = one_day_diff(true)
|
88
114
|
time=time.strftime(@format) if formatted
|
89
115
|
time
|
90
116
|
end
|
91
117
|
|
118
|
+
# Spanish for tomorrow method
|
119
|
+
#
|
120
|
+
#
|
92
121
|
def mañana
|
93
122
|
self.tomorrow(true)
|
94
123
|
end
|
95
124
|
|
125
|
+
# Portugues for tomorrow method
|
126
|
+
#
|
127
|
+
#
|
96
128
|
def manhã
|
97
129
|
self.tomorrow(true)
|
98
130
|
end
|
99
131
|
|
132
|
+
# Return one day of difference. One day more or less according to plus param. It is used by tomorrow and yesterday methods
|
133
|
+
#
|
134
|
+
# @param [Boolean] plus = if true, return one day more, else one day before
|
135
|
+
# @return [Time]
|
136
|
+
def one_day_diff(plus=true)
|
137
|
+
time=Time.now
|
138
|
+
diff = DICTIONARY[:day][:mult]
|
139
|
+
plus ? time+diff : time-diff
|
140
|
+
end
|
141
|
+
|
100
142
|
private
|
101
143
|
|
144
|
+
# Scans if period has day word
|
145
|
+
#
|
146
|
+
# @param [String] period = a string to convert to configured format
|
147
|
+
# @return [Boolean] true if perdiod contains the word day
|
148
|
+
def is_day?(period)
|
149
|
+
true if period.scan(DICTIONARY[:day][@lang]).size > 0
|
150
|
+
end
|
151
|
+
|
152
|
+
# Multiplication factor for a day
|
153
|
+
#
|
154
|
+
# @param [String] period = the string to convert to
|
155
|
+
# @return [Fixnum] multiplication factor for a day
|
102
156
|
def day_mult(period)
|
103
|
-
|
157
|
+
DICTIONARY[:day][:mult] if is_day?(period)
|
104
158
|
end
|
105
159
|
|
160
|
+
# Scans if period has week word
|
161
|
+
#
|
162
|
+
# @param [String] period = a string to convert to configured format
|
163
|
+
# @return [Boolean] true if perdiod contains the word week
|
164
|
+
def is_week?(period)
|
165
|
+
true if period.scan(DICTIONARY[:week][@lang]).size > 0
|
166
|
+
end
|
167
|
+
|
168
|
+
# Multiplication factor for a week
|
169
|
+
#
|
170
|
+
# @param [String] period = the string to convert to
|
171
|
+
# @return [Fixnum] multiplication factor for a week
|
106
172
|
def week_mult(period)
|
107
|
-
|
173
|
+
DICTIONARY[:week][:mult] if is_week?(period)
|
108
174
|
end
|
109
175
|
|
176
|
+
# Scans if period has week month
|
177
|
+
#
|
178
|
+
# @param [String] period = a string to convert to configured format
|
179
|
+
# @return [Boolean] true if perdiod contains the word month
|
180
|
+
def is_month?(period)
|
181
|
+
true if period.scan(DICTIONARY[:month][@lang]).size > 0
|
182
|
+
end
|
183
|
+
|
184
|
+
# Multiplication factor for a month
|
185
|
+
#
|
186
|
+
# @param [String] period = the string to convert to
|
187
|
+
# @return [Fixnum] multiplication factor for a month
|
110
188
|
def month_mult(period)
|
111
|
-
|
189
|
+
DICTIONARY[:month][:mult] if is_month?(period)
|
190
|
+
end
|
191
|
+
|
192
|
+
# Scans if period has week year
|
193
|
+
#
|
194
|
+
# @param [String] period = a string to convert to configured format
|
195
|
+
# @return [Boolean] true if perdiod contains the word year
|
196
|
+
def is_year?(period)
|
197
|
+
true if period.scan(DICTIONARY[:year][@lang]).size > 0
|
112
198
|
end
|
113
199
|
|
200
|
+
# Multiplication factor for a year
|
201
|
+
#
|
202
|
+
# @param [String] period = the string to convert to
|
203
|
+
# @return [Fixnum] multiplication factor for a year
|
114
204
|
def year_mult(period)
|
115
|
-
|
205
|
+
DICTIONARY[:year][:mult] if is_year?(period)
|
116
206
|
end
|
117
207
|
|
208
|
+
|
118
209
|
# Set true the matched keyword in a given string
|
119
210
|
#
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
211
|
+
# @param [String] period = the period of time expressed in a literal way
|
212
|
+
# @param [String] lang = the languaje to eval
|
213
|
+
# @return [Hash] times
|
123
214
|
def multiply_by(period)
|
124
|
-
|
125
215
|
return day_mult(period) || week_mult(period) || month_mult(period) || year_mult(period) || 1
|
126
|
-
# mult = 1
|
127
|
-
# mult = DICTIONARY[:day][:mult] if day_mult(period)
|
128
|
-
# mult = DICTIONARY[:week][:mult] if week_mult(period)
|
129
|
-
# mult = DICTIONARY[:month][:mult] if month_mult(period)
|
130
|
-
# mult = DICTIONARY[:year][:mult] if year_mult(period)
|
131
|
-
# return mult
|
132
216
|
end
|
133
217
|
|
134
218
|
|
135
219
|
# Return the Time object according to the splitted date in the given array
|
136
220
|
# |
|
137
|
-
#
|
138
|
-
#
|
221
|
+
# @param [Array] date = date splitted
|
222
|
+
# @return [Time]
|
139
223
|
def time_from_date(date)
|
140
224
|
numbers=date.scan(/\d+/).map!{|i| i.to_i}
|
141
225
|
day=numbers[2-numbers.index(numbers.max)]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dater
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -60,7 +60,7 @@ files:
|
|
60
60
|
- dater.gemspec
|
61
61
|
- lib/dater.rb
|
62
62
|
- lib/dater/version.rb
|
63
|
-
homepage: http://
|
63
|
+
homepage: http://romantestdev.github.io/dater/
|
64
64
|
licenses:
|
65
65
|
- MIT
|
66
66
|
post_install_message:
|