Skip to content

Commit

Permalink
* Define methods during method_missing instead of during initializati…
Browse files Browse the repository at this point in the history
…on. Allows for modification on the fly.
  • Loading branch information
binarylogic committed Aug 22, 2009
1 parent dbb2a95 commit 606269d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 30 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
32 changes: 6 additions & 26 deletions lib/settingslogic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 0 additions & 4 deletions spec/settingslogic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 606269d

Please sign in to comment.