Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moving all gem-related modules under one module

  • Loading branch information...
commit 74e0e654a4c906c5d691a4df4939f6b386c408ff 1 parent eca33a4
@winton authored
View
6 gem_template.gemspec
@@ -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
81 lib/gem_template/gems.rb
@@ -1,16 +1,29 @@
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
@@ -18,7 +31,7 @@ def activate(*gems)
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
View
65 lib/gem_template/gemsets.rb
@@ -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
View
32 lib/gem_template/gemspec.rb
@@ -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
View
160 spec/gem_template/gems_spec.rb
@@ -2,15 +2,15 @@
describe GemTemplate::Gems do
- before(:each) do
- GemTemplate::Gemsets.configs = [
- "#{$root}/spec/fixtures/gemsets.yml"
- ]
- GemTemplate::Gemsets.gemset = nil
- GemTemplate::Gems.testing = true
- end
-
describe :activate do
+ before(:each) do
+ GemTemplate::Gems.configs = [
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ GemTemplate::Gems.gemset = nil
+ GemTemplate::Gems.testing = true
+ end
+
it "should warn if unable to require rubygems" do
GemTemplate::Gems.stub!(:require)
GemTemplate::Gems.should_receive(:require).with('rubygems').and_raise(LoadError)
@@ -28,4 +28,148 @@
GemTemplate::Gems.activate :rspec, 'rake'
end
end
+
+ describe :gemset= do
+ before(:each) do
+ GemTemplate::Gems.configs = [
+ {
+ :gem_template => {
+ :rake => '>0.8.6',
+ :default => {
+ :externals => '=1.0.2'
+ }
+ }
+ },
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ end
+
+ describe :default do
+ before(:each) do
+ GemTemplate::Gems.gemset = :default
+ end
+
+ it "should set @gemset" do
+ GemTemplate::Gems.gemset.should == :default
+ end
+
+ it "should set @gemsets" do
+ GemTemplate::Gems.gemsets.should == {
+ :gem_template => {
+ :rake => ">0.8.6",
+ :default => {
+ :externals => '=1.0.2',
+ :rspec => "=1.3.1"
+ },
+ :rspec2 => { :rspec => "=2.3.0" }
+ }
+ }
+ end
+
+ it "should set Gems.versions" do
+ GemTemplate::Gems.versions.should == {
+ :rake => ">0.8.6",
+ :rspec => "=1.3.1",
+ :externals => "=1.0.2"
+ }
+ end
+
+ it "should set everything to nil if gemset given nil value" do
+ GemTemplate::Gems.gemset = nil
+ GemTemplate::Gems.gemset.should == nil
+ GemTemplate::Gems.gemsets.should == nil
+ GemTemplate::Gems.versions.should == nil
+ end
+ end
+
+ describe :rspec2 do
+ before(:each) do
+ GemTemplate::Gems.gemset = "rspec2"
+ end
+
+ it "should set @gemset" do
+ GemTemplate::Gems.gemset.should == :rspec2
+ end
+
+ it "should set @gemsets" do
+ GemTemplate::Gems.gemsets.should == {
+ :gem_template => {
+ :rake => ">0.8.6",
+ :default => {
+ :externals => '=1.0.2',
+ :rspec => "=1.3.1"
+ },
+ :rspec2 => { :rspec => "=2.3.0" }
+ }
+ }
+ end
+
+ it "should set Gems.versions" do
+ GemTemplate::Gems.versions.should == {
+ :rake => ">0.8.6",
+ :rspec => "=2.3.0"
+ }
+ end
+ end
+
+ describe :nil do
+ before(:each) do
+ GemTemplate::Gems.gemset = nil
+ end
+
+ it "should set everything to nil" do
+ GemTemplate::Gems.gemset.should == nil
+ GemTemplate::Gems.gemsets.should == nil
+ GemTemplate::Gems.versions.should == nil
+ end
+ end
+ end
+
+ describe :reload_gemspec do
+ before(:all) do
+ GemTemplate::Gems.configs = [
+ "#{$root}/spec/fixtures/gemsets.yml"
+ ]
+ GemTemplate::Gems.gemset = :default
+ end
+
+ before(:each) do
+ @gemspec_path = "#{$root}/gem_template.gemspec"
+ @gemspec = File.read(@gemspec_path)
+ yml = File.read("#{$root}/spec/fixtures/gemspec.yml")
+ File.stub!(:read).and_return yml
+ GemTemplate::Gems.reload_gemspec
+ end
+
+ it "should populate @gemspec" do
+ GemTemplate::Gems.gemspec.hash.should == {
+ "name" => "name",
+ "version" => "0.1.0",
+ "authors" => ["Author"],
+ "email" => "email@email.com",
+ "homepage" => "http://github.com/author/name",
+ "summary" => "Summary",
+ "description" => "Description",
+ "dependencies" => ["rake"],
+ "development_dependencies" => ["rspec"]
+ }
+ end
+
+ it "should create methods from keys of @gemspec" do
+ GemTemplate::Gems.gemspec.name.should == "name"
+ GemTemplate::Gems.gemspec.version.should == "0.1.0"
+ GemTemplate::Gems.gemspec.authors.should == ["Author"]
+ GemTemplate::Gems.gemspec.email.should == "email@email.com"
+ GemTemplate::Gems.gemspec.homepage.should == "http://github.com/author/name"
+ GemTemplate::Gems.gemspec.summary.should == "Summary"
+ GemTemplate::Gems.gemspec.description.should == "Description"
+ GemTemplate::Gems.gemspec.dependencies.should == ["rake"]
+ GemTemplate::Gems.gemspec.development_dependencies.should == ["rspec"]
+ end
+
+ it "should produce a valid gemspec" do
+ gemspec = eval(@gemspec, binding, @gemspec_path)
+ gemspec.validate.should == true
+ end
+ end
end
View
100 spec/gem_template/gemsets_spec.rb
@@ -1,100 +0,0 @@
-require 'spec_helper'
-
-describe GemTemplate::Gemsets do
-
- before(:each) do
- GemTemplate::Gemsets.configs = [
- {
- :gem_template => {
- :rake => '>0.8.6',
- :default => {
- :externals => '=1.0.2'
- }
- }
- },
- "#{$root}/spec/fixtures/gemsets.yml"
- ]
- end
-
- describe :gemset= do
- describe :default do
- before(:each) do
- GemTemplate::Gemsets.gemset = :default
- end
-
- it "should set @gemset" do
- GemTemplate::Gemsets.gemset.should == :default
- end
-
- it "should set @gemsets" do
- GemTemplate::Gemsets.gemsets.should == {
- :gem_template => {
- :rake => ">0.8.6",
- :default => {
- :externals => '=1.0.2',
- :rspec => "=1.3.1"
- },
- :rspec2 => { :rspec => "=2.3.0" }
- }
- }
- end
-
- it "should set Gems.versions" do
- GemTemplate::Gems.versions.should == {
- :rake => ">0.8.6",
- :rspec => "=1.3.1",
- :externals => "=1.0.2"
- }
- end
-
- it "should set everything to nil if gemset given nil value" do
- GemTemplate::Gemsets.gemset = nil
- GemTemplate::Gemsets.gemset.should == nil
- GemTemplate::Gemsets.gemsets.should == nil
- GemTemplate::Gems.versions.should == nil
- end
- end
-
- describe :rspec2 do
- before(:each) do
- GemTemplate::Gemsets.gemset = "rspec2"
- end
-
- it "should set @gemset" do
- GemTemplate::Gemsets.gemset.should == :rspec2
- end
-
- it "should set @gemsets" do
- GemTemplate::Gemsets.gemsets.should == {
- :gem_template => {
- :rake => ">0.8.6",
- :default => {
- :externals => '=1.0.2',
- :rspec => "=1.3.1"
- },
- :rspec2 => { :rspec => "=2.3.0" }
- }
- }
- end
-
- it "should set Gems.versions" do
- GemTemplate::Gems.versions.should == {
- :rake => ">0.8.6",
- :rspec => "=2.3.0"
- }
- end
- end
-
- describe :nil do
- before(:each) do
- GemTemplate::Gemsets.gemset = nil
- end
-
- it "should set everything to nil" do
- GemTemplate::Gemsets.gemset.should == nil
- GemTemplate::Gemsets.gemsets.should == nil
- GemTemplate::Gems.versions.should == nil
- end
- end
- end
-end
View
54 spec/gem_template/gemspec_spec.rb
@@ -1,54 +0,0 @@
-require 'spec_helper'
-
-describe GemTemplate::Gemspec do
-
- before(:all) do
- GemTemplate::Gemsets.configs = [
- "#{$root}/spec/fixtures/gemsets.yml"
- ]
- GemTemplate::Gemsets.gemset = :default
- end
-
- before(:each) do
- @gemspec_path = "#{$root}/gem_template.gemspec"
- @gemspec = File.read(@gemspec_path)
- yml = File.read("#{$root}/spec/fixtures/gemspec.yml")
- File.stub!(:read).and_return yml
- GemTemplate::Gemspec.reload
- end
-
- after(:all) do
- GemTemplate::Gemspec.reload
- end
-
- it "should populate @data" do
- GemTemplate::Gemspec.data.should == {
- "name" => "name",
- "version" => "0.1.0",
- "authors" => ["Author"],
- "email" => "email@email.com",
- "homepage" => "http://github.com/author/name",
- "summary" => "Summary",
- "description" => "Description",
- "dependencies" => ["rake"],
- "development_dependencies" => ["rspec"]
- }
- end
-
- it "should create methods from keys of @data" do
- GemTemplate::Gemspec.name.should == "name"
- GemTemplate::Gemspec.version.should == "0.1.0"
- GemTemplate::Gemspec.authors.should == ["Author"]
- GemTemplate::Gemspec.email.should == "email@email.com"
- GemTemplate::Gemspec.homepage.should == "http://github.com/author/name"
- GemTemplate::Gemspec.summary.should == "Summary"
- GemTemplate::Gemspec.description.should == "Description"
- GemTemplate::Gemspec.dependencies.should == ["rake"]
- GemTemplate::Gemspec.development_dependencies.should == ["rspec"]
- end
-
- it "should produce a valid gemspec" do
- gemspec = eval(@gemspec, binding, @gemspec_path)
- gemspec.validate.should == true
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.