abcde-configarrr 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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.
@@ -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.0
@@ -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,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,42 @@
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 set_yaml
14
+ if @parent
15
+ @yaml.has_key?(@parent) ? set(@yaml[@parent]) : raise(Configarrr::OptionError, "Please provide a valid parent value. #{@parent} does not exist.")
16
+ else
17
+ set @yaml
18
+ end
19
+ end
20
+
21
+ def save
22
+ ::File.open(@file, "w") { |file| file << self.to_hash.to_yaml }
23
+ end
24
+
25
+ def to_hash
26
+ @parent ? @yaml.merge({@parent => super}) : super
27
+ end
28
+
29
+ private
30
+ def parse_options(options)
31
+ @file = File.expand_path(options.delete(:file))
32
+ @parent = options.delete(:parent)
33
+ super
34
+ end
35
+
36
+ def ensure_file_exists
37
+ unless ::File.exists?(@file)
38
+ ::File.open(@file, "w") { |file| file << {}.to_yaml }
39
+ end
40
+ end
41
+ end
42
+ end
@@ -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,52 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Configarrr::YAML do
4
+ before do
5
+ @config = Configarrr::YAML.new(:file => SPEC_DIR + '/fixtures/config.yml')
6
+ end
7
+
8
+ context "initialising" do
9
+ it "raises without a file being provided" do
10
+ lambda { Configarrr::YAML.new }.should raise_error(ArgumentError, "Please provide a YAML file location.")
11
+ end
12
+
13
+ it "creates the file if necessary" do
14
+ file = SPEC_DIR + '/fixtures/config.created.yml'
15
+ FileUtils.rm(file) if File.exists?(file)
16
+ Configarrr::YAML.new(:file => file)
17
+ File.exists?(file).should be_true
18
+ end
19
+
20
+ it "sets options from the file" do
21
+ @config.first_key.should == "first_value"
22
+ end
23
+ end
24
+
25
+ context "choosing a parent scalar" do
26
+ before do
27
+ @yaml_parent = Configarrr::YAML.new(:file => SPEC_DIR + '/fixtures/config.yml', :parent => 'parent')
28
+ end
29
+
30
+ it "should return the parents children" do
31
+ @yaml_parent.first_key.should == "first_parent_value"
32
+ end
33
+
34
+ it "should save all of the yaml, not just the parent scalar" do
35
+ @yaml_parent.to_hash.should == @config.to_hash
36
+ end
37
+
38
+ context "when it doesn't exist" do
39
+ it "should raise an Configarrr::OptionError" do
40
+ 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.")
41
+ end
42
+ end
43
+ end
44
+
45
+ it_should_behave_like "a Configarrr implementation"
46
+
47
+ after :all do
48
+ %w( created parent ).each do |file|
49
+ File.unlink(SPEC_DIR + "/fixtures/config.#{file}.yml") if File.exists?(SPEC_DIR + "/fixtures/config.#{file}.yml")
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ first_key: first_value
2
+ parent:
3
+ first_key: first_parent_value
@@ -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
@@ -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,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abcde-configarrr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dylan Egan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-20 00:00:00 -07: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: false
39
+ homepage: http://github.com/abcde/configarrr
40
+ licenses:
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Pirates way of configuring, maties!
65
+ test_files:
66
+ - spec/configarrr/base_spec.rb
67
+ - spec/configarrr/yaml_spec.rb
68
+ - spec/shared.rb
69
+ - spec/spec_helper.rb