Permalink
Browse files

Moving all gem-related modules under one module

  • Loading branch information...
1 parent eca33a4 commit 74e0e654a4c906c5d691a4df4939f6b386c408ff @winton committed Dec 29, 2010
View
@@ -1,16 +1,16 @@
# -*- encoding: utf-8 -*-
Gem::Specification.new do |s|
- GemTemplate::Gemspec.data.each do |key, value|
+ GemTemplate::Gems.gemspec.hash.each do |key, value|
unless %w(dependencies development_dependencies).include?(key)
s.send "#{key}=", value
end
end
- GemTemplate::Gemspec.dependencies.each do |g|
+ GemTemplate::Gems.gemspec.dependencies.each do |g|
s.add_dependency g.to_s, GemTemplate::Gems.versions[g]
end
- GemTemplate::Gemspec.development_dependencies.each do |g|
+ GemTemplate::Gems.gemspec.development_dependencies.each do |g|
s.add_development_dependency g.to_s, GemTemplate::Gems.versions[g]
end
View
@@ -1,24 +1,37 @@
unless defined?(GemTemplate::Gems)
- require "#{File.dirname(__FILE__)}/gemsets"
+ require 'ostruct'
+ require 'yaml'
module GemTemplate
- class Gems
+ module Gems
class <<self
- attr_accessor :testing, :versions, :warn
+ attr_accessor :configs, :gemset, :gemsets, :gemspec, :testing, :versions, :warn
+ Gems.configs = [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ]
Gems.testing = false
Gems.warn = true
+ class Gemspec
+ attr_accessor :hash
+
+ def initialize(hash)
+ @hash = hash
+ @hash.each do |key, value|
+ self.class.send(:define_method, key) { value }
+ end
+ end
+ end
+
def activate(*gems)
begin
require 'rubygems' if !defined?(::Gem) || @testing
rescue LoadError
puts "rubygems library could not be required" if @warn
end
- Gemsets.gemset = :default unless defined?(@gemset)
+ self.gemset = :default unless defined?(@gemset) && @gemset
gems.flatten.collect(&:to_sym).each do |name|
version = @versions[name]
@@ -29,6 +42,66 @@ def activate(*gems)
end
end
end
+
+ def gemset=(gemset)
+ if gemset
+ @gemset = gemset.to_sym
+
+ @gemsets = @configs.reverse.collect { |config|
+ if config.is_a?(::String)
+ YAML::load(File.read(config)) rescue {}
+ elsif config.is_a?(::Hash)
+ config
+ end
+ }.inject({}) do |hash, config|
+ deep_merge(hash, symbolize_keys(config))
+ end
+
+ @versions = @gemsets[@gemspec.name.to_sym].inject({}) do |hash, (key, value)|
+ if value.is_a?(::String)
+ hash[key] = value
+ elsif value.is_a?(::Hash) && key == @gemset
+ value.each { |k, v| hash[k] = v }
+ end
+ hash
+ end
+ else
+ @gemset = nil
+ @gemsets = nil
+ @versions = nil
+ end
+ end
+
+ def reload_gemspec
+ data =
+ YAML::load(
+ File.read(
+ "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml"
+ )
+ ) rescue {}
+
+ @gemspec = Gemspec.new(data)
+ end
+
+ Gems.reload_gemspec
+
+ private
+
+ def deep_merge(first, second)
+ merger = lambda do |key, v1, v2|
+ Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
+ end
+ first.merge(second, &merger)
+ end
+
+ def symbolize_keys(hash)
+ return {} unless hash.is_a?(::Hash)
+ hash.inject({}) do |options, (key, value)|
+ value = symbolize_keys(value) if value.is_a?(::Hash)
+ options[(key.to_sym rescue key) || key] = value
+ options
+ end
+ end
end
end
end
@@ -1,65 +0,0 @@
-unless defined?(GemTemplate::Gemsets)
-
- require "#{File.dirname(__FILE__)}/gems"
- require "#{File.dirname(__FILE__)}/gemspec"
-
- require 'yaml'
-
- module GemTemplate
- class Gemsets
- class <<self
-
- attr_accessor :configs, :gemset, :gemsets
-
- Gemsets.configs = [ "#{File.expand_path('../../../', __FILE__)}/config/gemsets.yml" ]
-
- def gemset=(gemset)
- if gemset
- @gemset = gemset.to_sym
-
- @gemsets = @configs.reverse.collect { |config|
- if config.is_a?(::String)
- YAML::load(File.read(config)) rescue {}
- elsif config.is_a?(::Hash)
- config
- end
- }.inject({}) do |hash, config|
- deep_merge(hash, symbolize_keys(config))
- end
-
- Gems.versions = @gemsets[Gemspec.name.to_sym].inject({}) do |hash, (key, value)|
- if value.is_a?(::String)
- hash[key] = value
- elsif value.is_a?(::Hash) && key == @gemset
- value.each { |k, v| hash[k] = v }
- end
- hash
- end
- else
- @gemset = nil
- @gemsets = nil
- Gems.versions = nil
- end
- end
-
- private
-
- def deep_merge(first, second)
- merger = lambda do |key, v1, v2|
- Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2
- end
- first.merge(second, &merger)
- end
-
- def symbolize_keys(hash)
- return {} unless hash.is_a?(::Hash)
- hash.inject({}) do |options, (key, value)|
- value = symbolize_keys(value) if value.is_a?(::Hash)
- options[(key.to_sym rescue key) || key] = value
- options
- end
- end
- end
- end
- end
-end
@@ -1,32 +0,0 @@
-unless defined?(GemTemplate::Gemspec)
-
- require 'yaml'
-
- module GemTemplate
- class Gemspec
- class <<self
-
- attr_accessor :data
-
- def reload
- Gemspec.data =
- YAML::load(
- File.read(
- "#{File.expand_path('../../../', __FILE__)}/config/gemspec.yml"
- )
- ) rescue {}
-
- Gemspec.data.each do |key, value|
- self.send :eval, <<-EVAL
- def #{key}
- #{value.inspect}
- end
- EVAL
- end
- end
-
- Gemspec.reload
- end
- end
- end
-end
Oops, something went wrong.

0 comments on commit 74e0e65

Please sign in to comment.