confstruct 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  hash, struct, or block, confstruct aims to provide the flexibility to do things your
5
5
  way, while keeping things simple and intuitive.
6
6
 
7
- [![Build Status](https://secure.travis-ci.org/mbklein/confstruct.png)](http://travis-ci.org/mbklein/confstruct])
7
+ [![Build Status](https://secure.travis-ci.org/mbklein/confstruct.png)](http://travis-ci.org/mbklein/confstruct)
8
8
 
9
9
  ## Usage
10
10
 
@@ -148,6 +148,9 @@ on the same global configuration object.
148
148
 
149
149
  ## Release History
150
150
 
151
+ - <b>v0.1.0</b> - Initial release
152
+ - <b>v0.2.0</b> - Add fallback value to HashWithStructAccess#lookup!, native support for Rails I18n.
153
+
151
154
  ## Contributing to confstruct
152
155
 
153
156
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/lib/confstruct.rb CHANGED
@@ -1,3 +1,8 @@
1
- module Confstruct
2
- VERSION = '0.1.0'
1
+ require 'confstruct/utils'
2
+
3
+ module Confstruct
4
+ autoload :HashWithStructAccess, 'confstruct/hash_with_struct_access'
5
+ autoload :Configuration, 'confstruct/configuration'
6
+
7
+ VERSION = '0.2.0'
3
8
  end
@@ -4,6 +4,18 @@ require 'confstruct/utils'
4
4
  module Confstruct
5
5
  class Deferred < Proc; end
6
6
  def self.deferred &block; Deferred.new(&block); end
7
+
8
+ def self.i18n key=nil, &block
9
+ raise NameError, "I18n handler not loaded" unless Object.const_defined? :I18n # ensure the Rails I18n handler is loaded
10
+ Deferred.new do |hwsa|
11
+ val = block_given? ? eval_or_yield(hwsa, &block) : key
12
+ if val.is_a?(Date) or val.is_a?(Time) or val.is_a?(DateTime)
13
+ ::I18n.localize val
14
+ else
15
+ ::I18n.translate val
16
+ end
17
+ end
18
+ end
7
19
 
8
20
  if ::RUBY_VERSION < '1.9'
9
21
  begin
@@ -94,7 +106,7 @@ module Confstruct
94
106
  end
95
107
 
96
108
  def deferred! &block
97
- Deferred.new(&block)
109
+ Confstruct.deferred(&block)
98
110
  end
99
111
 
100
112
  def has? key_path
@@ -111,6 +123,10 @@ module Confstruct
111
123
  return true
112
124
  end
113
125
 
126
+ def i18n! key=nil, &block
127
+ Confstruct.i18n(key,&block)
128
+ end
129
+
114
130
  def inspect
115
131
  r = self.keys.collect { |k| "#{k.inspect}=>#{self[k].inspect}" }
116
132
  "{#{r.compact.join(', ')}}"
@@ -120,15 +136,15 @@ module Confstruct
120
136
  klazz == @@hash_class or super
121
137
  end
122
138
 
123
- def lookup! key_path
139
+ def lookup! key_path, fallback = nil
124
140
  val = self
125
141
  keys = key_path.split(/\./)
126
142
  keys.each do |key|
127
- return nil if val.nil?
143
+ return fallback if val.nil?
128
144
  if val.respond_to?(:has_key?) and val.has_key?(key.to_sym)
129
145
  val = val[key.to_sym]
130
146
  else
131
- return nil
147
+ return fallback
132
148
  end
133
149
  end
134
150
  return val
@@ -1,5 +1,4 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require 'confstruct/configuration'
3
2
 
4
3
  describe Confstruct::Configuration do
5
4
 
@@ -1,5 +1,4 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require 'confstruct/hash_with_struct_access'
3
2
 
4
3
  describe Confstruct::HashWithStructAccess do
5
4
 
@@ -74,7 +73,10 @@ describe Confstruct::HashWithStructAccess do
74
73
 
75
74
  it "should properly respond to #lookup!" do
76
75
  @hwsa.lookup!('github.url').should == @hash[:github][:url]
76
+ @hwsa.lookup!('github.foo.bar.baz',:default).should == :default
77
77
  @hwsa.lookup!('github.foo.bar.baz').should be_nil
78
+ @hwsa.github.quux = nil
79
+ @hwsa.lookup!('github.quux',:default).should be_nil
78
80
  end
79
81
 
80
82
  it "should provide introspection" do
@@ -148,6 +150,26 @@ describe Confstruct::HashWithStructAccess do
148
150
  it "should fail on other method signatures" do
149
151
  lambda { @hwsa.error(1, 2, 3) }.should raise_error(NoMethodError)
150
152
  end
153
+
154
+ it "should create arrays on the fly" do
155
+ @hwsa.github do
156
+ add_roles!({:jeeves => :valet}, {:wooster => :dolt})
157
+ add_roles! do
158
+ psmith :chum
159
+ end
160
+ end
161
+ @hwsa.github.roles.should == [{:jeeves => :valet}, {:wooster => :dolt}, {:psmith => :chum}]
162
+ @hwsa.github.roles.first.jeeves.should == :valet
163
+ end
164
+
165
+ it "should not allow #add!ing to non-Array types" do
166
+ lambda {
167
+ @hwsa.github do
168
+ add_url! 'https://github.com/mbklein/busted'
169
+ end
170
+ }.should raise_error(TypeError)
171
+ end
172
+
151
173
  end
152
174
 
153
175
  context "Proc values as virtual methods" do
@@ -203,23 +225,19 @@ describe Confstruct::HashWithStructAccess do
203
225
  @hwsa.github.regproc.is_a?(Proc).should be_true
204
226
  end
205
227
 
206
- it "should create arrays on the fly" do
228
+ it "should handle i18n translations" do
229
+ t = Time.now
230
+ I18n = RSpec::Mocks::Mock.new('I18n')
231
+ I18n.should_receive(:translate).with('Hello, World!').and_return('Bonjour, Monde!')
232
+ I18n.should_receive(:localize).with(t).and_return('French Time!')
207
233
  @hwsa.github do
208
- add_roles!({:jeeves => :valet}, {:wooster => :dolt})
209
- add_roles! do
210
- psmith :chum
211
- end
234
+ hello 'Hello, World!'
235
+ time t
236
+ local_hello i18n! { hello }
237
+ local_time i18n! { time }
212
238
  end
213
- @hwsa.github.roles.should == [{:jeeves => :valet}, {:wooster => :dolt}, {:psmith => :chum}]
214
- @hwsa.github.roles.first.jeeves.should == :valet
215
- end
216
-
217
- it "should not allow #add!ing to non-Array types" do
218
- lambda {
219
- @hwsa.github do
220
- add_url! 'https://github.com/mbklein/busted'
221
- end
222
- }.should raise_error(TypeError)
239
+ @hwsa.github.local_hello.should == 'Bonjour, Monde!'
240
+ @hwsa.github.local_time.should == 'French Time!'
223
241
  end
224
242
 
225
243
  end
@@ -1,5 +1,4 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
- require 'confstruct/utils'
3
2
 
4
3
  describe "Kernel.eval_or_yield" do
5
4
  before :all do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: confstruct
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Klein
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-14 00:00:00 Z
18
+ date: 2011-11-18 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake