Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 144a29c01af8f6bd6138a0657785456495af95d4 @stan committed Jun 25, 2010
Showing with 268 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +17 −0 README.rdoc
  3. +51 −0 Rakefile
  4. +1 −0 VERSION
  5. +15 −0 lib/application.yml
  6. +44 −0 lib/ruby_app_config.rb
  7. +17 −0 lib/test.rb
  8. BIN pkg/ruby_app_config-0.0.0.gem
  9. +50 −0 ruby_app_config.gemspec
  10. +15 −0 test/fixtures/sample.yml
  11. +9 −0 test/helper.rb
  12. +29 −0 test/test_ruby_app_config.rb
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Stanislas Mazurek
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, 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,17 @@
+= ruby_app_config
+
+Description goes here.
+
+== Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 2010 Stanislas Mazurek. See LICENSE for details.
@@ -0,0 +1,51 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "ruby_app_config"
+ gem.summary = ""
+ gem.description = ""
+ gem.email = "stan@mazurek.me"
+ gem.homepage = "http://github.com/stan/ruby_app_config"
+ gem.authors = ["stan Mazurek"]
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+end
+
+begin
+ require 'rcov/rcovtask'
+ Rcov::RcovTask.new do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+ end
+rescue LoadError
+ task :rcov do
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
+ end
+end
+
+task :test => :check_dependencies
+
+task :default => :test
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "ruby_app_config #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+0.0.0
@@ -0,0 +1,15 @@
+defaults: &defaults
+ application: AwesomeApp
+ version: <%= 'version'.upcase %>
+ foo:
+ key: bar
+ deep_merge: still_here
+
+development:
+ <<: *defaults
+ foo:
+ key: development_bar
+ another_key: test
+
+production:
+ <<: *defaults
@@ -0,0 +1,44 @@
+require 'ostruct'
+require 'erb'
+require 'yaml'
+
+class Object
+
+ def to_ostruct
+ return case self
+ when Hash
+ self.each do |key, value| self[key] = value.to_ostruct end
+ OpenStruct.new(self)
+ when Array
+ self.map! { |i| i.to_ostruct }
+ else
+ self
+ end
+ end
+
+end
+
+class Hash
+
+ def deep_merge!(second)
+ second.each_pair do |k,v|
+ if self[k].is_a?(Hash) and second[k].is_a?(Hash)
+ self[k].deep_merge!(second[k])
+ else
+ self[k] = second[k]
+ end
+ end
+ end
+
+end
+
+class AppConfig
+
+ def self.create(yaml_file, options = {})
+ yaml_data = YAML::load(ERB.new(IO.read(yaml_file)).result)
+ data = yaml_data["defaults"]
+ data.deep_merge!(yaml_data[options[:environment]]) if options[:environment]
+ data.to_ostruct
+ end
+
+end
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+
+%w(rubygems ruby_app_config yaml ap).each { |lib| require lib}
+
+config = AppConfig.create("application.yml")
+ap config
+puts ""
+
+config = AppConfig.create("application.yml", :environment => 'development')
+ap config
+puts ">> "+config.foo.key
+puts ""
+
+config = AppConfig.create("application.yml", :environment => 'production')
+ap config
+puts ">> "+config.foo.key
+puts ""
Binary file not shown.
@@ -0,0 +1,50 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{ruby_app_config}
+ s.version = "0.0.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["stan Mazurek"]
+ s.date = %q{2010-05-28}
+ s.description = %q{}
+ s.email = %q{stan@mazurek.me}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".document",
+ ".gitignore",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "lib/ruby_app_config.rb",
+ "test/helper.rb",
+ "test/test_ruby_app_config.rb"
+ ]
+ s.homepage = %q{http://github.com/stan/ruby_app_config}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.6}
+ s.summary = %q{}
+ s.test_files = [
+ "test/helper.rb",
+ "test/test_ruby_app_config.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
+
@@ -0,0 +1,15 @@
+defaults: &defaults
+ application: AwesomeApp
+ version: <%= 'version'.upcase %>
+ foo:
+ key: bar
+ deep_merge: still_here
+
+development:
+ <<: *defaults
+ foo:
+ key: development_bar
+ another_key: test
+
+production:
+ <<: *defaults
@@ -0,0 +1,9 @@
+require 'rubygems'
+require 'test/unit'
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'ruby_app_config'
+
+class Test::Unit::TestCase
+end
@@ -0,0 +1,29 @@
+require 'helper'
+class AppConfigTest < Test::Unit::TestCase
+ TEST_FILE = File.join(File.dirname(__FILE__),"fixtures","sample.yml")
+
+ def test_method_accessor_for_top_level_attributes
+ config = AppConfig.create(TEST_FILE)
+ assert_equal "AwesomeApp", config.application
+ end
+
+ def test_read_a_scoped_attribute
+ config = AppConfig.create(TEST_FILE, :environment => 'development')
+ assert_equal "development_bar", config.foo.key
+ end
+
+ def test_attributes_are_inherited
+ config = AppConfig.create(TEST_FILE, :environment => 'production')
+ assert_equal "bar", config.foo.key
+ end
+
+ def test_deep_attributes_are_merged
+ config = AppConfig.create(TEST_FILE, :environment => 'development')
+ assert_equal "still_here", config.foo.deep_merge
+ end
+
+ def test_erb_rendering
+ config = AppConfig.create(TEST_FILE)
+ assert_equal 'VERSION', config.version
+ end
+end

0 comments on commit 144a29c

Please sign in to comment.