Permalink
Browse files

more tests; have pinpointed collisions to top-level settings only

  • Loading branch information...
1 parent 04fddca commit 805df6864f1fa45f023cc2594f92a82517752d5d Nate Wiger committed with Feb 11, 2010
Showing with 21 additions and 6 deletions.
  1. +4 −1 lib/settingslogic.rb
  2. +3 −0 spec/settings.rb
  3. +4 −1 spec/settings.yml
  4. +8 −2 spec/settingslogic_spec.rb
  5. +2 −2 spec/spec_helper.rb
View
5 lib/settingslogic.rb
@@ -47,7 +47,7 @@ def []=(key, val)
instance.store(key.to_s, val)
instance.create_accessor_for(key.to_s, val)
end
-
+
def load!
instance
true
@@ -79,6 +79,7 @@ def method_missing(name, *args, &block)
# if you are using this in rails. If you pass a string it should be an absolute path to your settings file.
# Then you can pass a hash, and it just allows you to access the hash via methods.
def initialize(hash_or_file = self.class.source, section = nil)
+ #puts "new! #{hash_or_file}"
case hash_or_file
when Hash
self.replace hash_or_file
@@ -120,6 +121,7 @@ def []=(key,val)
# rather than the app_yml['deploy_to'] hash. Jeezus.
def create_accessors!
self.each do |key,val|
+ #puts "accessor_for: #{key}"
create_accessor_for(key)
end
end
@@ -132,6 +134,7 @@ def create_accessor_for(key, val=nil)
instance_variable_set("@#{key}", val)
self.class.class_eval <<-EndEval
def #{key}
+ #puts 'class_eval: #{key}'
return @#{key} if @#{key}
raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}'
value = fetch('#{key}')
View
3 spec/settings.rb
@@ -1,3 +1,6 @@
class Settings < Settingslogic
source "#{File.dirname(__FILE__)}/settings.yml"
+end
+
+class SettingsInst < Settingslogic
end
View
5 spec/settings.yml
@@ -16,4 +16,7 @@ language:
paradigm: object oriented
collides:
- does: not
+ does: not
+nested:
+ collides:
+ does: not either
View
10 spec/settingslogic_spec.rb
@@ -20,7 +20,7 @@
it "should enable erb" do
Settings.setting3.should == 25
end
-
+
it "should namespace settings" do
Settings2.setting1_child.should == "saweet"
Settings2.deep.another.should == "my value"
@@ -37,6 +37,7 @@
end
it "should not collide with global methods" do
+ Settings3.nested.collides.does.should == 'not either'
Settings3.collides.does.should == 'not'
end
@@ -88,11 +89,16 @@
Settings.language.erlang.paradigm.should == 'functional'
end
- it "should handle badly-named settings" do
+ it "should handle oddly-named settings" do
Settings.language['some-dash-setting#'] = 'dashtastic'
Settings.language['some-dash-setting#'].should == 'dashtastic'
end
+ it "should support instance usage as well" do
+ settings = SettingsInst.new(Settings.source)
+ settings.setting1.setting1_child.should == "saweet"
+ end
+
it "should be able to get() a key with dot.notation" do
Settings.get('setting1.setting1_child').should == "saweet"
Settings.get('setting1.deep.another').should == "my value"
View
4 spec/spec_helper.rb
@@ -10,8 +10,8 @@
require 'settings3'
# Needed to test Settings3
-def collides
- @collides = 'collision'
+Object.send :define_method, 'collides' do
+ 'collision'
end
Spec::Runner.configure do |config|

0 comments on commit 805df68

Please sign in to comment.