configarrr 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,41 @@
1
+ = configarrr
2
+
3
+ == Description:
4
+
5
+ A simple interface for providin' configuration. Not all o' me own creations. Parts borrowed, see Acknowledgements.
6
+
7
+ == Usage:
8
+
9
+ config = Configarrr::Simple.configure do
10
+ set :key, :value
11
+ set :hash => { :other_key => :other_value }
12
+ end
13
+
14
+ >> config.key
15
+ => :value
16
+ >> config.hash[:other_key]
17
+ => :other_value
18
+
19
+ == Acknowledgements:
20
+
21
+ Sinatra - http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb - borrowed set and metadef.
22
+
23
+ == License:
24
+
25
+ (The MIT License)
26
+
27
+ Copyright (c) 2009 Dylan Egan
28
+
29
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
30
+ this software and associated documentation files (the 'Software'), to deal in
31
+ the Software without restriction, including without limitation the rights to use,
32
+ copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
33
+ Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
34
+
35
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
38
+ INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
39
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
40
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
41
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "configarrr"
5
+ gemspec.summary = "Pirates way of configuring, maties!"
6
+ gemspec.description = "Actually it's not. It's just a way of handling configuration."
7
+ gemspec.email = "dylanegan@gmail.com"
8
+ gemspec.homepage = "http://github.com/abcde/configarrr"
9
+ gemspec.authors = ["Dylan Egan"]
10
+ gemspec.files = %w(README.markdown Rakefile VERSION) + Dir.glob("{lib,spec}/**/*")
11
+ end
12
+ rescue LoadError
13
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
14
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,51 @@
1
+ module Configarrr
2
+ class Base
3
+ def initialize(options = {})
4
+ @keys = []
5
+ parse_options(options)
6
+ end
7
+
8
+ def self.configure(options = {}, &block)
9
+ config = new(options)
10
+ config.configure(&block)
11
+ config
12
+ end
13
+
14
+ def configure(&block)
15
+ instance_eval(&block)
16
+ end
17
+
18
+ def set(key, value=self)
19
+ if value.kind_of?(Proc)
20
+ metadef(key, &value)
21
+ metadef("#{key}?") { !!__send__(key) }
22
+ metadef("#{key}=") { |val| set(key, Proc.new{val}) }
23
+ @keys << key
24
+ elsif value == self && key.is_a?(Hash)
25
+ key.to_hash.each { |k,v| set(k, v) }
26
+ elsif respond_to?("#{key}=")
27
+ __send__ "#{key}=", value
28
+ @keys << key
29
+ else
30
+ set key, Proc.new{value}
31
+ end
32
+ self
33
+ end
34
+
35
+ def to_hash
36
+ @keys.inject({}) do |hash, key|
37
+ hash[key] = __send__ "#{key}"
38
+ hash
39
+ end
40
+ end
41
+
42
+ private
43
+ def parse_options(options)
44
+ end
45
+
46
+ def metadef(message, &block)
47
+ (class << self; self; end).
48
+ send :define_method, message, &block
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,4 @@
1
+ module Configarrr
2
+ class Simple < Base
3
+ end
4
+ end
@@ -0,0 +1,46 @@
1
+ require 'yaml'
2
+
3
+ module Configarrr
4
+ class YAML < Base
5
+ def initialize(options = {})
6
+ raise ArgumentError, "Please provide a YAML file location." unless options[:file]
7
+ super(options)
8
+ ensure_file_exists
9
+ @yaml = ::YAML.load_file(@file) || {}
10
+ set_yaml
11
+ end
12
+
13
+ def defaults
14
+ {}
15
+ end
16
+
17
+ def set_yaml
18
+ if @parent
19
+ @yaml.has_key?(@parent) ? set(defaults.merge(@yaml[@parent])) : raise(Configarrr::OptionError, "Please provide a valid parent value. #{@parent} does not exist.")
20
+ else
21
+ set defaults.merge(@yaml)
22
+ end
23
+ end
24
+
25
+ def save
26
+ ::File.open(@file, "w") { |file| file << self.to_hash.to_yaml }
27
+ end
28
+
29
+ def to_hash
30
+ @parent ? @yaml.merge({@parent => super}) : super
31
+ end
32
+
33
+ private
34
+ def parse_options(options)
35
+ @file = File.expand_path(options.delete(:file))
36
+ @parent = options.delete(:parent)
37
+ super
38
+ end
39
+
40
+ def ensure_file_exists
41
+ unless ::File.exists?(@file)
42
+ ::File.open(@file, "w") { |file| file << {}.to_yaml }
43
+ end
44
+ end
45
+ end
46
+ end
data/lib/configarrr.rb ADDED
@@ -0,0 +1,7 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ module Configarrr
4
+ class OptionError < StandardError; end
5
+ end
6
+
7
+ %w( base simple yaml ).each { |lib| require "configarrr/#{lib}" }
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ class MyConfig < Configarrr::Base
4
+ attr_accessor :my_option
5
+ end
6
+
7
+ describe Configarrr::Base do
8
+ before do
9
+ @config = Configarrr::Base.new
10
+ end
11
+
12
+ context "when inherited" do
13
+ before do
14
+ @my_config = MyConfig.configure do
15
+ set :my_option, :my_value
16
+ end
17
+ end
18
+
19
+ it "should work with defined attributes in child class" do
20
+ @my_config.my_option.should == :my_value
21
+ end
22
+
23
+ it "should track defined attributes in child class" do
24
+ @my_config.to_hash.keys.should include(:my_option)
25
+ end
26
+ end
27
+
28
+ it_should_behave_like "a Configarrr implementation"
29
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ class YAMLWithDefaults < Configarrr::YAML
4
+ def defaults
5
+ { 'first_key' => 1234, 'third_key' => 1234 }
6
+ end
7
+ end
8
+
9
+ describe Configarrr::YAML do
10
+ before do
11
+ @config = Configarrr::YAML.new(:file => SPEC_DIR + '/fixtures/config.yml')
12
+ end
13
+
14
+ context "initialising" do
15
+ it "raises without a file being provided" do
16
+ lambda { Configarrr::YAML.new }.should raise_error(ArgumentError, "Please provide a YAML file location.")
17
+ end
18
+
19
+ it "creates the file if necessary" do
20
+ file = SPEC_DIR + '/fixtures/config.created.yml'
21
+ FileUtils.rm(file) if File.exists?(file)
22
+ Configarrr::YAML.new(:file => file)
23
+ File.exists?(file).should be_true
24
+ end
25
+
26
+ it "sets options from the file" do
27
+ @config.first_key.should == "first_value"
28
+ end
29
+ end
30
+
31
+ context "choosing a parent scalar" do
32
+ before do
33
+ @yaml_parent = Configarrr::YAML.new(:file => SPEC_DIR + '/fixtures/config.yml', :parent => 'parent')
34
+ end
35
+
36
+ it "should return the parents children" do
37
+ @yaml_parent.first_key.should == "first_parent_value"
38
+ end
39
+
40
+ it "should save all of the yaml, not just the parent scalar" do
41
+ @yaml_parent.to_hash.should == @config.to_hash
42
+ end
43
+
44
+ context "when it doesn't exist" do
45
+ it "should raise an Configarrr::OptionError" do
46
+ lambda { Configarrr::YAML.new(:file => SPEC_DIR + '/fixtures/config.yml', :parent => 'non_existant_parent') }.should raise_error(Configarrr::OptionError, "Please provide a valid parent value. non_existant_parent does not exist.")
47
+ end
48
+ end
49
+ end
50
+
51
+ context "providing default settings" do
52
+ before do
53
+ @yaml_with_defaults = YAMLWithDefaults.new(:file => SPEC_DIR + '/fixtures/config.yml')
54
+ end
55
+
56
+ it "should not override provided settings" do
57
+ @yaml_with_defaults.first_key.should_not == 1234
58
+ end
59
+
60
+ it "should provide setting if not provided" do
61
+ @yaml_with_defaults.third_key.should == 1234
62
+ end
63
+ end
64
+
65
+ it_should_behave_like "a Configarrr implementation"
66
+
67
+ after :all do
68
+ %w( created parent ).each do |file|
69
+ File.unlink(SPEC_DIR + "/fixtures/config.#{file}.yml") if File.exists?(SPEC_DIR + "/fixtures/config.#{file}.yml")
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ first_key: first_value
2
+ parent:
3
+ first_key: first_parent_value
data/spec/shared.rb ADDED
@@ -0,0 +1,13 @@
1
+ shared_examples_for "a Configarrr implementation" do
2
+ context "configure" do
3
+ before :each do
4
+ @config.configure do
5
+ set :key, :value
6
+ end
7
+ end
8
+
9
+ it "should allow you to set values" do
10
+ @config.key.should == :value
11
+ end
12
+ end
13
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ -f o -c
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ SPEC_DIR = File.dirname(__FILE__) unless defined? SPEC_DIR
5
+ $:<< SPEC_DIR
6
+
7
+ require File.join(SPEC_DIR, '..', 'lib', 'configarrr')
8
+
9
+ require File.dirname(__FILE__) + '/shared'
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: configarrr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dylan Egan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-30 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Actually it's not. It's just a way of handling configuration.
17
+ email: dylanegan@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.markdown
24
+ files:
25
+ - README.markdown
26
+ - Rakefile
27
+ - VERSION
28
+ - lib/configarrr.rb
29
+ - lib/configarrr/base.rb
30
+ - lib/configarrr/simple.rb
31
+ - lib/configarrr/yaml.rb
32
+ - spec/configarrr/base_spec.rb
33
+ - spec/configarrr/yaml_spec.rb
34
+ - spec/fixtures/config.yml
35
+ - spec/shared.rb
36
+ - spec/spec.opts
37
+ - spec/spec_helper.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/abcde/configarrr
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Pirates way of configuring, maties!
66
+ test_files:
67
+ - spec/configarrr/base_spec.rb
68
+ - spec/configarrr/yaml_spec.rb
69
+ - spec/shared.rb
70
+ - spec/spec_helper.rb