cldwalker-local_gem 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,63 @@
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 cldwalker-local_gem -s http://gems.github.com
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:
34
+
35
+ 1. Mild: Call LocalGem with its class methods.
36
+
37
+ require 'local_gem'
38
+ LocalGem.local_gem 'alias'
39
+
40
+ 2. Hot: Call methods directly having included it into your Kernel namespace.
41
+
42
+ require 'local_gem'
43
+ include LocalGem
44
+ local_gem 'alias'
45
+
46
+ 3. Spicy: Override require() and gem() with local_require() and local_gem() respectively.
47
+
48
+ require 'local_gem'
49
+ require 'local_gem/override'
50
+ gem 'alias'
51
+
52
+ All three ways would add my local alias library to $LOAD_PATH. These three ways
53
+ also apply to local_require().
54
+
55
+
56
+ == Motivation
57
+
58
+ Got tired of installing/uninstalling the latest version of a gem I'm actively working on. This
59
+ also makes it easy to treat any random directory of ruby files as a gem.
60
+
61
+ == Limitations
62
+
63
+ The override and rcov don't play nicely.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
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.description = "Loads any gem/library simply given its path. Great for nascent gems and/or for trying the latest code on a gem."
22
+ s.summary = s.description
23
+ s.email = "gabriel.horner@gmail.com"
24
+ s.homepage = "http://github.com/cldwalker/local_gem"
25
+ s.authors = ["Gabriel Horner"]
26
+ s.has_rdoc = true
27
+ s.files = FileList["VERSION.yml","Rakefile", "README.rdoc", "LICENSE.txt", "{bin,lib,test}/**/*"]
28
+ end
29
+
30
+ rescue LoadError
31
+ puts "Jeweler not available. Install it for jeweler-related tasks with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
32
+ end
33
+
34
+ Rake::TestTask.new do |t|
35
+ t.libs << 'lib'
36
+ t.pattern = 'test/**/*_test.rb'
37
+ t.verbose = false
38
+ end
39
+
40
+ Rake::RDocTask.new do |rdoc|
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = 'test'
43
+ rdoc.options << '--line-numbers' << '--inline-source'
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
47
+
48
+ task :default => :test
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 1
@@ -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,65 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+ require 'config_struct'
4
+
5
+ module LocalGem
6
+ extend self
7
+ def local_require(path) #:nodoc:
8
+ Singleton.local_require(path)
9
+ end
10
+
11
+ def local_gem(*args) #:nodoc:
12
+ Singleton.local_gem(*args)
13
+ end
14
+
15
+ def setup_config(config, &block) #:nodoc:
16
+ Singleton.setup_config(config, &block)
17
+ end
18
+
19
+ module Singleton
20
+ extend self
21
+ attr_accessor :config, :config_file
22
+
23
+ # Holds the mapping of local gems and their paths to load.
24
+ def config
25
+ @config ||= read_config
26
+ end
27
+
28
+ # Reads config from @config_file and returns a hash.
29
+ # @config_file can be set via its accessor. Otherwise it defaults to a local local_gem.yml or
30
+ # ~/.local_gem.yml.
31
+ def read_config
32
+ @config_file ||= ['local_gem.yml', File.join("~", ".local_gem.yml")].detect {|e| File.exists?(File.expand_path(e)) }
33
+ @config_file ? YAML::load(File.new(File.expand_path(@config_file))) : {:gems=>{}}
34
+ end
35
+
36
+ # Takes either a hash or a block and initializes config().
37
+ def setup_config(config=nil, &block)
38
+ @config = config || ConfigStruct.block_to_hash(block)
39
+ end
40
+
41
+ # Loads the local gem if found or defaults to a normal gem call.
42
+ def local_gem(*args)
43
+ load_local_gem(args[0]) || gem(*args)
44
+ end
45
+
46
+ # Loads the local gem if found and then does a normal require on it.
47
+ def local_require(lib)
48
+ load_local_gem(lib)
49
+ require(lib)
50
+ end
51
+
52
+ # Adds a given library's path (specified in config) to the beginning of $LOAD_PATH.
53
+ def load_local_gem(library)
54
+ if path = config[:gems][library]
55
+ path = [path] unless path.is_a?(Array)
56
+ path.map {|e| File.expand_path(e) }.each do |f|
57
+ $:.unshift(f) unless $:.include?(f)
58
+ end
59
+ true
60
+ else
61
+ false
62
+ end
63
+ end
64
+ end
65
+ 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,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cldwalker-local_gem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Horner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-01 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Loads any gem/library simply given its path. Great for nascent gems and/or for trying the latest code on a gem.
17
+ email: gabriel.horner@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - Rakefile
27
+ - README.rdoc
28
+ - LICENSE.txt
29
+ - lib/config_struct.rb
30
+ - lib/local_gem
31
+ - lib/local_gem/override.rb
32
+ - lib/local_gem.rb
33
+ - test/local_gem_test.rb
34
+ - test/override_test.rb
35
+ - test/test_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/cldwalker/local_gem
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Loads any gem/library simply given its path. Great for nascent gems and/or for trying the latest code on a gem.
62
+ test_files: []
63
+