dater 0.3.1 → 0.3.2
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/.gitignore +5 -1
- data/Gemfile.lock +1 -1
- data/Rakefile +8 -0
- data/lib/dater/version.rb +1 -1
- data/lib/dater.rb +60 -163
- data/lib/translator.rb +126 -0
- metadata +5 -4
data/.gitignore
CHANGED
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
data/lib/dater/version.rb
CHANGED
data/lib/dater.rb
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
require 'date'
|
3
|
+
require_relative 'translator'
|
3
4
|
|
4
5
|
module Dater
|
5
6
|
|
6
7
|
class Resolver
|
7
8
|
|
8
|
-
attr_accessor :format
|
9
|
+
attr_accessor :format
|
9
10
|
|
10
11
|
|
11
12
|
# Creates a Dater::Resolver object
|
@@ -16,7 +17,7 @@ module Dater
|
|
16
17
|
def initialize(format='%Y-%m-%d', lang="en", today_for_nil=false)
|
17
18
|
@today_for_nil=today_for_nil
|
18
19
|
@format=format
|
19
|
-
@
|
20
|
+
@translate=Dater::Translator.new(lang)
|
20
21
|
end
|
21
22
|
|
22
23
|
|
@@ -30,7 +31,8 @@ module Dater
|
|
30
31
|
period = now.strftime(@format) if today_for_nil
|
31
32
|
return period
|
32
33
|
else
|
33
|
-
|
34
|
+
translated = @translate.this period
|
35
|
+
@last_date = @date = time_for_period(no_special_chars(translated))
|
34
36
|
@date.strftime(@format) if @date.respond_to? :strftime
|
35
37
|
end
|
36
38
|
end
|
@@ -41,52 +43,12 @@ module Dater
|
|
41
43
|
self.for(period)
|
42
44
|
end
|
43
45
|
|
46
|
+
def lang= lang
|
47
|
+
@translate=Dater::Translator.new(lang)
|
48
|
+
end
|
44
49
|
|
45
50
|
private
|
46
51
|
|
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
52
|
|
91
53
|
TIME_IN_SECONDS = {
|
92
54
|
day: 86400,
|
@@ -97,79 +59,58 @@ module Dater
|
|
97
59
|
|
98
60
|
WEEKDAYS = [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"]
|
99
61
|
|
100
|
-
def spanish_translator word
|
101
|
-
word.split(" ").map do |word|
|
102
|
-
translate_from_spanish word
|
103
|
-
end.join(" ")
|
104
|
-
end
|
105
62
|
|
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
|
111
|
-
end
|
112
|
-
|
113
|
-
def portuguese_translator word
|
114
|
-
word.split(" ").map do |word|
|
115
|
-
translate_from_portuguese word
|
116
|
-
end.join(" ")
|
117
|
-
end
|
118
|
-
|
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
|
124
|
-
end
|
125
63
|
|
126
64
|
# Returns the formatted date according to the given period of time expresed in a literal way
|
127
65
|
#
|
128
66
|
# @param [String] period = time expressed literally (e.g: in 2 days)
|
129
67
|
# @return [String] formatted time
|
130
|
-
def time_for_period(
|
131
|
-
word = english_for no_special_chars(period)
|
68
|
+
def time_for_period(string=nil)
|
132
69
|
|
133
|
-
@last_date = case word
|
134
70
|
|
135
|
-
|
71
|
+
@last_date = case string
|
72
|
+
|
73
|
+
when /today/,/now/
|
136
74
|
now
|
137
75
|
|
138
|
-
when /tomorrow/
|
76
|
+
when /tomorrow/
|
139
77
|
tomorrow_time
|
140
78
|
|
141
|
-
when /yesterday/
|
79
|
+
when /yesterday/
|
142
80
|
yesterday_time
|
143
81
|
|
144
|
-
when /sunday
|
145
|
-
time_for_weekday(
|
82
|
+
when /sunday/, /monday/, /tuesday/, /wednesday/, /thursday/, /friday/, /saturday/
|
83
|
+
time_for_weekday(string)
|
146
84
|
|
147
|
-
when /next/
|
148
|
-
now + period_of_time_from_string(
|
85
|
+
when /next/
|
86
|
+
now + period_of_time_from_string(string.gsub("next","1"))
|
149
87
|
|
150
|
-
when /last/
|
151
|
-
now - period_of_time_from_string(
|
88
|
+
when /last/
|
89
|
+
now - period_of_time_from_string(string.gsub("last","1"))
|
152
90
|
|
153
|
-
when /
|
91
|
+
when /\d[\sa-zA-Z]+\sbefore/
|
154
92
|
@last_date ||= now
|
155
|
-
@last_date -= period_of_time_from_string(
|
93
|
+
@last_date -= period_of_time_from_string(string)
|
156
94
|
|
157
|
-
when /
|
95
|
+
when /\d[\sa-zA-Z]+\sago/
|
158
96
|
@last_date ||= now
|
159
|
-
now - period_of_time_from_string(
|
97
|
+
now - period_of_time_from_string(string)
|
160
98
|
|
161
|
-
when /
|
99
|
+
when /\d[\sa-zA-Z]+\slater/
|
162
100
|
@last_date ||= now
|
163
|
-
@last_date += period_of_time_from_string(
|
164
|
-
|
165
|
-
when /in/i
|
166
|
-
now + period_of_time_from_string(word)
|
101
|
+
@last_date += period_of_time_from_string(string)
|
167
102
|
|
168
|
-
when /
|
169
|
-
|
103
|
+
when /in/,/\d\sdays?/, /\d\smonths?/, /\d\sweeks?/, /\d\syears?/
|
104
|
+
now + period_of_time_from_string(string)
|
170
105
|
|
171
106
|
when /\d+.\d+.\d+/
|
172
|
-
time_from_date(
|
107
|
+
time_from_date(string)
|
108
|
+
|
109
|
+
when /rand/,/future/
|
110
|
+
now + rand(100_000_000)
|
111
|
+
|
112
|
+
when /past/
|
113
|
+
now - rand(100_000_000)
|
173
114
|
end
|
174
115
|
|
175
116
|
return @last_date
|
@@ -189,23 +130,7 @@ module Dater
|
|
189
130
|
@last_date = Time.now + TIME_IN_SECONDS[:day]
|
190
131
|
end
|
191
132
|
|
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
|
205
|
-
|
206
|
-
|
207
133
|
|
208
|
-
|
209
134
|
# Returns one week/month/year of difference from today's date. Formatted or not according to formatted param
|
210
135
|
#
|
211
136
|
# @param [String] period = the factor of difference (day, week, month, year) from today's date
|
@@ -229,17 +154,27 @@ module Dater
|
|
229
154
|
end
|
230
155
|
|
231
156
|
def time_for_weekday(word)
|
232
|
-
day =
|
157
|
+
day = extract_day_from word
|
233
158
|
@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)
|
159
|
+
begin
|
239
160
|
@count+= move_a_day(word)
|
240
|
-
end
|
161
|
+
end until is_required_day?(@count, day)
|
241
162
|
@count
|
163
|
+
end
|
242
164
|
|
165
|
+
def extract_day_from word
|
166
|
+
WEEKDAYS.select{ |day| day if day==word.scan(/[a-zA-Z]+/).last }.join
|
167
|
+
end
|
168
|
+
|
169
|
+
# Method to know if the day is the required day
|
170
|
+
#
|
171
|
+
# @param [Time] time
|
172
|
+
# @param [String] day = to match in case statement
|
173
|
+
# @return [Boolean] = true if day is the required day
|
174
|
+
def is_required_day?(time, day)
|
175
|
+
day_to_ask = "#{day}?"
|
176
|
+
result = eval("time.#{day_to_ask}") if time.respond_to? day_to_ask.to_sym
|
177
|
+
return result
|
243
178
|
end
|
244
179
|
|
245
180
|
# Return a day to add or substrac according to the given word
|
@@ -272,7 +207,7 @@ module Dater
|
|
272
207
|
# @param [String] period
|
273
208
|
# @return [Fixnum] multiplication factor for a day
|
274
209
|
def day_mult(period)
|
275
|
-
TIME_IN_SECONDS[:day] if is_day?
|
210
|
+
TIME_IN_SECONDS[:day] if is_day? period
|
276
211
|
end
|
277
212
|
|
278
213
|
# Scans if period has week word
|
@@ -288,7 +223,7 @@ module Dater
|
|
288
223
|
# @param [String] period
|
289
224
|
# @return [Fixnum] multiplication factor for a week
|
290
225
|
def week_mult(period)
|
291
|
-
TIME_IN_SECONDS[:week] if is_week?
|
226
|
+
TIME_IN_SECONDS[:week] if is_week? period
|
292
227
|
end
|
293
228
|
|
294
229
|
# Scans if period has month word
|
@@ -304,7 +239,7 @@ module Dater
|
|
304
239
|
# @param [String] period
|
305
240
|
# @return [Fixnum] multiplication factor for a month
|
306
241
|
def month_mult(period)
|
307
|
-
TIME_IN_SECONDS[:month] if is_month?
|
242
|
+
TIME_IN_SECONDS[:month] if is_month? period
|
308
243
|
end
|
309
244
|
|
310
245
|
# Scans if period string contain year word
|
@@ -320,42 +255,18 @@ module Dater
|
|
320
255
|
# @param [String] period = the string to convert to
|
321
256
|
# @return [Fixnum] multiplication factor for a year
|
322
257
|
def year_mult(period)
|
323
|
-
TIME_IN_SECONDS[:year] if is_year?
|
258
|
+
TIME_IN_SECONDS[:year] if is_year? period
|
324
259
|
end
|
325
260
|
|
326
261
|
# Returns seconds to multiply by for the given string
|
327
262
|
#
|
328
263
|
# @param [String] period = the period of time expressed in a literal way
|
329
264
|
# @return [Fixnum] number to multiply by
|
330
|
-
def multiply_by(
|
331
|
-
return day_mult(
|
265
|
+
def multiply_by(string)
|
266
|
+
return day_mult(string) || week_mult(string) || month_mult(string) || year_mult(string) || 1
|
332
267
|
end
|
333
268
|
|
334
|
-
|
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
|
269
|
+
|
359
270
|
|
360
271
|
# Return the Time object according to the splitted date in the given array
|
361
272
|
#
|
@@ -380,7 +291,6 @@ module Dater
|
|
380
291
|
#
|
381
292
|
#
|
382
293
|
def method_missing(meth)
|
383
|
-
# if meth.to_s =~ /^(next_|próximo_|proximo_|last_|último_|ultimo_|in_|\d_|for).+$/i
|
384
294
|
self.class.send :define_method, meth do
|
385
295
|
string = meth.to_s.gsub("_"," ")
|
386
296
|
self.for("for('#{string}')")
|
@@ -393,20 +303,7 @@ module Dater
|
|
393
303
|
end
|
394
304
|
|
395
305
|
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')
|
306
|
+
arg.gsub(/(á|Á)/, 'a').gsub(/(é|É)/, 'e').gsub(/(í|Í)/, 'i').gsub(/(ó|Ó)/, 'o').gsub(/(ú|Ú)/, 'u').gsub(/(ç|Ç)/, 'c').downcase
|
410
307
|
end
|
411
308
|
end
|
412
309
|
end
|
data/lib/translator.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Dater
|
4
|
+
|
5
|
+
class Translator
|
6
|
+
|
7
|
+
SUPPORTED_LANGUAGES = ["en","es","pt"]
|
8
|
+
|
9
|
+
PORTUGUESE = {
|
10
|
+
day:/dia/,
|
11
|
+
week:/semana/,
|
12
|
+
month:/mes/,
|
13
|
+
year:/ano/,
|
14
|
+
today:/hoje/,
|
15
|
+
tomorrow:/amanhã/,
|
16
|
+
yesterday:/ontem/,
|
17
|
+
in:/em/,
|
18
|
+
next:/prox/,
|
19
|
+
later:/depois/,
|
20
|
+
ago:/atras/,
|
21
|
+
before:/antes/,
|
22
|
+
monday:/segunda/,
|
23
|
+
tuesday:/terca/,
|
24
|
+
wednesday:/quarta/,
|
25
|
+
thursday:/quinta/,
|
26
|
+
friday:/sexta/,
|
27
|
+
saturday:/sabado/,
|
28
|
+
sunday:/domingo/,
|
29
|
+
rand:/acaso/,
|
30
|
+
futura:/futur/,
|
31
|
+
past:/passad/
|
32
|
+
}
|
33
|
+
|
34
|
+
SPANISH = {
|
35
|
+
day:/dia/,
|
36
|
+
week:/semana/,
|
37
|
+
month:/mes/,
|
38
|
+
year:/año/,
|
39
|
+
today:/hoy/,
|
40
|
+
tomorrow:/mañana/,
|
41
|
+
yesterday:/ayer/,
|
42
|
+
in:/en/,
|
43
|
+
next:/prox/,
|
44
|
+
later:/despues/,
|
45
|
+
ago:/atras/,
|
46
|
+
before:/antes/,
|
47
|
+
monday:/lunes/,
|
48
|
+
tuesday:/martes/,
|
49
|
+
wednesday:/miercoles/,
|
50
|
+
thursday:/jueves/,
|
51
|
+
friday:/viernes/,
|
52
|
+
saturday:/sabado/,
|
53
|
+
sunday:/domingo/,
|
54
|
+
rand:/aleator/,
|
55
|
+
future:/futur/,
|
56
|
+
past:/pasad/
|
57
|
+
}
|
58
|
+
|
59
|
+
def initialize(lang)
|
60
|
+
raise "Languaje #{lang} not supported" unless SUPPORTED_LANGUAGES.include? lang
|
61
|
+
@lang = lang
|
62
|
+
@dictionary = @lang == "es" ? SPANISH : PORTUGUESE
|
63
|
+
end
|
64
|
+
|
65
|
+
def this word
|
66
|
+
return word if @lang=="en"
|
67
|
+
mapper word
|
68
|
+
end
|
69
|
+
|
70
|
+
def mapper word
|
71
|
+
word.split(" ").map do |word|
|
72
|
+
get_english_for word
|
73
|
+
end.join(" ")
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_english_for word
|
77
|
+
@dictionary.each_pair do |k,v|
|
78
|
+
return k.to_s if word =~ v
|
79
|
+
end
|
80
|
+
word
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
# def english_for(word=nil)
|
87
|
+
|
88
|
+
# case @lang
|
89
|
+
# when "es"
|
90
|
+
# spanish_translator word
|
91
|
+
# when "pt"
|
92
|
+
# portuguese_translator word
|
93
|
+
# else
|
94
|
+
# word
|
95
|
+
# end
|
96
|
+
# end
|
97
|
+
|
98
|
+
# def spanish_translator word
|
99
|
+
# word.split(" ").map do |word|
|
100
|
+
# translate_from_spanish word
|
101
|
+
# end.join(" ")
|
102
|
+
# end
|
103
|
+
|
104
|
+
# def translate_from_spanish word
|
105
|
+
# SPANISH.each_pair do |k,v|
|
106
|
+
# return k.to_s if word =~ v
|
107
|
+
# end
|
108
|
+
# word
|
109
|
+
# end
|
110
|
+
|
111
|
+
# def portuguese_translator word
|
112
|
+
# word.split(" ").map do |word|
|
113
|
+
# translate_from_portuguese word
|
114
|
+
# end.join(" ")
|
115
|
+
# end
|
116
|
+
|
117
|
+
# def translate_from_portuguese word
|
118
|
+
# PORTUGUESE.each_pair do |k,v|
|
119
|
+
# return k.to_s if word =~ v
|
120
|
+
# end
|
121
|
+
# word
|
122
|
+
# end
|
123
|
+
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
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.3.
|
4
|
+
version: 0.3.2
|
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-
|
12
|
+
date: 2013-12-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- dater.gemspec
|
61
61
|
- lib/dater.rb
|
62
62
|
- lib/dater/version.rb
|
63
|
+
- lib/translator.rb
|
63
64
|
homepage: http://romgrod.github.io/dater/
|
64
65
|
licenses:
|
65
66
|
- MIT
|
@@ -75,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
76
|
version: '0'
|
76
77
|
segments:
|
77
78
|
- 0
|
78
|
-
hash:
|
79
|
+
hash: 3211044642615986663
|
79
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
81
|
none: false
|
81
82
|
requirements:
|
@@ -84,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
85
|
version: '0'
|
85
86
|
segments:
|
86
87
|
- 0
|
87
|
-
hash:
|
88
|
+
hash: 3211044642615986663
|
88
89
|
requirements: []
|
89
90
|
rubyforge_project:
|
90
91
|
rubygems_version: 1.8.25
|