local_gem 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,8 @@
1
+ == 0.1.3
2
+ * Bug fix for 1.9 and Rubyforge release
3
+
4
+ == 0.1.0
5
+ * Added better docs and tests
6
+
7
+ == 0.0.1
8
+ * Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2009 Gabriel Horner
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,71 @@
1
+ == Description
2
+
3
+ You have the beginnings of a ruby library and you want to access it quick.
4
+ You don't want to bother making a gemspec for it and uninstalling/reinstalling its gem
5
+ while you mess with it. Simply tell LocalGem what paths it should load for
6
+ your local gem and they will be loaded. Note that it doesn't matter how gem-like
7
+ your project is ie lib and bin directories etc. LocalGem *only* needs to know
8
+ the full path to your gem/library.
9
+
10
+ == Setup
11
+
12
+ Install the gem with:
13
+
14
+ sudo gem install local_gem
15
+
16
+ To setup your local gem paths, give LocalGem a hash of gem names pointing
17
+ to a path or an array of paths to load. You can do this with LocalGem.setup_config
18
+
19
+ LocalGem.setup_config do |c|
20
+ c.gems = {'gem1'=>"/gem1/path/lib", 'gem2'=> ["/gem2/path/lib", "/gem2/path/bin"] }
21
+ end
22
+
23
+ Or a config file at either a local local_gem.yml or ~/.local_gem.yml .
24
+ See local_gem.yml.example for an example config file.
25
+
26
+ == Usage
27
+
28
+ The two main methods that LocalGem provides are local_gem() and local_require()
29
+ which map to gem() and require() respectively. Both methods will attempt
30
+ to load local gems that you have defined. If no gem is found than they resort to default gem/require
31
+ behavior.
32
+
33
+ There are 3 ways to use this library, depending on how much you want LocalGem to invade your
34
+ namespace:
35
+
36
+ 1. Peace time:
37
+
38
+ require 'local_gem'
39
+ LocalGem.local_gem 'mygem'
40
+ LocalGem.local_require 'anothergem'
41
+
42
+ 2. Diplomacy is fading:
43
+
44
+ require 'local_gem'
45
+ include LocalGem
46
+ local_gem 'mygem'
47
+ local_require 'anothergem'
48
+
49
+ 3. You're fscked (Don't worry, they should default to their normal behavior, should being the
50
+ keyword):
51
+
52
+ require 'local_gem'
53
+ require 'local_gem/override'
54
+ gem 'mygem'
55
+ require 'anothergem'
56
+
57
+ All three ways would add my local alias library to $LOAD_PATH. These three ways
58
+ also apply to local_require().
59
+
60
+
61
+ == Motivation
62
+
63
+ Got tired of installing/uninstalling the latest version of a gem I'm actively working on. This
64
+ also makes it easy to treat any random directory of ruby files as a gem.
65
+
66
+ == Links
67
+ http://tagaholic.me/2009/02/05/local-gem-loads-your-current-code-now.html
68
+
69
+ == Limitations
70
+
71
+ The override and rcov don't play nicely.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ begin
5
+ require 'rcov/rcovtask'
6
+
7
+ Rcov::RcovTask.new do |t|
8
+ t.libs << 'test'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.rcov_opts = ["-T -x '/Library/Ruby/*'"]
11
+ t.verbose = true
12
+ end
13
+ rescue LoadError
14
+ puts "Rcov not available. Install it for rcov-related tasks with: sudo gem install rcov"
15
+ end
16
+
17
+ begin
18
+ require 'jeweler'
19
+ Jeweler::Tasks.new do |s|
20
+ s.name = "local_gem"
21
+ s.summary = "Loads any gem/library simply given its path. Great for nascent gems and/or for trying the latest code on a gem."
22
+ s.description = "You have the beginnings of a ruby library and you want to access it quick. You don't want to bother making a gemspec for it and uninstalling/reinstalling its gem while you mess with it. Simply tell LocalGem what paths it should load for your local gem and they will be loaded. Note that it doesn't matter how gem-like your project is ie lib and bin directories etc. LocalGem only needs to know the full path to your gem/library."
23
+ s.email = "gabriel.horner@gmail.com"
24
+ s.homepage = "http://tagaholic.me/local_gem/"
25
+ s.authors = ["Gabriel Horner"]
26
+ s.rubyforge_project = 'tagaholic'
27
+ s.has_rdoc = true
28
+ s.files = FileList["CHANGELOG.rdoc", "VERSION.yml","Rakefile", "README.rdoc", "LICENSE.txt", "{bin,lib,test}/**/*"]
29
+ end
30
+
31
+ rescue LoadError
32
+ puts "Jeweler not available. Install it for jeweler-related tasks with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
33
+ end
34
+
35
+ Rake::TestTask.new do |t|
36
+ t.libs << 'lib'
37
+ t.pattern = 'test/**/*_test.rb'
38
+ t.verbose = false
39
+ end
40
+
41
+ Rake::RDocTask.new do |rdoc|
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = 'test'
44
+ rdoc.options << '--line-numbers' << '--inline-source'
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
49
+ task :default => :test
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 3
@@ -0,0 +1,18 @@
1
+ require 'ostruct'
2
+
3
+ class ConfigStruct < OpenStruct
4
+ # Converts the data within the given block to hash
5
+ def self.block_to_hash(block=nil)
6
+ config = self.new
7
+ if block
8
+ block.call(config)
9
+ config.to_hash
10
+ else
11
+ {}
12
+ end
13
+ end
14
+
15
+ def to_hash #:nodoc:
16
+ @table
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ module Kernel
2
+ alias_method :old_require, :require
3
+ alias_method :old_gem, :gem
4
+
5
+ # Overrides gem() to behave like LocalGem::Singleton.local_gem().
6
+ def gem(*args)
7
+ LocalGem::Singleton.load_local_gem(args[0]) || old_gem(*args)
8
+ end
9
+
10
+ # Overrides require() to behave like LocalGem::Singleton.local_require().
11
+ def require(lib)
12
+ LocalGem::Singleton.load_local_gem(lib)
13
+ old_require(lib)
14
+ end
15
+
16
+ private :old_require, :old_gem
17
+ end
18
+
19
+ self.send :include, LocalGem
data/lib/local_gem.rb ADDED
@@ -0,0 +1,66 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+ require 'config_struct'
4
+ require 'yaml'
5
+
6
+ module LocalGem
7
+ extend self
8
+ def local_require(path) #:nodoc:
9
+ Singleton.local_require(path)
10
+ end
11
+
12
+ def local_gem(*args) #:nodoc:
13
+ Singleton.local_gem(*args)
14
+ end
15
+
16
+ def setup_config(config, &block) #:nodoc:
17
+ Singleton.setup_config(config, &block)
18
+ end
19
+
20
+ module Singleton
21
+ extend self
22
+ attr_accessor :config, :config_file
23
+
24
+ # Holds the mapping of local gems and their paths to load.
25
+ def config
26
+ @config ||= read_config
27
+ end
28
+
29
+ # Reads config from @config_file and returns a hash.
30
+ # @config_file can be set via its accessor. Otherwise it defaults to a local local_gem.yml or
31
+ # ~/.local_gem.yml.
32
+ def read_config
33
+ @config_file ||= ['local_gem.yml', File.join("~", ".local_gem.yml")].detect {|e| File.exists?(File.expand_path(e)) }
34
+ @config_file ? YAML::load(File.new(File.expand_path(@config_file))) : {:gems=>{}}
35
+ end
36
+
37
+ # Takes either a hash or a block and initializes config().
38
+ def setup_config(config=nil, &block)
39
+ @config = config || ConfigStruct.block_to_hash(block)
40
+ end
41
+
42
+ # Loads the local gem if found or defaults to a normal gem call.
43
+ def local_gem(*args)
44
+ load_local_gem(args[0]) || gem(*args)
45
+ end
46
+
47
+ # Loads the local gem if found and then does a normal require on it.
48
+ def local_require(lib)
49
+ load_local_gem(lib)
50
+ require(lib)
51
+ end
52
+
53
+ # Adds a given library's path (specified in config) to the beginning of $LOAD_PATH.
54
+ def load_local_gem(library)
55
+ if path = config[:gems][library]
56
+ path = [path] unless path.is_a?(Array)
57
+ path.map {|e| File.expand_path(e) }.each do |f|
58
+ $:.unshift(f) unless $:.include?(f)
59
+ end
60
+ true
61
+ else
62
+ false
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,70 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class LocalGem::Test < Test::Unit::TestCase
4
+ before(:each) {
5
+ LocalGem::Singleton.config = { :gems=>{
6
+ 'gem1'=>'path1', 'gem2'=>['path2','path3']}
7
+ }
8
+ }
9
+
10
+ test "local_gem adds gem's one path to $:" do
11
+ full_gem_path = File.expand_path(LocalGem::Singleton.config[:gems]['gem1'])
12
+ assert !$:.include?(full_gem_path)
13
+ LocalGem.local_gem('gem1')
14
+ assert $:.include?(full_gem_path)
15
+ end
16
+
17
+ test "local_gem adds gem's multiple paths to $:" do
18
+ full_gem_paths = LocalGem::Singleton.config[:gems]['gem2'].map {|e| File.expand_path(e) }
19
+ assert ($: & full_gem_paths).empty?
20
+ LocalGem.local_gem('gem2')
21
+ assert !($: & full_gem_paths).empty?
22
+ end
23
+
24
+ test "local_gem defaults to gem if no local gem found" do
25
+ LocalGem::Singleton.expects(:gem).once
26
+ LocalGem.local_gem('invalid')
27
+ end
28
+
29
+ test "local_gem calls load_local_gem for a valid gem" do
30
+ LocalGem::Singleton.expects(:load_local_gem).once.returns(true)
31
+ LocalGem.local_gem('blah')
32
+ end
33
+
34
+ test "local_require calls load_local_gem and require" do
35
+ LocalGem::Singleton.expects(:require).once
36
+ LocalGem::Singleton.expects(:load_local_gem).once
37
+ LocalGem.local_require('blah')
38
+ end
39
+
40
+ test "setup_config with hash initializes config" do
41
+ config = {:gems=>{'blah_gem'=>'blah_path'}}
42
+ LocalGem::Singleton.setup_config(config)
43
+ assert_equal config, LocalGem::Singleton.config
44
+ end
45
+
46
+ test "setup_config with block initializes config" do
47
+ config = {:gems=>{'blah_gem'=>'blah_path'}}
48
+ LocalGem::Singleton.setup_config do |c|
49
+ c.gems = config[:gems]
50
+ end
51
+ assert_equal config, LocalGem::Singleton.config
52
+ end
53
+
54
+ test "read_config uses existing config_file" do
55
+ LocalGem::Singleton.config_file = 'blah'
56
+ File.expects(:new)
57
+ YAML::expects(:load)
58
+ LocalGem::Singleton.read_config
59
+ assert_equal 'blah', LocalGem::Singleton.config_file
60
+ end
61
+
62
+ test "read_config uses config_file it detects" do
63
+ LocalGem::Singleton.config_file = nil
64
+ File.expects(:new)
65
+ YAML::expects(:load)
66
+ File.expects(:exists?).times(2).returns(false, true)
67
+ LocalGem::Singleton.read_config
68
+ assert 'local_gem.yml', File.basename(LocalGem::Singleton.config_file)
69
+ end
70
+ end
@@ -0,0 +1,20 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class LocalGem::OverrideTest < Test::Unit::TestCase
4
+ before(:all) { require 'local_gem/override' }
5
+ test "gem calls old_gem and load_local_gem" do
6
+ LocalGem::Singleton.expects(:load_local_gem).once
7
+ self.expects(:old_gem).once
8
+ gem('blah')
9
+ end
10
+
11
+ test "require calls old_require and load_local_gem" do
12
+ LocalGem::Singleton.expects(:load_local_gem).once
13
+ self.expects(:old_require).once
14
+ require('blah')
15
+ end
16
+
17
+ test "loading override should have included LocalGem in self" do
18
+ assert self.instance_eval("class<<self; self; end").ancestors.include?(LocalGem)
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'context' #gem install jeremymcanally-context -s http://gems.github.com
4
+ require 'mocha'
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'local_gem'
7
+
8
+ class Test::Unit::TestCase
9
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: local_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Horner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-22 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: You have the beginnings of a ruby library and you want to access it quick. You don't want to bother making a gemspec for it and uninstalling/reinstalling its gem while you mess with it. Simply tell LocalGem what paths it should load for your local gem and they will be loaded. Note that it doesn't matter how gem-like your project is ie lib and bin directories etc. LocalGem only needs to know the full path to your gem/library.
17
+ email: gabriel.horner@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE.txt
24
+ - README.rdoc
25
+ files:
26
+ - CHANGELOG.rdoc
27
+ - LICENSE.txt
28
+ - README.rdoc
29
+ - Rakefile
30
+ - VERSION.yml
31
+ - lib/config_struct.rb
32
+ - lib/local_gem.rb
33
+ - lib/local_gem/override.rb
34
+ - test/local_gem_test.rb
35
+ - test/override_test.rb
36
+ - test/test_helper.rb
37
+ has_rdoc: true
38
+ homepage: http://tagaholic.me/local_gem/
39
+ licenses: []
40
+
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: tagaholic
61
+ rubygems_version: 1.3.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Loads any gem/library simply given its path. Great for nascent gems and/or for trying the latest code on a gem.
65
+ test_files:
66
+ - test/local_gem_test.rb
67
+ - test/override_test.rb
68
+ - test/test_helper.rb