dater 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Gemfile.lock +1 -1
  2. data/lib/dater/version.rb +1 -1
  3. data/lib/dater.rb +81 -32
  4. metadata +1 -1
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dater (0.2.1)
4
+ dater (0.2.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/dater/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dater
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
data/lib/dater.rb CHANGED
@@ -9,13 +9,13 @@ module Dater
9
9
  attr_accessor :format, :lang
10
10
 
11
11
  DICTIONARY = {
12
- day: { "en"=>/day(s)?/, "es" => /d(i|í)a(s)?/,"pt" => /dia(s)?/},
13
- week: { "en"=>/week(s)?/, "es" => /semana(s)?/, "pt" => /semana(s)?/},
14
- month: { "en"=>/month(s)?/,"es" => /mes(es)?/, "pt" => /mes(es)?/},
15
- year: { "en"=>/year(s)?/, "es" => /año(s)?/, "pt" => /ano(s)?/},
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'}
12
+ day: { "en"=>/days/, "es" => /(dias|días)/,"pt" => /dias/, :mult => 86400},
13
+ week: { "en"=>/weeks/, "es" => /semanas/, "pt" => /semanas/, :mult => 604800},
14
+ month: { "en"=>/months/, "es" => /meses/, "pt" => /meses/, :mult => 2592000},
15
+ year: { "en"=>/years/, "es" => /años/, "pt" => /anos/, :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
19
  }
20
20
 
21
21
  # Creates a Dater::Resolver object
@@ -33,26 +33,21 @@ module Dater
33
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
- case period.downcase
36
+ @date=case period.downcase
37
37
  when DICTIONARY[:today][@lang]
38
- return (Time.now).strftime(@format)
38
+ self.today(false)
39
39
  when DICTIONARY[:tomorrow][@lang]
40
- return (Time.now+(3600*24)).strftime(@format)
40
+ self.tomorrow(false)
41
41
  when DICTIONARY[:yesterday][@lang]
42
- return (Time.now-(3600*24)).strftime(@format)
43
- when /\d+\/\d+\/\d+/
44
- parts=period.split('/')
45
- when /\d+\-\d+\-\d+/
46
- parts=period.split('-')
42
+ self.yesterday(false)
43
+ when /\d+.\d+.\d+/
44
+ time_from_date(period)
45
+ when /\d+\s.+/
46
+ Time.now+period.scan(/\d+/)[0].to_i*self.multiply_by(period,@lang)
47
47
  else
48
- if (amount=period.scan(/\d+/)).size>0
49
- additional=amount[0].to_i*self.multiply_by(period,@lang)
50
- @date=Time.now+additional
51
- else
52
- return period
53
- end
48
+ return period
54
49
  end
55
- @date=time_from_date(parts) unless parts.nil?
50
+ # unless parts.nil?
56
51
  return @date.strftime(@format)
57
52
  end
58
53
 
@@ -62,28 +57,82 @@ module Dater
62
57
  # Param [String] lang = the languaje to eval
63
58
  # Return [Hash] times
64
59
  def multiply_by(period, lang)
65
- return 3600*24 if period.scan(DICTIONARY[:day][lang]).size>0
66
- return 3600*24*7 if period.scan(DICTIONARY[:week][lang]).size>0
67
- return 3600*24*30 if period.scan(DICTIONARY[:month][lang]).size>0
68
- return 3600*24*30*12 if period.scan(DICTIONARY[:year][lang]).size>0
69
- return 1
60
+ mult = 1
61
+ mult = DICTIONARY[:day][:mult] if is_day?(period, lang)
62
+ mult = DICTIONARY[:week][:mult] if is_week?(period, lang)
63
+ mult = DICTIONARY[:month][:mult] if is_month?(period, lang)
64
+ mult = DICTIONARY[:year][:mult] if is_year?(period, lang)
65
+ return mult
66
+ end
67
+
68
+ def is_day?(period, lang)
69
+ period.scan(DICTIONARY[:day][lang]).size > 0 ? true : false
70
+ end
71
+
72
+ def is_week?(period, lang)
73
+ period.scan(DICTIONARY[:week][lang]).size > 0 ? true : false
74
+ end
75
+
76
+ def is_month?(period, lang)
77
+ period.scan(DICTIONARY[:month][lang]).size > 0 ? true : false
78
+ end
79
+
80
+ def is_year?(period, lang)
81
+ period.scan(DICTIONARY[:year][lang]).size > 0 ? true : false
70
82
  end
71
83
 
72
84
  # Return the Time object according to the splitted date in the given array
73
- #
85
+ # |
74
86
  # Param [Array] date = date splitted
75
87
  # Return [Time]
76
88
  def time_from_date(date)
77
- date.map!{|i| i.to_i}
78
- if date.first.to_s.size==4
79
- return Date.new(date[0],date[1],date[2]).to_time
89
+ numbers=date.scan(/\d+/).map!{|i| i.to_i}
90
+ if numbers.first.to_s.size==4
91
+ return Date.new(numbers[0],numbers[1],numbers[2]).to_time
80
92
  else
81
- return Date.new(date[2],date[1],date[0]).to_time
93
+ return Date.new(numbers[2],numbers[1],numbers[0]).to_time
82
94
  end
83
95
  end
84
96
 
85
97
  def para(period)
86
98
  self.for(period)
87
99
  end
100
+
101
+ def today(formatted=true)
102
+ formatted ? Time.now.strftime(@format) :Time.now
103
+ end
104
+
105
+ def hoy
106
+ self.today(true)
107
+ end
108
+
109
+ def hoje
110
+ self.today(true)
111
+ end
112
+
113
+ def yesterday(formatted=true)
114
+ formatted ? (Time.now-(3600*24)).strftime(@format) : Time.now-(3600*24)
115
+ end
116
+
117
+ def ayer
118
+ self.yesterday(true)
119
+ end
120
+
121
+ def ontem
122
+ self.yesterday(true)
123
+ end
124
+
125
+ def tomorrow(formatted=true)
126
+ formatted ? (Time.now+(3600*24)).strftime(@format) : (Time.now+(3600*24))
127
+ end
128
+
129
+ def mañana
130
+ self.tomorrow(true)
131
+ end
132
+
133
+ def manhã
134
+ self.tomorrow(true)
135
+ end
136
+
88
137
  end
89
138
  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.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: