homecoming 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 984f5b31cb94ef8c040a44eab2e393bbecfe1917
4
+ data.tar.gz: fe029bcac9fd794eae77280f6e7fc2267bf97099
5
+ SHA512:
6
+ metadata.gz: 042a0d9c8334afc1310697e7e4612fe2c8659f777159dc1edc41232a7bd74606de206748c3767a58b5e3efdd46407a97585fe8463b31f26c7136f632d0dc90af
7
+ data.tar.gz: 80365a5a23eca9fe34f472e67cc488430c36fb1d2b0f8a69d2785176b2ac99f37a529aaefda5b6343eb76205f8012c639f96674a020cb24a4cfb72b23251c489
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in homecoming.gemspec
4
+ gemspec
5
+
6
+ gem 'simplecov', :require => false, :group => :test
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 René Föhring
2
+
3
+ MIT License
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.md ADDED
@@ -0,0 +1,89 @@
1
+ # Homecoming
2
+
3
+ Homecoming let's you easily traverse all parent directories of a given or
4
+ the current directory.
5
+
6
+ This comes in handy when searching for a config file
7
+ with settings that can be overridden by the same config file in a lower level
8
+ directory (a popular example being `.gitignore`).
9
+
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'homecoming'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install homecoming
24
+
25
+
26
+ ## Usage
27
+
28
+ ### Finding files
29
+
30
+ `Homecoming.find` searches for a given filename in the current and all parent
31
+ directories.
32
+
33
+ # Given the following directory structure:
34
+
35
+ /
36
+ home/
37
+ rrrene/
38
+ projects/
39
+ your_project/
40
+ .yourconfig
41
+ .yourconfig
42
+
43
+ Homecoming.find(".yourconfig", "/home/rrrene/projects/your_project")
44
+ # => ["/home/rrrene/.yourconfig",
45
+ "/home/rrrene/projects/your_project/.yourconfig"]
46
+
47
+ If no path is given as second parameter, the current directory is the
48
+ starting point of the traversal.
49
+
50
+ ### Traversing parents
51
+
52
+ `Homecoming.each` traverses and yields the given and all parent
53
+ directories.
54
+
55
+ Homecoming.each("/home/rrrene/projects/your_project") do |dir|
56
+ # ...
57
+ end
58
+
59
+ If no path is given, the current directory is the starting point of the
60
+ traversal.
61
+
62
+ In our example, this would yield the following directories beginning with the
63
+ given/current one:
64
+
65
+ "/home/rrrene/projects/your_project"
66
+ "/home/rrrene/projects"
67
+ "/home/rrrene"
68
+ "/home"
69
+ "/"
70
+
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it ( http://github.com/<my-github-username>/homecoming/fork )
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
79
+
80
+
81
+ ## Author
82
+
83
+ René Föhring (@rrrene)
84
+
85
+
86
+ ## License
87
+
88
+ Homecoming is released under the MIT License. See the LICENSE.txt file for further
89
+ details.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = "test/**/*_test.rb"
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'homecoming/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "homecoming"
8
+ spec.version = Homecoming::VERSION
9
+ spec.authors = ["René Föhring"]
10
+ spec.email = ["rf@bamaru.de"]
11
+ spec.summary = %q{Easy upwards directory traversal}
12
+ spec.description = %q{Easy upwards directory traversal}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,34 @@
1
+ module Homecoming
2
+ class Find
3
+ def initialize(filename, start_dir = Dir.pwd)
4
+ @start_dir = start_dir
5
+ @filename = filename
6
+ end
7
+
8
+ # Returns all found files with the given filename in the current and all
9
+ # parent directories.
10
+ #
11
+ # # Given the following directory structure:
12
+ #
13
+ # /
14
+ # home/
15
+ # rrrene/
16
+ # projects/
17
+ # your_project/
18
+ # .yourconfig
19
+ # .yourconfig
20
+ #
21
+ # Homecoming.find(".yourconfig", "/home/rrrene/projects/your_project")
22
+ # # => ["/home/rrrene/.yourconfig",
23
+ # "/home/rrrene/projects/your_project/.yourconfig"]
24
+ #
25
+ def files
26
+ found_files = []
27
+ Traversal.new(@start_dir) do |dir|
28
+ filename = File.join(dir, @filename)
29
+ found_files.unshift filename if File.exist?(filename)
30
+ end
31
+ found_files
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ module Homecoming
2
+ class Traversal
3
+ def initialize(dir = Dir.pwd, &block)
4
+ old_length = nil
5
+ while dir != '.' && dir.length != old_length
6
+ yield dir
7
+ old_length = dir.length
8
+ dir = File.dirname(dir)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Homecoming
2
+ VERSION = "0.1.0"
3
+ end
data/lib/homecoming.rb ADDED
@@ -0,0 +1,33 @@
1
+ require "homecoming/find"
2
+ require "homecoming/traversal"
3
+ require "homecoming/version"
4
+
5
+ # Homecoming searches for a given filename in the current and all parent
6
+ # directories.
7
+ #
8
+ # # Given the following directory structure:
9
+ #
10
+ # /
11
+ # home/
12
+ # rrrene/
13
+ # projects/
14
+ # your_project/
15
+ # .yourconfig
16
+ # .yourconfig
17
+ #
18
+ # Homecoming.find(".yourconfig", "/home/rrrene/projects/your_project")
19
+ # # => ["/home/rrrene/.yourconfig",
20
+ # "/home/rrrene/projects/your_project/.yourconfig"]
21
+ #
22
+ # If no path is given as second parameter, the current directory is the
23
+ # starting point of the traversal.
24
+ #
25
+ module Homecoming
26
+ def self.find(filename, path = Dir.pwd)
27
+ Homecoming::Find.new(filename, path).files
28
+ end
29
+
30
+ def self.each(path = Dir.pwd, &block)
31
+ Homecoming::Traversal.new(path, &block)
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ # Nothing here
@@ -0,0 +1 @@
1
+ # settings.yml
@@ -0,0 +1 @@
1
+ # Nothing here
@@ -0,0 +1,30 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
+
3
+ describe ::Homecoming do
4
+ it "should find both .yourconfig files" do
5
+ Dir.chdir File.join(fixture_path(:simple), "projects", "your_project")
6
+ list = Homecoming.find(".yourconfig")
7
+ here = File.dirname(__FILE__)
8
+ assert_equal ["#{here}/fixtures/simple/.yourconfig", "#{here}/fixtures/simple/projects/your_project/.yourconfig"], list
9
+ end
10
+
11
+ it "should find both config/settings.yml files" do
12
+ Dir.chdir File.join(fixture_path(:simple), "projects", "your_project")
13
+ list = Homecoming.find("config/settings.yml")
14
+ here = File.dirname(__FILE__)
15
+ assert_equal ["#{here}/fixtures/simple/config/settings.yml", "#{here}/fixtures/simple/projects/your_project/config/settings.yml"], list
16
+ end
17
+
18
+ it "should find both .yourconfig files" do
19
+ Dir.chdir File.join(fixture_path(:simple), "projects", "your_project")
20
+ list = []
21
+ Homecoming.each do |dir|
22
+ list << dir
23
+ end
24
+ assert_equal Dir.pwd, list.first
25
+ assert_equal "/", list.last
26
+ assert_equal Dir.pwd.split('/').size, list.size
27
+ # directories in the list are getting shorter (getting closer to home)
28
+ assert_equal list.sort_by(&:size), list.reverse
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'minitest/spec'
7
+ require 'minitest/autorun'
8
+ require 'bundler'
9
+ Bundler.require
10
+
11
+ def fixture_path(name)
12
+ File.join(File.dirname(__FILE__), "fixtures", name.to_s)
13
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: homecoming
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - René Föhring
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Easy upwards directory traversal
42
+ email:
43
+ - rf@bamaru.de
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - homecoming.gemspec
54
+ - lib/homecoming.rb
55
+ - lib/homecoming/find.rb
56
+ - lib/homecoming/traversal.rb
57
+ - lib/homecoming/version.rb
58
+ - test/fixtures/simple/.yourconfig
59
+ - test/fixtures/simple/config/settings.yml
60
+ - test/fixtures/simple/projects/your_project/.yourconfig
61
+ - test/fixtures/simple/projects/your_project/config/settings.yml
62
+ - test/homecoming_test.rb
63
+ - test/test_helper.rb
64
+ homepage: ''
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.0.3
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Easy upwards directory traversal
88
+ test_files:
89
+ - test/fixtures/simple/.yourconfig
90
+ - test/fixtures/simple/config/settings.yml
91
+ - test/fixtures/simple/projects/your_project/.yourconfig
92
+ - test/fixtures/simple/projects/your_project/config/settings.yml
93
+ - test/homecoming_test.rb
94
+ - test/test_helper.rb