Created
January 30, 2011 04:26
-
-
Save jwreagor/802534 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Super tiny block configuration class, which is | |
# a great example of nested instance evaluation. | |
# All using a single class. | |
# | |
# Inspired by ... | |
# http://github.com/ahoward/configuration/blob/master/lib/configuration.rb | |
# | |
# Updated: More functionality, less lines of code (gem soon) | |
# | |
class ConfigSet | |
Index = Hash.new | |
def initialize(name) | |
@name = name | |
end | |
def yielding(&block) | |
@yielding = true | |
yield | |
@yielding = false | |
end | |
def self.for(name, &block) | |
config = Index[name] ||= ConfigSet.new(name) | |
block && config.yielding { config.instance_eval &block } | |
config | |
end | |
def method_missing(name, *args, &block) | |
case | |
when name.to_s =~ /\=/ | |
instance_variable_set("@#{name.to_s.gsub(/\=/, '')}", args[0]) | |
when @yielding | |
value = block ? ConfigSet.for(name, &block) : args[0] | |
instance_variable_set "@#{name}", value | |
when !args[0] | |
instance_variable_get "@#{name}" | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'bacon' | |
$:.unshift File.expand_path(File.dirname(__FILE__)) | |
require 'config' | |
require 'pathname' | |
$:.unshift Pathname(__FILE__).dirname.expand_path.to_s | |
require 'spec_helper.rb' | |
describe ConfigSet, 'storing values' do | |
it 'should set values in a block' do | |
config = ConfigSet.for :app do | |
username 'test123' | |
password 'pen159' | |
end | |
config.username.should.equal 'test123' | |
end | |
it 'should set values in nested blocks' do | |
config = ConfigSet.for :app do | |
username 'test123' | |
a { ho { sucka 'bitch' } } | |
end | |
config.a.ho.sucka.should.equal 'bitch' | |
end | |
it 'should load the same config set twice' do | |
ConfigSet.for :app do | |
something 'apple' | |
end | |
ConfigSet.for(:app).something.should.equal 'apple' | |
end | |
it 'should set a value through its setter' do | |
config = ConfigSet.for :app do | |
username 'jacob' | |
end | |
config.username = 'else' | |
config.username.should.equal 'else' | |
end | |
it 'should set a nested value through its setter' do | |
config = ConfigSet.for :app do | |
user { bacon { name 'jacob' } } | |
end | |
config.user.bacon.name = 'else' | |
config.user.bacon.name.should.equal 'else' | |
end | |
end | |
puts Bacon.summary_on_exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment