chronic18n 0.0.3 → 0.0.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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
data/chronic18n.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{chronic18n}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marcos Piccinini"]
12
- s.date = %q{2010-06-16}
12
+ s.date = %q{2010-06-21}
13
13
  s.description = %q{One 'way' to i18n chronic}
14
14
  s.email = %q{x@nofxx.com}
15
15
  s.extra_rdoc_files = [
@@ -26,8 +26,11 @@ Gem::Specification.new do |s|
26
26
  "chronic18n.gemspec",
27
27
  "lib/chronic18n.rb",
28
28
  "lib/chronic18n/dics/es.yml",
29
+ "lib/chronic18n/dics/es_regex.yml",
29
30
  "lib/chronic18n/dics/it.yml",
31
+ "lib/chronic18n/dics/it_regex.yml",
30
32
  "lib/chronic18n/dics/pt.yml",
33
+ "lib/chronic18n/dics/pt_regex.yml",
31
34
  "lib/chronic18n/translator.rb",
32
35
  "spec/chronic18n/en_spec.rb",
33
36
  "spec/chronic18n/es_spec.rb",
data/lib/chronic18n.rb CHANGED
@@ -5,8 +5,18 @@ require "chronic18n/translator"
5
5
 
6
6
  module Chronic18n
7
7
 
8
- def self.parse(txt, lang = nil)
9
- Chronic.parse(lang ? Translator.new(txt, lang).work : txt)
8
+ #
9
+ # Try to get a Time object out of a chunk of text.
10
+ #
11
+ # Chronic18n.parse("hoje", "pt")
12
+ # Chronic18n.parse("oggi", "it")
13
+ #
14
+ # Falls back to Chronic with "en" or w/o params:
15
+ #
16
+ # Chronic18n.parse("today")
17
+ #
18
+ def self.parse(txt, lang = "en")
19
+ Chronic.parse(lang != "en" ? Translator.new(txt, lang).work : txt)
10
20
  end
11
21
 
12
22
  end
@@ -1,4 +1,4 @@
1
- :es:
1
+ es:
2
2
  # Misc
3
3
  manana: morning
4
4
  mañana: morning
@@ -0,0 +1 @@
1
+ es_regex:
@@ -1,4 +1,4 @@
1
- :it:
1
+ it:
2
2
  # Misc
3
3
  mattina: morning
4
4
  pomeriggio: afternoon
@@ -0,0 +1 @@
1
+ it_regex:
@@ -1,4 +1,4 @@
1
- :pt:
1
+ pt:
2
2
  # Misc
3
3
  manhã: morning
4
4
  manha: morning
@@ -0,0 +1,5 @@
1
+ pt_regex:
2
+ !ruby/regexp '/semana qu?e? vem/': next week
3
+ !ruby/regexp '/mes qu?e? vem/': next month
4
+ !ruby/regexp '/ano qu?e? vem/': next year
5
+
@@ -1,20 +1,28 @@
1
1
  module Chronic18n
2
2
 
3
+ DICS = Dir[File.join(File.dirname(__FILE__), 'dics', '') + "*.yml"].reduce({}) do |h,y|
4
+ h.merge YAML.load(File.read(y))
5
+ end
3
6
 
4
7
  class Translator
5
8
 
6
9
  def initialize(txt, lang)
7
- @txt = txt
8
- @dic = load_dic(lang)
9
- end
10
-
11
- def load_dic(lang)
12
- YAML.load(File.read(File.join(File.dirname(__FILE__), "dics", "#{lang}.yml")))[lang]
10
+ @txt, @lang = txt, lang.to_s
13
11
  end
14
12
 
15
13
  def work
16
- @txt.split(/\s/).map { |w| @dic[w] || w }.join(" ")
14
+ # Return the text if we don't have the dictionary
15
+ return @txt unless dic = DICS[@lang]
16
+
17
+ # Regex gsub!
18
+ if rxp = DICS["#{@lang}_regex"]
19
+ rxp.each { |k, v| @txt.gsub!(k, v)}
20
+ end
21
+
22
+ # Match translate
23
+ @txt.split(/\s/).map { |w| dic[w] || w }.join(" ")
17
24
  end
25
+
18
26
  end
19
27
 
20
28
  end
@@ -6,5 +6,12 @@ describe "English" do
6
6
  Chronic18n.parse("sunday").wday.should eql(0)
7
7
  end
8
8
 
9
+ it "should fallback to en fine" do
10
+ Chronic18n.parse("sunday", "en").wday.should eql(0)
11
+ end
12
+
13
+ it "should fallback to en fine" do
14
+ Chronic18n.parse("sunday", :en).wday.should eql(0)
15
+ end
9
16
 
10
17
  end
@@ -1,5 +1,12 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
+ HOUR = HOURS = 3600
4
+ DAY = DAYS = 86400
5
+
6
+ def get_time(t)
7
+ Time.at(Time.now + t)
8
+ end
9
+
3
10
  describe "Portuguese" do
4
11
 
5
12
  it "should parse something" do
@@ -7,7 +14,7 @@ describe "Portuguese" do
7
14
  end
8
15
 
9
16
  it "should parse hour" do
10
- Chronic18n.parse("daqui 7 horas", :pt).hour.should eql(Time.now.hour + 7)
17
+ Chronic18n.parse("daqui 7 horas", :pt).hour.should eql(get_time(7 * HOURS).hour)
11
18
  end
12
19
 
13
20
  it "should parse hour" do
@@ -27,12 +34,16 @@ describe "Portuguese" do
27
34
  Chronic18n.parse("maio 3", :pt).month.should eql(5)
28
35
  end
29
36
 
30
- # it "should parse month" do
31
- # Chronic18n.parse("daqui 3 dias", :pt).day.should eql(Time.now.day + 3)
32
- # end
37
+ it "should parse next week" do
38
+ Chronic18n.parse("mes que vem", :pt).month.should eql((Time.now.month + 1 % 12))
39
+ end
33
40
 
34
- # it "should parse month" do
35
- # Chronic18n.parse("daqui 3 semanas", :pt).day.should eql(Time.now.day + 3)
36
- # end
41
+ it "should parse month" do
42
+ Chronic18n.parse("daqui 3 dias", :pt).day.should eql(get_time(3 * DAYS).day)
43
+ end
44
+
45
+ it "should parse month" do
46
+ Chronic18n.parse("daqui 3 semanas", :pt).day.should eql(get_time(21 * DAYS).day)
47
+ end
37
48
 
38
49
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chronic18n
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 3
10
- version: 0.0.3
8
+ - 4
9
+ version: 0.0.4
11
10
  platform: ruby
12
11
  authors:
13
12
  - Marcos Piccinini
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-06-16 00:00:00 -03:00
17
+ date: 2010-06-21 00:00:00 -03:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 17
30
28
  segments:
31
29
  - 0
32
30
  - 2
@@ -42,7 +40,6 @@ dependencies:
42
40
  requirements:
43
41
  - - ">="
44
42
  - !ruby/object:Gem::Version
45
- hash: 13
46
43
  segments:
47
44
  - 1
48
45
  - 2
@@ -69,8 +66,11 @@ files:
69
66
  - chronic18n.gemspec
70
67
  - lib/chronic18n.rb
71
68
  - lib/chronic18n/dics/es.yml
69
+ - lib/chronic18n/dics/es_regex.yml
72
70
  - lib/chronic18n/dics/it.yml
71
+ - lib/chronic18n/dics/it_regex.yml
73
72
  - lib/chronic18n/dics/pt.yml
73
+ - lib/chronic18n/dics/pt_regex.yml
74
74
  - lib/chronic18n/translator.rb
75
75
  - spec/chronic18n/en_spec.rb
76
76
  - spec/chronic18n/es_spec.rb
@@ -92,7 +92,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - - ">="
94
94
  - !ruby/object:Gem::Version
95
- hash: 3
96
95
  segments:
97
96
  - 0
98
97
  version: "0"
@@ -101,7 +100,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
100
  requirements:
102
101
  - - ">="
103
102
  - !ruby/object:Gem::Version
104
- hash: 3
105
103
  segments:
106
104
  - 0
107
105
  version: "0"