Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a special type of Deferred to work with Rails I18n

  • Loading branch information...
commit f96832df13f9181dcd9044dbee5aa588af7b22eb 1 parent 3008d34
@mbklein mbklein authored
View
18 lib/confstruct/hash_with_struct_access.rb
@@ -4,6 +4,18 @@
module Confstruct
class Deferred < Proc; end
def self.deferred &block; Deferred.new(&block); end
+
+ def self.i18n key=nil, &block
+ raise NameError, "I18n handler not loaded" unless Object.const_defined? :I18n # ensure the Rails I18n handler is loaded
+ Deferred.new do |hwsa|
+ val = block_given? ? eval_or_yield(hwsa, &block) : key
+ if val.is_a?(Date) or val.is_a?(Time) or val.is_a?(DateTime)
+ ::I18n.localize val
+ else
+ ::I18n.translate val
+ end
+ end
+ end
if ::RUBY_VERSION < '1.9'
begin
@@ -94,7 +106,7 @@ def deep_merge! hash
end
def deferred! &block
- Deferred.new(&block)
+ Confstruct.deferred(&block)
end
def has? key_path
@@ -111,6 +123,10 @@ def has? key_path
return true
end
+ def i18n! key=nil, &block
+ Confstruct.i18n(key,&block)
+ end
+
def inspect
r = self.keys.collect { |k| "#{k.inspect}=>#{self[k].inspect}" }
"{#{r.compact.join(', ')}}"
View
46 spec/confstruct/hash_with_struct_access_spec.rb
@@ -150,6 +150,26 @@
it "should fail on other method signatures" do
lambda { @hwsa.error(1, 2, 3) }.should raise_error(NoMethodError)
end
+
+ it "should create arrays on the fly" do
+ @hwsa.github do
+ add_roles!({:jeeves => :valet}, {:wooster => :dolt})
+ add_roles! do
+ psmith :chum
+ end
+ end
+ @hwsa.github.roles.should == [{:jeeves => :valet}, {:wooster => :dolt}, {:psmith => :chum}]
+ @hwsa.github.roles.first.jeeves.should == :valet
+ end
+
+ it "should not allow #add!ing to non-Array types" do
+ lambda {
+ @hwsa.github do
+ add_url! 'https://github.com/mbklein/busted'
+ end
+ }.should raise_error(TypeError)
+ end
+
end
context "Proc values as virtual methods" do
@@ -205,23 +225,19 @@
@hwsa.github.regproc.is_a?(Proc).should be_true
end
- it "should create arrays on the fly" do
+ it "should handle i18n translations" do
+ t = Time.now
+ I18n = RSpec::Mocks::Mock.new('I18n')
+ I18n.should_receive(:translate).with('Hello, World!').and_return('Bonjour, Monde!')
+ I18n.should_receive(:localize).with(t).and_return('French Time!')
@hwsa.github do
- add_roles!({:jeeves => :valet}, {:wooster => :dolt})
- add_roles! do
- psmith :chum
- end
+ hello 'Hello, World!'
+ time t
+ local_hello i18n! { hello }
+ local_time i18n! { time }
end
- @hwsa.github.roles.should == [{:jeeves => :valet}, {:wooster => :dolt}, {:psmith => :chum}]
- @hwsa.github.roles.first.jeeves.should == :valet
- end
-
- it "should not allow #add!ing to non-Array types" do
- lambda {
- @hwsa.github do
- add_url! 'https://github.com/mbklein/busted'
- end
- }.should raise_error(TypeError)
+ @hwsa.github.local_hello.should == 'Bonjour, Monde!'
+ @hwsa.github.local_time.should == 'French Time!'
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.