Permalink
Browse files

* Define methods during method_missing instead of during initializati…

…on. Allows for modification on the fly.
  • Loading branch information...
binarylogic committed Aug 22, 2009
1 parent dbb2a95 commit 606269dfc1209c1f60961816e863b16dd6267a12
Showing with 10 additions and 30 deletions.
  1. +4 −0 CHANGELOG.rdoc
  2. +6 −26 lib/settingslogic.rb
  3. +0 −4 spec/settingslogic_spec.rb
View
@@ -1,3 +1,7 @@
+== 2.0.2 released 2009-08-22
+
+* Define methods during method_missing instead of during initialization. Allows for modification on the fly.
+
== 2.0.0 released 2009-08-22
* Less magic, instead of automatically defining a Settings constant, you should define your own constant. See the readme for an example.
View
@@ -3,8 +3,6 @@
# A simple settings solution using a YAML file. See README for more information.
class Settingslogic < Hash
- class UndefinedSetting < StandardError; end
-
class << self
def name # :nodoc:
instance.key?("name") ? instance.name : super
@@ -54,34 +52,16 @@ def initialize(hash_or_file = self.class.source)
hash = hash[self.class.namespace] if self.class.namespace
self.update hash
end
-
- define_settings!
end
private
def method_missing(name, *args, &block)
- raise UndefinedSetting.new("The '#{name}' was not found in your configuration file: #{self.class.source}")
- end
-
- def define_settings!
- self.each do |key, value|
- case value
- when Hash
- instance_eval <<-"end_eval", __FILE__, __LINE__
- def #{key}
- @#{key} ||= self.class.new(self[#{key.inspect}])
- end
- end_eval
- else
- instance_eval <<-"end_eval", __FILE__, __LINE__
- def #{key}
- @#{key} ||= self[#{key.inspect}]
- end
- def #{key}=(value)
- @#{key} = value
- end
- end_eval
- end
+ if key?(name.to_s)
+ value = self[name.to_s].is_a?(Hash) ? self.class.new(self[name.to_s]) : self[name.to_s]
+ self.class.send(:define_method, name) { value }
+ send(name)
+ else
+ super
end
end
end
@@ -17,10 +17,6 @@
Settings.setting3.should == 25
end
- it "should raise an error for unfound settings" do
- lambda { Settings.undefined }.should raise_error(Settingslogic::UndefinedSetting)
- end
-
it "should namespace settings" do
Settings2.setting1_child.should == "saweet"
end

0 comments on commit 606269d

Please sign in to comment.