Permalink
Browse files

Converting to RSpec 2.0

  • Loading branch information...
jacquescrocker committed Aug 8, 2010
1 parent 66c7034 commit 3221dff29ba20d6f934eb63a4525045599425474
View
@@ -0,0 +1,2 @@
+---
+BUNDLE_DISABLE_SHARED_GEMS: "1"
View
@@ -0,0 +1,7 @@
+source 'http://rubygems.org'
+
+gem "rspec", ">= 2.0.0.beta.19"
+gem 'autotest'
+gem "growl-glue"
+
+gem "ruby-debug"
View
@@ -0,0 +1,30 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ autotest (4.3.2)
+ columnize (0.3.1)
+ diff-lcs (1.1.2)
+ growl-glue (1.0.7)
+ linecache (0.43)
+ rspec (2.0.0.beta.19)
+ rspec-core (= 2.0.0.beta.19)
+ rspec-expectations (= 2.0.0.beta.19)
+ rspec-mocks (= 2.0.0.beta.19)
+ rspec-core (2.0.0.beta.19)
+ rspec-expectations (2.0.0.beta.19)
+ diff-lcs (>= 1.1.2)
+ rspec-mocks (2.0.0.beta.19)
+ ruby-debug (0.10.3)
+ columnize (>= 0.1)
+ ruby-debug-base (~> 0.10.3.0)
+ ruby-debug-base (0.10.3)
+ linecache (>= 0.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ autotest
+ growl-glue
+ rspec (>= 2.0.0.beta.19)
+ ruby-debug
View
@@ -9,7 +9,10 @@ begin
s.homepage = "http://github.com/railsjedi/rails_config"
s.description = "Provides an easy to use Application Configuration object"
s.authors = ["Jacques Crocker"]
- s.files = FileList["[A-Z]*", "{bin,generators,lib,test}/**/*"]
+ s.files = FileList["[A-Z]*", "{bin,generators,lib,spec}/**/*"]
+
+ s.add_development_dependency 'rspec', ">=2.0.0.beta.19"
+
end
Jeweler::GemcutterTasks.new
rescue LoadError
View
@@ -0,0 +1,2 @@
+Autotest.add_discovery { "rspec2" }
+
View
@@ -1,4 +1,5 @@
-require 'rails_config/settings/deep_merge' unless defined?(DeepMerge)
-require 'rails_config/settings/builder'
+require 'rails_config/vendor/deep_merge' unless defined?(DeepMerge)
+require 'pathname'
+require 'rails_config/setting_builder'
require 'rails_config/railtie'
@@ -2,13 +2,12 @@
module RailsConfig
class Railtie < Rails::Railtie
initializer :setup_rails_config do
- ::Settings = RailsConfig::Settings::Builder.load_files(
+ ::Settings = RailsConfig::SettingBuilder.load_files(
:paths => [
Rails.root.join("config", "settings.yml").to_s,
Rails.root.join("config", "settings", "#{Rails.env}.yml").to_s,
Rails.root.join("config", "environments", "#{Rails.env}.yml").to_s
- ],
- :root_path => Rails.root
+ ]
)
end
end
@@ -0,0 +1,61 @@
+require 'ostruct'
+require 'yaml'
+require 'erb'
+
+module RailsConfig
+ module SettingBuilder
+ @@load_paths = []
+
+ # Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
+ # if the first file if they exist in the first file.
+ def self.load_files(*files)
+ config = OpenStruct.new
+
+ @@load_paths = [files].flatten.compact.uniq
+ # add singleton method to our Settings that reloads its settings from the load_paths
+ def config.reload!
+
+ conf = {}
+ SettingBuilder.load_paths.to_a.each do |path|
+ file_conf = YAML.load(ERB.new(IO.read(path.to_s)).result) if path and File.exists?(path.to_s)
+ next unless file_conf
+
+ if conf.size > 0
+ DeepMerge.deep_merge!(file_conf, conf, :preserve_unmergeables => false)
+ else
+ conf = file_conf
+ end
+ end
+
+ # load all the new values into the openstruct
+ marshal_load(RailsConfig::SettingBuilder.convert(conf).marshal_dump)
+
+ return self
+ end
+
+ config.reload!
+ return config
+ end
+
+ def self.load_paths
+ @@load_paths
+ end
+
+ # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
+ def self.convert(h) #:nodoc:
+ s = OpenStruct.new
+ h.each do |k, v|
+ s.new_ostruct_member(k)
+ if v.is_a?(Hash)
+ s.send( (k+'=').to_sym, convert(v))
+ elsif v.is_a?(Array)
+ converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
+ s.send("#{k}=".to_sym, converted_array)
+ else
+ s.send("#{k}=".to_sym, v)
+ end
+ end
+ s
+ end
+ end
+end
@@ -1,170 +0,0 @@
-require 'ostruct'
-require 'yaml'
-require 'erb'
-
-module RailsConfig
- module Settings
- # == Summary
- # This is API documentation, NOT documentation on how to use this plugin. For that, see the README.
- class Builder
- @@load_paths = []
- @@expand_keys = []
- @@root_path = ""
-
- # Create a config object (OpenStruct) from a yaml file. If a second yaml file is given, then the sections of that file will overwrite the sections
- # if the first file if they exist in the first file.
- def self.load_files(options = {})
- config = OpenStruct.new
-
- @@load_paths = [options[:paths]].flatten.compact.uniq
- @@expand_keys = [options[:expand_keys]].flatten.compact.uniq
- @@root_path = options[:root_path]
-
- # add singleton method to our Settings that reloads its settings from the load_paths options
- def config.reload!
-
- conf = {}
- Builder.load_paths.to_a.each do |path|
- file_conf = YAML.load(ERB.new(IO.read(path)).result) if path and File.exists?(path)
- next unless file_conf
-
- if conf.size > 0
- DeepMerge.deep_merge!(file_conf, conf, :preserve_unmergeables => false)
- else
- conf = file_conf
- end
- end
-
- # expand the javascripts config to handle *.* paths
- Builder.expand_keys.to_a.each do |expand_path|
- expand_path = expand_path.to_s
- if conf[expand_path]
- conf[expand_path] = RailsConfig::Settings::Builder.expand(conf[expand_path], "#{Builder.root_path}/public/#{expand_path}")
- end
- end
-
- # load all the new values into the openstruct
- marshal_load(RailsConfig::Settings::Builder.convert(conf).marshal_dump)
-
- return self
- end
-
- config.reload!
- return config
- end
-
- def self.load_paths
- @@load_paths
- end
-
- def self.expand_keys
- @@expand_keys
- end
-
- def self.root_path
- @@root_path
- end
-
- # Recursively converts Hashes to OpenStructs (including Hashes inside Arrays)
- def self.convert(h) #:nodoc:
- s = OpenStruct.new
- h.each do |k, v|
- s.new_ostruct_member(k)
- if v.is_a?(Hash)
- s.send( (k+'=').to_sym, convert(v))
- elsif v.is_a?(Array)
- converted_array = v.collect { |e| e.instance_of?(Hash) ? convert(e) : e }
- s.send("#{k}=".to_sym, converted_array)
- else
- s.send("#{k}=".to_sym, v)
- end
- end
- s
- end
-
- # expand a config val
- def self.expand(config, base_path)
- case config.class.to_s
- when "Hash"
- return expand_hash(config, base_path)
- when "Array"
- return expand_array(config, base_path)
- when "String"
- return expand_string(config, base_path)
- end
- return config
- end
-
- # expand a string and returns a list
- def self.expand_string(config, base_path)
- # puts "Expanding String: #{config.inspect}"
- if config.include?("*")
- results = Dir["#{base_path}/#{config}"].map{|i| i.to_s.gsub("#{base_path}/", "") }
-
- # puts "EXPANDED PATH: #{base_path}/#{config}"
- # puts results.inspect
- return results
- else
- return config
- end
- end
-
- # expand a hash by cycling throw all the hash values
- def self.expand_hash(config, base_path)
- # puts "Expanding Hash: #{config.inspect}"
- new_config = {}
- config.each do |key, val|
- new_config[key] = expand(val, base_path)
- end
- return new_config
- end
-
- # expand an array by cycling through all the values
- def self.expand_array(config, base_path)
- # puts "Expanding Array: #{config.inspect}"
- new_config = []
- config.each do |val|
- new_val = expand(val, base_path)
- if new_val.is_a?(Array)
- new_val.each do |inner|
- new_config << inner
- end
- else
- new_config << new_val
- end
- end
- return new_config.uniq
- end
-
- # Cycles through the array of single element hashes
- # and deep merges any duplicates it finds
- #
- # This is needed so you can define stylesheet keys
- # in multiple config files
- def self.merge_assets(list)
- assets = Array(list).map do |i|
- if i.is_a?(OpenStruct)
- i.marshal_dump
- else
- i
- end
- end
-
- # filter out the duplicate single hash keys
- hash_keys = assets.select{|i| i.is_a?(Hash) and i.keys.size == 1}.group_by{|i| i.keys[0]}
- hash_keys.each do |key, value|
- if Array(value).size > 1
- merged = value.inject({}){|merged, v| DeepMerge.deep_merge!(v,merged)}
- value[0].replace(merged)
- value[1..-1].each do |v|
- v.clear
- end
- end
- end
-
- assets.select{|i| !i.blank? }
- end
-
- end
- end
-end
Oops, something went wrong.

0 comments on commit 3221dff

Please sign in to comment.