dater 0.1.1 → 0.2.0

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.
Files changed (4) hide show
  1. data/Gemfile.lock +1 -1
  2. data/lib/dater/version.rb +1 -1
  3. data/lib/dater.rb +61 -44
  4. metadata +2 -2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dater (0.0.3)
4
+ dater (0.1.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.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/dater.rb CHANGED
@@ -1,10 +1,23 @@
1
1
  # coding: utf-8
2
+ require 'date'
2
3
  require_relative "dater/version"
3
4
 
4
5
  module Dater
5
6
 
6
7
  class Resolver
7
8
 
9
+ attr_accessor :format, :lang
10
+
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'}
19
+ }
20
+
8
21
  # Creates a Dater::Resolver object
9
22
  #
10
23
  # Param [String] format = date format
@@ -20,62 +33,66 @@ module Dater
20
33
  # Return [String] converted date to the configured format. If period is nil, returns date for tomorrow
21
34
  def for(period=nil)
22
35
  return (Time.now).strftime(@format) if period.nil?
23
- case period.downcase
24
- when 'today','hoy','hoje'
36
+ case period.downcase
37
+ when DICTIONARY[:today][@lang]
25
38
  return (Time.now).strftime(@format)
26
- when 'tomorrow','mañana','manhã'
39
+ when DICTIONARY[:tomorrow][@lang]
27
40
  return (Time.now+(3600*24)).strftime(@format)
28
- when 'yesterday','ayer','ontem'
41
+ when DICTIONARY[:yesterday][@lang]
29
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('-')
30
47
  else
31
- if period.include?('/')
32
- date=period.split('/')
33
- if @format.include? '-'
34
- return date.join('-') if date.first.size==4
35
- return "#{date[2]}-#{date[1]}-#{date[0]}"
36
- else
37
- return date.join('/') if date.first.size==4
38
- return "#{date[2]}/#{date[1]}/#{date[0]}"
39
- end
40
- elsif period.include?('-')
41
- date=period.split('-')
42
- if @format.include? '-'
43
- return date.join('-') if date.first.size==4
44
- return "#{date[2]}-#{date[1]}-#{date[0]}"
45
- else
46
- return date.join('/') if date.first.size==4
47
- return "#{date[2]}/#{date[1]}/#{date[0]}"
48
- end
49
- elsif (amount=period.scan(/\d+/)).size>0
50
- case @lang
51
- when 'es'
52
- days=true if period.scan(/dia(s)?/).size>0
53
- months=true if period.scan(/mes(es)?/).size>0
54
- year=true if period.scan(/año(s)?/).size>0
55
- when 'en'
56
- days=true if period.scan(/day(s)?/).size>0
57
- months=true if period.scan(/month(s)?/).size>0
58
- year=true if period.scan(/year(s)?/).size>0
59
- when 'pt'
60
- days=true if period.scan(/dia(s)?/).size>0
61
- months=true if period.scan(/mes(es)?/).size>0
62
- year=true if period.scan(/ano(s)?/).size>0
63
- end
64
- if year
65
- multiply_by=3600*24*30*12
66
- elsif months
67
- multiply_by=3600*24*30
48
+ if (amount=period.scan(/\d+/)).size>0
49
+ times=self.eval_times(period, @lang)
50
+ if times[:year]
51
+ multiply_by = 3600*24*30*12
52
+ elsif times[:months]
53
+ multiply_by = 3600*24*30
54
+ elsif times[:weeks]
55
+ multiply_by = 3600*24*7
56
+ elsif times[:days]
57
+ multiply_by = 3600*24
68
58
  else
69
- multiply_by=3600*24
59
+ multiply_by = 1
70
60
  end
71
61
  additional=amount[0].to_i*multiply_by
72
62
  @date=Time.now+additional
73
- @date=@date.strftime(@format)
74
63
  else
75
64
  return period
76
65
  end
77
66
  end
78
- return @date
67
+ @date=time_from_date(parts) unless parts.nil?
68
+ return @date.strftime(@format)
69
+ end
70
+
71
+ # Set true the matched keyword in a given string
72
+ #
73
+ # Param [String] period = the period of time expressed in a literal way
74
+ # Param [String] lang = the languaje to eval
75
+ # Return [Hash] times
76
+ def eval_times(period, lang)
77
+ times = {}
78
+ times[:days] = true if period.scan(DICTIONARY[:day][lang]).size>0
79
+ times[:weeks] = true if period.scan(DICTIONARY[:week][lang]).size>0
80
+ times[:months] = true if period.scan(DICTIONARY[:month][lang]).size>0
81
+ times[:year] = true if period.scan(DICTIONARY[:year][lang]).size>0
82
+ times
83
+ end
84
+
85
+ # Return the Time object according to the splitted date in the given array
86
+ #
87
+ # Param [Array] date = date splitted
88
+ # Return [Time]
89
+ def time_from_date(date)
90
+ date.map!{|i| i.to_i}
91
+ if date.first.to_s.size==4
92
+ return Date.new(date[0],date[1],date[2]).to_time
93
+ else
94
+ return Date.new(date[2],date[1],date[0]).to_time
95
+ end
79
96
  end
80
97
 
81
98
  def para(period)
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.1.1
4
+ version: 0.2.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-09-05 00:00:00.000000000 Z
12
+ date: 2013-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler