Skip to content

Commit

Permalink
specs
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Jun 14, 2010
1 parent 4b2bdcb commit b7e0b45
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 24 deletions.
8 changes: 2 additions & 6 deletions lib/chronic18n.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
require "chronic"
require "yaml"

require "chronic18n/translator"

module Chronic18n

def self.parse(txt, lang = nil)
Chronic.parse(lang ? const_get(lang.capitalize).translate(txt) : txt)
end

def self.load_dic(lang)
YAML.load(File.read(File.join(File.dirname(__FILE__), "chronic18n", "dics", "#{lang}.yml")))[lang]
Chronic.parse(lang ? Translator.new(txt, lang).work : txt)
end

end

require "chronic18n/pt"
12 changes: 0 additions & 12 deletions lib/chronic18n/pt.rb

This file was deleted.

20 changes: 20 additions & 0 deletions lib/chronic18n/translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Chronic18n


class Translator

def initialize(txt, lang)
@txt = txt
@dic = load_dic(lang)
end

def load_dic(lang)
YAML.load(File.read(File.join(File.dirname(__FILE__), "dics", "#{lang}.yml")))[lang]
end

def work
@txt.split(/\s/).map { |w| @dic[w] || w }.join(" ")
end
end

end
30 changes: 30 additions & 0 deletions spec/chronic18n/es_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "Spanish" do

it "should parse something" do
Chronic18n.parse("lunes", :es).should be_a Time
end

it "should parse hour" do
Chronic18n.parse("en 7 horas", :es).hour.should eql((Time.now.hour + 7) % 24)
end

it "should parse hour" do
Chronic18n.parse("en 7 minutos", :es).min.should eql((Time.now.min + 7) % 60)
end

it "should parse sunday" do
Chronic18n.parse("domingo", :es).wday.should eql(0)
end

it "should parse monday" do
Chronic18n.parse("martes", :es).wday.should eql(2)
end

it "should parse month" do
Chronic18n.parse("mayo 3", :es).month.should eql(5)
end


end
30 changes: 30 additions & 0 deletions spec/chronic18n/it_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe "Spanish" do

it "should parse something" do
Chronic18n.parse("sabato", :it).should be_a Time
end

it "should parse hour" do
Chronic18n.parse("in 7 oras", :it).hour.should eql((Time.now.hour + 7) % 24)
end

it "should parse hour" do
Chronic18n.parse("in 7 minutos", :it).min.should eql((Time.now.min + 7) % 60)
end

it "should parse sunday" do
Chronic18n.parse("domenica", :it).wday.should eql(0)
end

it "should parse monday" do
Chronic18n.parse("mercoledi", :it).wday.should eql(3)
end

it "should parse month" do
Chronic18n.parse("maggio", :it).month.should eql(5)
end


end
12 changes: 6 additions & 6 deletions spec/chronic18n/pt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
Chronic18n.parse("maio 3", :pt).month.should eql(5)
end

it "should parse month" do
Chronic18n.parse("daqui 3 dias", :pt).day.should eql(Time.now.day + 3)
end
# it "should parse month" do
# Chronic18n.parse("daqui 3 dias", :pt).day.should eql(Time.now.day + 3)
# end

it "should parse month" do
Chronic18n.parse("daqui 3 semanas", :pt).day.should eql(Time.now.day + 3)
end
# it "should parse month" do
# Chronic18n.parse("daqui 3 semanas", :pt).day.should eql(Time.now.day + 3)
# end

end

0 comments on commit b7e0b45

Please sign in to comment.