dater 0.2.7 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +75 -26
- data/dater.gemspec +0 -1
- data/lib/dater.rb +298 -119
- data/lib/dater/version.rb +1 -1
- metadata +8 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -3,6 +3,8 @@
|
|
3
3
|
Dater
|
4
4
|
=====
|
5
5
|
|
6
|
+
Dater
|
7
|
+
|
6
8
|
Convert a period of time expressed in a literal way like 'in 2 days' to a formatted future date with a given format. It aims to be helpful in regression tests when you have to deal with dinamic dates.
|
7
9
|
|
8
10
|
You can also, convert dates from 'dd/mm/yyyy' to 'yyyy-mm-dd' or viceversa.
|
@@ -29,54 +31,101 @@ Or install it yourself as:
|
|
29
31
|
|
30
32
|
## Usage
|
31
33
|
|
32
|
-
|
34
|
+
require 'dater'
|
35
|
+
|
36
|
+
date=Dater::Resolver.new('%Y-%m-%d')
|
37
|
+
|
38
|
+
#date.for(String)
|
39
|
+
#Example:
|
40
|
+
date.for("in 2 weeks")
|
41
|
+
=begin
|
42
|
+
String could be:
|
43
|
+
"in 4 days" # => yyyy-mm-dd (date for 4 days from the date of today)
|
44
|
+
"in 2 weeks" # => yyyy-mm-dd (date for 2 weeks from the date of today)
|
45
|
+
"in 10 months" # => yyyy-mm-dd (date for 10 months from the date of today)
|
46
|
+
"in 1 year" # => yyyy-mm-dd (date for 1 year from the date of today)
|
47
|
+
"next day" # => yyyy-mm-dd (date for tomorrow from the date of today)
|
48
|
+
"next week" # => yyyy-mm-dd (adds a period of time for a week and returns the date)
|
49
|
+
"next month" # => yyyy-mm-dd (adds a period of time for a month and returns the date)
|
50
|
+
"next year" # => yyyy-mm-dd (adds a period of time for a year and returns the date)
|
51
|
+
"yesterday" # => yyyy-mm-dd (date of yesterday)
|
52
|
+
"today" # => yyyy-mm-dd (date of today)
|
53
|
+
"tomorrow" # => yyyy-mm-dd (date of tomorrow)
|
54
|
+
"2 days ago" # => yyyy-mm-dd (N days ago from today)
|
55
|
+
"3 weeks ago" # => yyyy-mm-dd (N weeks ago from today)
|
56
|
+
"4 months ago" # => yyyy-mm-dd (N months ago from today)
|
57
|
+
"1 year ago" # => yyyy-mm-dd (N year ago from today)
|
58
|
+
|
59
|
+
#Some equivalent methods you can use:
|
60
|
+
date.yesterday
|
61
|
+
date.today
|
62
|
+
date.tomorrow
|
63
|
+
date.sunday
|
64
|
+
date.monday
|
65
|
+
date.thuesday
|
66
|
+
date.wednesday
|
67
|
+
date.thursday
|
68
|
+
date.friday
|
69
|
+
date.saturday
|
70
|
+
date.next_day
|
71
|
+
date.next_week
|
72
|
+
date.next_month
|
73
|
+
date.next_year
|
74
|
+
=end
|
75
|
+
|
76
|
+
# Special feature
|
77
|
+
|
78
|
+
later and before
|
79
|
+
|
80
|
+
Giving the keywords 'later' or 'before' at the end of the string, Dater allows you to request dates from the last date requested. If there isn't any date requested previously, it takes as last requested date today's date
|
81
|
+
|
82
|
+
date.for("2 days later") # => You will get the date for two days later from the last requested date.
|
83
|
+
Example: Supose that the last requested date was 10/12/2013 (dd/mm/YYYY), using '2 days later' will give the result '12/12/2013'.
|
84
|
+
|
85
|
+
date.for("1 month before") # => You will get the date for one month before from the last requested date.
|
33
86
|
|
34
|
-
|
87
|
+
If the argument is nil, it will return nil. But you can initialize Dater object with 'today_for_nil' flag like:
|
35
88
|
|
36
|
-
|
89
|
+
date = Dater::Resolver.new("%Y-%m-%d", true)
|
37
90
|
|
38
|
-
|
91
|
+
date.for #=> yyyy-mm-dd (date of today)
|
39
92
|
|
40
|
-
|
93
|
+
You'll find format directives at following link:
|
41
94
|
|
42
|
-
|
43
|
-
date.for("in 2 weeks") # => yyyy-mm-dd (date for 2 weeks from the date of today)
|
44
|
-
date.for("in 10 months") # => yyyy-mm-dd (date for 10 months from the date of today)
|
45
|
-
date.for("in 1 year") # => yyyy-mm-dd (date for 1 year from the date of today)
|
95
|
+
http://apidock.com/ruby/Time/strftime
|
46
96
|
|
47
|
-
|
97
|
+
It does not work correctly with leap-years and calculates months of 30 days only.
|
48
98
|
|
49
|
-
date.for("tomorrow") # => yyyy-mm-dd (date of tomorrow)
|
50
99
|
|
51
|
-
|
100
|
+
It supports following languages: English (lang='en'), Spanish (lang='es') and Portuguese (lang='pt')
|
52
101
|
|
53
|
-
|
102
|
+
***
|
54
103
|
|
55
|
-
date = Dater::Resolver.new("%Y-%m-%d", true)
|
56
104
|
|
57
|
-
|
105
|
+
Si deseas usar esta gema en español puedes inicializar la clase de la siguiente manera:
|
58
106
|
|
59
|
-
|
107
|
+
fecha=Dater::Resolver.new('%Y-%m-%d', false, "es")
|
108
|
+
fecha.para("en 1 semana")
|
109
|
+
fecha.ayer
|
110
|
+
fecha.hoy
|
111
|
+
fecha.mañana
|
60
112
|
|
61
|
-
http://apidock.com/ruby/Time/strftime
|
62
113
|
|
63
|
-
|
114
|
+
Con eso puedes pasar argumentos en idioma español (por ejemplo 'en 2 días', 'en 10 meses', 'en 1 año')
|
64
115
|
|
65
|
-
==================================================
|
66
116
|
|
67
|
-
|
117
|
+
***
|
68
118
|
|
69
|
-
|
119
|
+
Se você quiser usar esta gem em Português, você pode inicializar a classe da seguinte forma:
|
70
120
|
|
71
|
-
|
121
|
+
data=Dater::Resolver.new('%Y-%m-%d', false, "pt")
|
122
|
+
data.para("em 2 dias")
|
72
123
|
|
73
|
-
|
124
|
+
## Contributor
|
74
125
|
|
75
|
-
|
126
|
+
Federico Hertzulis
|
76
127
|
|
77
|
-
date=Dater::Resolver.new('%Y-%m-%d', "pt")
|
78
128
|
|
79
|
-
Com isso você pode passar argumentos em Português (em 2 dias, em 10 meses, 1 ano)
|
80
129
|
## Contributing
|
81
130
|
|
82
131
|
1. Fork it
|
data/dater.gemspec
CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
|
-
|
21
20
|
spec.add_development_dependency "bundler", "~> 1.3"
|
22
21
|
spec.add_development_dependency "rake"
|
23
22
|
end
|
data/lib/dater.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
require 'date'
|
3
|
-
require_relative "dater/version"
|
4
3
|
|
5
4
|
module Dater
|
6
5
|
|
@@ -8,197 +7,312 @@ module Dater
|
|
8
7
|
|
9
8
|
attr_accessor :format, :lang
|
10
9
|
|
11
|
-
DICTIONARY = {
|
12
|
-
day: { "en"=>/day/, "es" => /(dia|día)/, "pt" => /dia/, :mult => 86400},
|
13
|
-
week: { "en"=>/week/, "es" => /semana/, "pt" => /semana/, :mult => 604800},
|
14
|
-
month: { "en"=>/month/, "es" => /mes/, "pt" => /mes/, :mult => 2592000},
|
15
|
-
year: { "en"=>/year/, "es" => /año/, "pt" => /ano/, :mult => 31536000},
|
16
|
-
today: { "en"=>'today', "es" => 'hoy', "pt" => 'hoje' },
|
17
|
-
tomorrow: { "en"=>'tomorrow', "es" => 'mañana', "pt" => 'manhã' },
|
18
|
-
yesterday:{ "en"=>'yesterday',"es" => 'ayer', "pt" => 'ontem' }
|
19
|
-
}
|
20
10
|
|
21
11
|
# Creates a Dater::Resolver object
|
22
12
|
#
|
23
13
|
# @param [String] format = date format
|
24
|
-
# @param [Boolean] today_for_nil = Indicates if must return today'd date if argument in for method is nil
|
25
14
|
# @param [String] lang = languaje for matching (en=english, es=spanish, pt=portuguese)
|
26
|
-
|
15
|
+
# @param [Boolean] today_for_nil = Indicates if must return today's date if given argument is nil
|
16
|
+
def initialize(format='%Y-%m-%d', lang="en", today_for_nil=false)
|
27
17
|
@today_for_nil=today_for_nil
|
28
18
|
@format=format
|
29
|
-
@lang=lang if ["en","es","pt"].include? lang
|
19
|
+
@lang=lang if ["en","es","pt"].include? lang
|
30
20
|
end
|
31
21
|
|
22
|
+
|
23
|
+
|
32
24
|
# Convert the period of time passed as argument to the configured format
|
33
25
|
#
|
34
|
-
# @param [String] period = a period of time
|
35
|
-
# @return [String] converted date to the configured format. If period is nil, returns date for tomorrow
|
26
|
+
# @param [String] period = a period of time expreseed in a literal way to convert to the configured format (@format)
|
27
|
+
# @return [String] converted date to the configured format. If period is nil and @today_for_nil is true, returns date for tomorrow. Else returns nil
|
36
28
|
def for(period=nil)
|
37
|
-
if period.nil?
|
38
|
-
|
39
|
-
end
|
40
|
-
@date=case period.downcase
|
41
|
-
when DICTIONARY[:today][@lang]
|
42
|
-
self.today(false)
|
43
|
-
when DICTIONARY[:tomorrow][@lang]
|
44
|
-
self.tomorrow(false)
|
45
|
-
when DICTIONARY[:yesterday][@lang]
|
46
|
-
self.yesterday(false)
|
47
|
-
when /\d+.\d+.\d+/
|
48
|
-
time_from_date(period)
|
49
|
-
when /\d+\s.+/
|
50
|
-
Time.now+period.scan(/\d+/)[0].to_i*multiply_by(period)
|
51
|
-
else
|
29
|
+
if period.nil? or period == ""
|
30
|
+
period = now.strftime(@format) if today_for_nil
|
52
31
|
return period
|
32
|
+
else
|
33
|
+
@last_date = @date = time_for_period(period)
|
34
|
+
@date.strftime(@format) if @date.respond_to? :strftime
|
53
35
|
end
|
54
|
-
# unless parts.nil?
|
55
|
-
return @date.strftime(@format)
|
56
36
|
end
|
57
37
|
|
58
|
-
# Spanish for 'for' method
|
59
|
-
#
|
60
|
-
#
|
38
|
+
# Spanish and portuguese equivalent for 'for' method
|
39
|
+
#
|
61
40
|
def para(period)
|
62
41
|
self.for(period)
|
63
42
|
end
|
64
43
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
PORTUGUESE = {
|
48
|
+
day:/dia/i,
|
49
|
+
week:/semana/i,
|
50
|
+
month:/mes/i,
|
51
|
+
year:/ano/i,
|
52
|
+
today:/hoje/,
|
53
|
+
tomorrow:/amanhã/i,
|
54
|
+
yesterday:/ontem/i,
|
55
|
+
in:/em/i,
|
56
|
+
next:/prox/i,
|
57
|
+
later:/depois/i,
|
58
|
+
ago:/atras/i,
|
59
|
+
before:/antes/i,
|
60
|
+
monday:/segunda/i,
|
61
|
+
tuesday:/terca/i,
|
62
|
+
wednesday:/quarta/i,
|
63
|
+
thursday:/quinta/i,
|
64
|
+
friday:/sexta/i,
|
65
|
+
saturday:/sabado/i,
|
66
|
+
sunday:/domingo/i
|
67
|
+
}
|
68
|
+
|
69
|
+
SPANISH = {
|
70
|
+
day:/dia/i,
|
71
|
+
week:/semana/i,
|
72
|
+
month:/mes/i,
|
73
|
+
year:/año/i,
|
74
|
+
today:/hoy/i,
|
75
|
+
tomorrow:/mañana/i,
|
76
|
+
yesterday:/ayer/i,
|
77
|
+
in:/en/i,
|
78
|
+
next:/prox/i,
|
79
|
+
later:/despues/i,
|
80
|
+
ago:/atras/i,
|
81
|
+
before:/antes/i,
|
82
|
+
monday:/lunes/i,
|
83
|
+
tuesday:/martes/i,
|
84
|
+
wednesday:/miercoles/i,
|
85
|
+
thursday:/jueves/i,
|
86
|
+
friday:/viernes/i,
|
87
|
+
saturday:/sabado/i,
|
88
|
+
sunday:/domingo/i
|
89
|
+
}
|
90
|
+
|
91
|
+
TIME_IN_SECONDS = {
|
92
|
+
day: 86400,
|
93
|
+
week: 604800,
|
94
|
+
month: 2592000,
|
95
|
+
year: 31536000
|
96
|
+
}
|
97
|
+
|
98
|
+
WEEKDAYS = [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
|
99
|
+
|
100
|
+
def spanish_translator word
|
101
|
+
word.split(" ").map do |word|
|
102
|
+
translate_from_spanish word
|
103
|
+
end.join(" ")
|
104
|
+
end
|
105
|
+
|
106
|
+
def translate_from_spanish word
|
107
|
+
SPANISH.each_pair do |k,v|
|
108
|
+
return k.to_s if word =~ v
|
109
|
+
end
|
110
|
+
word
|
73
111
|
end
|
74
112
|
|
75
|
-
|
76
|
-
|
77
|
-
|
113
|
+
def portuguese_translator word
|
114
|
+
word.split(" ").map do |word|
|
115
|
+
translate_from_portuguese word
|
116
|
+
end.join(" ")
|
78
117
|
end
|
79
118
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
119
|
+
def translate_from_portuguese word
|
120
|
+
PORTUGUESE.each_pair do |k,v|
|
121
|
+
return k.to_s if word =~ v
|
122
|
+
end
|
123
|
+
word
|
85
124
|
end
|
86
125
|
|
87
|
-
# Returns the time
|
126
|
+
# Returns the formatted date according to the given period of time expresed in a literal way
|
88
127
|
#
|
89
|
-
# @param [
|
90
|
-
# @return [
|
91
|
-
def
|
92
|
-
|
93
|
-
|
94
|
-
|
128
|
+
# @param [String] period = time expressed literally (e.g: in 2 days)
|
129
|
+
# @return [String] formatted time
|
130
|
+
def time_for_period(period=nil)
|
131
|
+
word = english_for no_special_chars(period)
|
132
|
+
|
133
|
+
@last_date = case word
|
134
|
+
|
135
|
+
when /today/i
|
136
|
+
now
|
137
|
+
|
138
|
+
when /tomorrow/i
|
139
|
+
tomorrow_time
|
140
|
+
|
141
|
+
when /yesterday/i
|
142
|
+
yesterday_time
|
143
|
+
|
144
|
+
when /sunday/i, /monday/i, /tuesday/i, /wednesday/i, /thursday/i, /friday/i, /saturday/i
|
145
|
+
time_for_weekday(word)
|
146
|
+
|
147
|
+
when /next/i
|
148
|
+
now + period_of_time_from_string(word.gsub("next","1"))
|
149
|
+
|
150
|
+
when /last/i
|
151
|
+
now - period_of_time_from_string(word.gsub("last","1"))
|
152
|
+
|
153
|
+
when /before/i
|
154
|
+
@last_date ||= now
|
155
|
+
@last_date -= period_of_time_from_string(word)
|
156
|
+
|
157
|
+
when /ago/i
|
158
|
+
@last_date ||= now
|
159
|
+
now - period_of_time_from_string(word)
|
160
|
+
|
161
|
+
when /later/i
|
162
|
+
@last_date ||= now
|
163
|
+
@last_date += period_of_time_from_string(word)
|
164
|
+
|
165
|
+
when /in/i
|
166
|
+
now + period_of_time_from_string(word)
|
167
|
+
|
168
|
+
when /day/i, /month/i, /week/i, /year/i
|
169
|
+
move_to(word)
|
170
|
+
|
171
|
+
when /\d+.\d+.\d+/
|
172
|
+
time_from_date(word)
|
173
|
+
end
|
174
|
+
|
175
|
+
return @last_date
|
176
|
+
end
|
177
|
+
|
178
|
+
# Returns now time
|
179
|
+
# @return [Time] @last_date = today's time
|
180
|
+
def now
|
181
|
+
@last_date=Time.now
|
95
182
|
end
|
96
183
|
|
97
|
-
|
98
|
-
|
99
|
-
#
|
100
|
-
def ayer
|
101
|
-
self.yesterday(true)
|
184
|
+
def yesterday_time
|
185
|
+
@last_date = Time.now - TIME_IN_SECONDS[:day]
|
102
186
|
end
|
103
187
|
|
104
|
-
|
105
|
-
|
106
|
-
#
|
107
|
-
def ontem
|
108
|
-
self.yesterday(true)
|
188
|
+
def tomorrow_time
|
189
|
+
@last_date = Time.now + TIME_IN_SECONDS[:day]
|
109
190
|
end
|
110
191
|
|
192
|
+
def english_for(word=nil)
|
193
|
+
unless word.nil?
|
194
|
+
word = no_special_chars(word.downcase)
|
195
|
+
end
|
196
|
+
case @lang
|
197
|
+
when /es/,/sp/
|
198
|
+
spanish_translator word
|
199
|
+
when /pt/,/port/
|
200
|
+
portuguese_translator word
|
201
|
+
else
|
202
|
+
word
|
203
|
+
end
|
204
|
+
end
|
111
205
|
|
112
|
-
|
113
|
-
|
114
|
-
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# Returns one week/month/year of difference from today's date. Formatted or not according to formatted param
|
115
210
|
#
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
211
|
+
# @param [String] period = the factor of difference (day, week, month, year) from today's date
|
212
|
+
# @return [Time]
|
213
|
+
def next(period)
|
214
|
+
Time.now + multiply_by(period)
|
120
215
|
end
|
121
216
|
|
122
|
-
#
|
217
|
+
# Returns one week/month/year of difference from today's date. Formatted or not according to formatted param
|
123
218
|
#
|
124
|
-
#
|
125
|
-
|
126
|
-
|
219
|
+
# @param [String] period = the factor of difference (day, week, month, year) from today's date
|
220
|
+
# @return [Time]
|
221
|
+
def last(period)
|
222
|
+
Time.now - multiply_by(period)
|
127
223
|
end
|
128
224
|
|
129
|
-
#
|
130
|
-
#
|
131
|
-
|
132
|
-
|
133
|
-
self.tomorrow(true)
|
225
|
+
# Returns the number of seconds for the given string to add or substract according to argument
|
226
|
+
# If argument has the word 'last' it goes backward, else forward
|
227
|
+
def move_to(word)
|
228
|
+
word.scan(/last/i).size>0 ? self.last(word) : self.next(word)
|
134
229
|
end
|
135
230
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
231
|
+
def time_for_weekday(word)
|
232
|
+
day = WEEKDAYS.select{ |day| day if day==word.scan(/[a-zA-Z]+/).last }.join
|
233
|
+
@count = Time.now
|
234
|
+
|
235
|
+
# Add one day if today is the same required week day
|
236
|
+
@count += move_a_day(word) if is_required_day?(@count, day)
|
237
|
+
|
238
|
+
until is_required_day?(@count, day)
|
239
|
+
@count+= move_a_day(word)
|
240
|
+
end
|
241
|
+
@count
|
242
|
+
|
144
243
|
end
|
145
244
|
|
146
|
-
|
245
|
+
# Return a day to add or substrac according to the given word
|
246
|
+
# Substract if word contains 'last' word
|
247
|
+
# @return +/-[Fixnum] time in seconds for a day (+/-)
|
248
|
+
def move_a_day(word)
|
249
|
+
word.scan(/last/i).size > 0 ? a_day_backward : a_day_forward
|
250
|
+
end
|
251
|
+
|
252
|
+
# Returns the amount in seconds for a day (positive)
|
253
|
+
def a_day_forward
|
254
|
+
TIME_IN_SECONDS[:day]
|
255
|
+
end
|
256
|
+
|
257
|
+
# Returns the amount in seconds for a day (negative)
|
258
|
+
def a_day_backward
|
259
|
+
-TIME_IN_SECONDS[:day]
|
260
|
+
end
|
147
261
|
|
148
262
|
# Scans if period has day word
|
149
263
|
#
|
150
|
-
# @param [String] period
|
264
|
+
# @param [String] period
|
151
265
|
# @return [Boolean] true if perdiod contains the word day
|
152
266
|
def is_day?(period)
|
153
|
-
|
267
|
+
period.scan(/day/i).size > 0
|
154
268
|
end
|
155
269
|
|
156
270
|
# Multiplication factor for a day
|
157
271
|
#
|
158
|
-
# @param [String] period
|
272
|
+
# @param [String] period
|
159
273
|
# @return [Fixnum] multiplication factor for a day
|
160
274
|
def day_mult(period)
|
161
|
-
|
275
|
+
TIME_IN_SECONDS[:day] if is_day?(english_for period)
|
162
276
|
end
|
163
277
|
|
164
278
|
# Scans if period has week word
|
165
279
|
#
|
166
|
-
# @param [String] period
|
280
|
+
# @param [String] period
|
167
281
|
# @return [Boolean] true if perdiod contains the word week
|
168
282
|
def is_week?(period)
|
169
|
-
|
283
|
+
period.scan(/week/i).size > 0
|
170
284
|
end
|
171
285
|
|
172
286
|
# Multiplication factor for a week
|
173
287
|
#
|
174
|
-
# @param [String] period
|
288
|
+
# @param [String] period
|
175
289
|
# @return [Fixnum] multiplication factor for a week
|
176
290
|
def week_mult(period)
|
177
|
-
|
291
|
+
TIME_IN_SECONDS[:week] if is_week?(english_for period)
|
178
292
|
end
|
179
293
|
|
180
|
-
# Scans if period has
|
294
|
+
# Scans if period has month word
|
181
295
|
#
|
182
|
-
# @param [String] period
|
296
|
+
# @param [String] period
|
183
297
|
# @return [Boolean] true if perdiod contains the word month
|
184
298
|
def is_month?(period)
|
185
|
-
|
299
|
+
period.scan(/month/).size > 0
|
186
300
|
end
|
187
301
|
|
188
302
|
# Multiplication factor for a month
|
189
303
|
#
|
190
|
-
# @param [String] period
|
304
|
+
# @param [String] period
|
191
305
|
# @return [Fixnum] multiplication factor for a month
|
192
306
|
def month_mult(period)
|
193
|
-
|
307
|
+
TIME_IN_SECONDS[:month] if is_month?(english_for period)
|
194
308
|
end
|
195
309
|
|
196
|
-
# Scans if period
|
310
|
+
# Scans if period string contain year word
|
197
311
|
#
|
198
|
-
# @param [String] period
|
312
|
+
# @param [String] period to scan
|
199
313
|
# @return [Boolean] true if perdiod contains the word year
|
200
314
|
def is_year?(period)
|
201
|
-
|
315
|
+
period.scan(/year/i).size > 0
|
202
316
|
end
|
203
317
|
|
204
318
|
# Multiplication factor for a year
|
@@ -206,28 +320,93 @@ module Dater
|
|
206
320
|
# @param [String] period = the string to convert to
|
207
321
|
# @return [Fixnum] multiplication factor for a year
|
208
322
|
def year_mult(period)
|
209
|
-
|
323
|
+
TIME_IN_SECONDS[:year] if is_year?(english_for period)
|
210
324
|
end
|
211
325
|
|
212
|
-
|
213
|
-
# Set true the matched keyword in a given string
|
326
|
+
# Returns seconds to multiply by for the given string
|
214
327
|
#
|
215
328
|
# @param [String] period = the period of time expressed in a literal way
|
216
|
-
# @
|
217
|
-
# @return [Hash] times
|
329
|
+
# @return [Fixnum] number to multiply by
|
218
330
|
def multiply_by(period)
|
219
331
|
return day_mult(period) || week_mult(period) || month_mult(period) || year_mult(period) || 1
|
220
332
|
end
|
333
|
+
|
334
|
+
# Method to know if the day is the required day
|
335
|
+
#
|
336
|
+
# @param [Time] time
|
337
|
+
# @param [String] day = to match in case statement
|
338
|
+
# @return [Boolean] = true if day is the required day
|
339
|
+
def is_required_day?(time, day)
|
340
|
+
case english_for(day).to_s
|
341
|
+
when /monday/i
|
342
|
+
time.monday?
|
343
|
+
when /tuesday/i
|
344
|
+
time.tuesday?
|
345
|
+
when /wednesday/i
|
346
|
+
time.wednesday?
|
347
|
+
when /thursday/i
|
348
|
+
time.thursday?
|
349
|
+
when /friday/i
|
350
|
+
time.friday?
|
351
|
+
when /saturday/i
|
352
|
+
time.saturday?
|
353
|
+
when /sunday/i
|
354
|
+
time.sunday?
|
355
|
+
else
|
356
|
+
false
|
357
|
+
end
|
358
|
+
end
|
221
359
|
|
222
|
-
|
223
360
|
# Return the Time object according to the splitted date in the given array
|
224
|
-
#
|
225
|
-
# @param [
|
361
|
+
#
|
362
|
+
# @param [String] date
|
226
363
|
# @return [Time]
|
227
364
|
def time_from_date(date)
|
228
365
|
numbers=date.scan(/\d+/).map!{|i| i.to_i}
|
229
366
|
day=numbers[2-numbers.index(numbers.max)]
|
230
367
|
Date.new(numbers.max,numbers[1],day).to_time
|
231
368
|
end
|
369
|
+
|
370
|
+
|
371
|
+
# Returns the time according to the given string
|
372
|
+
#
|
373
|
+
# @param [String] word = period of time in literal way
|
374
|
+
# @return [Fixnum] multiplication factor (seconds)
|
375
|
+
def period_of_time_from_string(word)
|
376
|
+
word.scan(/\d+/)[0].to_i * multiply_by(word)
|
377
|
+
end
|
378
|
+
|
379
|
+
# Try to convert Missing methods to string and call to for method with converted string
|
380
|
+
#
|
381
|
+
#
|
382
|
+
def method_missing(meth)
|
383
|
+
# if meth.to_s =~ /^(next_|próximo_|proximo_|last_|último_|ultimo_|in_|\d_|for).+$/i
|
384
|
+
self.class.send :define_method, meth do
|
385
|
+
string = meth.to_s.gsub("_"," ")
|
386
|
+
self.for("for('#{string}')")
|
387
|
+
end
|
388
|
+
begin
|
389
|
+
self.send(meth.to_s)
|
390
|
+
rescue
|
391
|
+
raise "Method does not exists (#{meth})."
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
def no_special_chars(arg)
|
396
|
+
arg.\
|
397
|
+
gsub('á', 'a').\
|
398
|
+
gsub('é', 'e').\
|
399
|
+
gsub('í', 'i').\
|
400
|
+
gsub('ó', 'o').\
|
401
|
+
gsub('ú', 'u').\
|
402
|
+
gsub('ç', 'c').\
|
403
|
+
gsub('Á', 'a').\
|
404
|
+
gsub('É', 'e').\
|
405
|
+
gsub('Í', 'i').\
|
406
|
+
gsub('Ó', 'o').\
|
407
|
+
gsub('Ú', 'u').\
|
408
|
+
gsub('Ñ', 'n').\
|
409
|
+
gsub('Ç', 'c')
|
410
|
+
end
|
232
411
|
end
|
233
|
-
end
|
412
|
+
end
|
data/lib/dater/version.rb
CHANGED
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.
|
4
|
+
version: 0.3.0
|
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-
|
12
|
+
date: 2013-12-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -73,12 +73,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
73
73
|
- - ! '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
|
+
segments:
|
77
|
+
- 0
|
78
|
+
hash: -58735641299839235
|
76
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
80
|
none: false
|
78
81
|
requirements:
|
79
82
|
- - ! '>='
|
80
83
|
- !ruby/object:Gem::Version
|
81
84
|
version: '0'
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
hash: -58735641299839235
|
82
88
|
requirements: []
|
83
89
|
rubyforge_project:
|
84
90
|
rubygems_version: 1.8.25
|