tcravit_ruby_lib 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/Rakefile +4 -0
- data/lib/tcravit_ruby_lib/config_searcher.rb +57 -0
- data/lib/tcravit_ruby_lib/version.rb +3 -0
- data/lib/tcravit_ruby_lib.rb +5 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/tcravit_ruby_lib/config_searcher_spec.rb +56 -0
- data/tcravit_ruby_lib.gemspec +22 -0
- metadata +69 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
module TcravitRubyLib #:nodoc:
|
4
|
+
|
5
|
+
# Utilities for locating a configuration directory within a tree.
|
6
|
+
#
|
7
|
+
# Unix utilities such as +git+ will look for the .git directory in a
|
8
|
+
# project by starting with the current directory and traversing upward
|
9
|
+
# until a .git folder is found. This allows local configuration in a
|
10
|
+
# subdirectory to override a global configuration file upstream.
|
11
|
+
#
|
12
|
+
# This module, inspired by a post on Practicing Ruby, implements that
|
13
|
+
# kind of traversal for Ruby applications.
|
14
|
+
module ConfigSearcher
|
15
|
+
|
16
|
+
# Locate a configuration folder by starting in the specified directory
|
17
|
+
# and traversing upward through the directory tree.
|
18
|
+
#
|
19
|
+
# === Options
|
20
|
+
#
|
21
|
+
# * +:start_in+ or +:start_dir+ - The directory in which to begin the
|
22
|
+
# search. Defaults to the current directory if unspecified or if the
|
23
|
+
# specified directory doesn't exist.
|
24
|
+
# * +:look_for+ or +:config_dir+ - The name of the configuration directory
|
25
|
+
# to look for. Defaults to +.config+ if unspecified.
|
26
|
+
# * +:only_container_dir: - If this option is true, the +:look_for+
|
27
|
+
# directory will not be returned as part of the path.
|
28
|
+
#
|
29
|
+
# === Returns
|
30
|
+
#
|
31
|
+
# If the configuration directory is found, its path will be returned as a
|
32
|
+
# string. Otherwise, an empty string will be returned. If the +start_in+
|
33
|
+
# directory doesn't exist, an exception will be raised.
|
34
|
+
def self.locate_config_dir(opts={})
|
35
|
+
start_dir = opts[:start_in] || opts[:start_dir] || "."
|
36
|
+
config_dir_name = opts[:look_for] || opts[:config_dir] || ".config"
|
37
|
+
only_container_dir = opts[:only_container_dir] || false
|
38
|
+
|
39
|
+
dir = Pathname.new(start_dir)
|
40
|
+
app_config_dir = dir + config_dir_name
|
41
|
+
|
42
|
+
if dir.children.include?(app_config_dir)
|
43
|
+
if only_container_dir
|
44
|
+
app_config_dir.to_s.split('/')[0..-2].join('/')
|
45
|
+
else
|
46
|
+
app_config_dir.expand_path.to_s
|
47
|
+
end
|
48
|
+
else
|
49
|
+
return nil if dir.expand_path.root?
|
50
|
+
# In the event the opts come from a hash that's used elsewhere, I don't
|
51
|
+
# want to modify it, so I make a copy and replace the :start_in value.
|
52
|
+
# I'm sure there's a better way to do this.
|
53
|
+
locate_config_dir(opts.reject{|k,v| k == :start_in}.merge({start_in: dir.parent.to_s}))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
describe "TcravitRubyLib" do
|
5
|
+
describe "ConfigSearcher" do
|
6
|
+
|
7
|
+
BASE_DIR = '/tmp/config_searcher_test'
|
8
|
+
DEEP_DIR = "#{BASE_DIR}/foo/bar/baz"
|
9
|
+
CONFIG_DIR = "#{BASE_DIR}/.config"
|
10
|
+
|
11
|
+
before(:all) do
|
12
|
+
FileUtils.mkdir_p DEEP_DIR
|
13
|
+
FileUtils.mkdir_p CONFIG_DIR
|
14
|
+
end
|
15
|
+
|
16
|
+
after(:all) do
|
17
|
+
FileUtils.remove_dir BASE_DIR, true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should successfully find a directory which exists" do
|
21
|
+
dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".config")
|
22
|
+
dir_path.to_s.should_not be_nil
|
23
|
+
dir_path.to_s.should == CONFIG_DIR
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not find a directory when one doesn't exist" do
|
27
|
+
dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".snausages")
|
28
|
+
dir_path.to_s.should == ""
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return the container dir when the only_container_dir option is provided" do
|
32
|
+
dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: DEEP_DIR, look_for: ".config", only_container_dir: true)
|
33
|
+
dir_path.to_s.should == BASE_DIR
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should raise an exception when the start_in directory doesn't exist" do
|
37
|
+
an_exception = nil
|
38
|
+
|
39
|
+
begin
|
40
|
+
dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_in: "#{DEEP_DIR}xxxxxxx", look_for: ".snausages")
|
41
|
+
rescue => e
|
42
|
+
an_exception = e
|
43
|
+
end
|
44
|
+
|
45
|
+
an_exception.should_not be_nil
|
46
|
+
an_exception.message.should == "No such file or directory - #{DEEP_DIR}xxxxxxx"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should behave the same for the alternative option names" do
|
50
|
+
dir_path = TcravitRubyLib::ConfigSearcher.locate_config_dir(start_dir: DEEP_DIR, config_dir: ".config")
|
51
|
+
dir_path.to_s.should_not be_nil
|
52
|
+
dir_path.to_s.should == CONFIG_DIR
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "tcravit_ruby_lib/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "tcravit_ruby_lib"
|
7
|
+
s.version = TcravitRubyLib::VERSION
|
8
|
+
s.authors = ["Tammy Cravit"]
|
9
|
+
s.email = ["tcravit@taylored-software.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Random reusable ruby stuff}
|
12
|
+
|
13
|
+
s.rubyforge_project = "tcravit_ruby_lib"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "rspec"
|
21
|
+
# s.add_runtime_dependency "rest-client"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tcravit_ruby_lib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tammy Cravit
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-02 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70131351967320 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70131351967320
|
25
|
+
description:
|
26
|
+
email:
|
27
|
+
- tcravit@taylored-software.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- CHANGELOG.md
|
34
|
+
- Gemfile
|
35
|
+
- Rakefile
|
36
|
+
- lib/tcravit_ruby_lib.rb
|
37
|
+
- lib/tcravit_ruby_lib/config_searcher.rb
|
38
|
+
- lib/tcravit_ruby_lib/version.rb
|
39
|
+
- spec/spec_helper.rb
|
40
|
+
- spec/tcravit_ruby_lib/config_searcher_spec.rb
|
41
|
+
- tcravit_ruby_lib.gemspec
|
42
|
+
homepage: ''
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project: tcravit_ruby_lib
|
62
|
+
rubygems_version: 1.8.11
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Random reusable ruby stuff
|
66
|
+
test_files:
|
67
|
+
- spec/spec_helper.rb
|
68
|
+
- spec/tcravit_ruby_lib/config_searcher_spec.rb
|
69
|
+
has_rdoc:
|