load_path 0.0.1 → 0.2.0
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.
- checksums.yaml +15 -0
- data/CHANGELOG.md +9 -0
- data/README.md +32 -14
- data/lib/load_path.rb +10 -24
- data/lib/path_builder.rb +75 -0
- metadata +7 -7
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MTg3NWQ4MDBjNzYwY2ZiNjU0ZTE3YWJmMmUwODE2MDQzNDBhOGMxOA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjJhOGZkMDJiNWE4MmExZjY5NDlmZWRkNGFmMTdhMzFlNDNiNTRlOA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MmUyNjYxNGQxYzI5MGY5YzRjZDA5N2RiYjk5Njc2MzI5ZjQ5NTNlNDBjZjVk
|
10
|
+
NzgyYzZhMjIxYjUzODlmZTUxODk1MjgxMGI5MWFiYjE1YzRjZjFhZjRmNmIw
|
11
|
+
ODMwZThiOWRhMTAxZDc3Yzc1MDRiMjcxNjk2MGU1NGQ5MDAxYmY=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZTA5OGNiYmVjYWZkYzFhYzQwOGFhMWE1ZTgyZTUzZWQwNDk3M2QyZTJkMTJk
|
14
|
+
YTM4N2QyMjRmNGQ3NWZjNWJlM2MyMTNjZDVkNTA0OWViODE2YjI3MjU5MzJj
|
15
|
+
YmUzZWMzNjYxYWU0OGU2ZmM4Njg1NDQyN2ZhODc2ZWRhNjE1MDY=
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
#### 0.0.1
|
2
|
+
* Ability to configure load path and require files
|
3
|
+
|
4
|
+
#### 0.1.0
|
5
|
+
* Adds a `PathHelper` and `PathBuilder` class for constructing paths.
|
6
|
+
|
7
|
+
#### 0.2.0
|
8
|
+
* Adds default option of blank to file_path. The way the calling class is inferred is changed and alot of bug fixes gone into the configure block.
|
9
|
+
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRb1vaCQj_2gIgEdX5Fly1vMjfbFKYkuLW1P5c4-3dcFyQ3OqgZoTiOto_8" height="50"> Load Path
|
2
2
|
|
3
|
-
This gem is written to make it easy to configure the load path in Ruby files. Simple use a *configure* block to setup your load path without ugly code to interact with the file system.
|
3
|
+
This gem is written to make it easy to configure the load path in Ruby files. Simple use a *configure* block to setup your load path without ugly code to interact with the file system.
|
4
4
|
|
5
5
|
===================
|
6
6
|
|
@@ -8,45 +8,50 @@ This gem is written to make it easy to configure the load path in Ruby files. Si
|
|
8
8
|
|
9
9
|
For questions and to report bugs, <nayyara.samuel@opower.com>. For contributing to code, send a pull request with a detailed description of changes to this repo.
|
10
10
|
|
11
|
-
###
|
11
|
+
### Building
|
12
12
|
|
13
13
|
To build the gem use the rake commands for gem tasks. To install:
|
14
14
|
|
15
15
|
rake install
|
16
16
|
|
17
17
|
### Usage
|
18
|
-
If you have been programming Ruby you will notice that `require_relative` is not encouraged.
|
19
|
-
The alternative is to setup your load path with code that looks something like the following:
|
18
|
+
If you have been programming Ruby you will notice that `require_relative` is not encouraged.
|
19
|
+
The alternative is to setup your load path with code that looks something like the following:
|
20
20
|
|
21
21
|
```
|
22
22
|
this_dir = File.dirname(__FILE__)
|
23
23
|
|
24
|
-
#Add the folder lib 2 levels above
|
24
|
+
#Add the folder `lib` 2 levels above
|
25
25
|
lib_dir = File.join(this_dir, '..', '..', 'lib')
|
26
26
|
$: << lib_dir
|
27
27
|
|
28
|
-
#Add the folder utils to load path
|
28
|
+
#Add the folder `utils` to load path
|
29
29
|
utils_dir = File.join(this_dir, '..', 'utils')
|
30
30
|
$: << utils_dir
|
31
31
|
|
32
|
-
#Add the folder others under this directory to load path
|
32
|
+
#Add the folder `others` under this directory to load path
|
33
33
|
others_dir = File.join(this_dir, 'others')
|
34
34
|
$: << others_dir
|
35
35
|
|
36
|
+
#Add the folder `bin` on the same level as `lib` folder
|
37
|
+
bin_dir = File.join(this_dir, '..', '..', 'bin')
|
38
|
+
|
36
39
|
```
|
37
40
|
|
38
|
-
And so forth. You can make the code look nicer but the line `$: << some_directory` looks ugly. With **LoadPath** your code looks something like this:
|
41
|
+
And so forth. You can make the code look nicer but the line `$: << some_directory` looks ugly. With **LoadPath** your code looks something like this:
|
39
42
|
|
40
43
|
```
|
41
|
-
require 'load_path
|
44
|
+
require 'load_path'
|
42
45
|
|
43
|
-
LoadPath.configure do
|
46
|
+
LoadPath.configure do
|
44
47
|
add parent_directory('lib', up: 2)
|
45
48
|
add sibling_directory('utils')
|
46
49
|
add child_directory('others')
|
50
|
+
add path_buider { parent_directory('lib', up: 2).sibling_directory('bin') }
|
47
51
|
end
|
48
52
|
```
|
49
|
-
|
53
|
+
Notice the use of the `path_builder` block. This is required if you wish you chain paths together.
|
54
|
+
|
50
55
|
You can then proceed to require the files with `require` instead of `require_relative` as normal. **NOTE**: You must first have the desired directory on the load path before requiring files from it.
|
51
56
|
|
52
57
|
The configure block also lets you require your files in bulk. For example to require all files in the lib folder 2 levels above, you would write code like this:
|
@@ -55,11 +60,13 @@ The configure block also lets you require your files in bulk. For example to req
|
|
55
60
|
Dir.glob(File.join(this_dir, '..', '..', 'lib', '*.rb')) do |file|
|
56
61
|
require File.basename(file)
|
57
62
|
end
|
58
|
-
```
|
63
|
+
```
|
59
64
|
|
60
65
|
Which can be made cleaner with:
|
61
66
|
|
62
67
|
```
|
68
|
+
require 'load_path'
|
69
|
+
|
63
70
|
LoadPath.configure do
|
64
71
|
add parent_directory('lib', up: 2)
|
65
72
|
add sibling_directory('utils')
|
@@ -69,8 +76,19 @@ LoadPath.configure do
|
|
69
76
|
require_all sibling_directory('utils')
|
70
77
|
end
|
71
78
|
```
|
79
|
+
|
80
|
+
There is also a `PathBuilder` class to help you construct paths in your code. Here is how the class can be used:
|
81
|
+
|
82
|
+
```
|
83
|
+
require 'path_builder'
|
84
|
+
|
85
|
+
path_builder = LoadPath::PathBuilder.new('.')
|
86
|
+
config_file = path_builder.child_directory('config').file_path('config.yml')
|
87
|
+
|
88
|
+
```
|
89
|
+
|
72
90
|
### Examples
|
73
91
|
|
74
|
-
See also the example under
|
92
|
+
See also the example under tests directory. The following file system structure is used by the test:
|
75
93
|
|
76
|
-
<img src="https://github.
|
94
|
+
<img src="https://github.com/nayyara84/load-path/blob/master/img/test_case.png?raw=true" height="250">
|
data/lib/load_path.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'path_builder'
|
1
2
|
#
|
2
3
|
# This module makes the setting up of the load path for Ruby projects easy
|
3
4
|
# by the use of a simple configure() block. Also your statements to setup the load path
|
@@ -20,7 +21,7 @@ module LoadPath
|
|
20
21
|
# Construct the file location/path of the file that is using this module
|
21
22
|
def self.calling_class_path(call_stack)
|
22
23
|
me = __FILE__
|
23
|
-
configure_call_index = call_stack.
|
24
|
+
configure_call_index = call_stack.index { |call| call =~ /#{me}.*\:in.*configure/ }
|
24
25
|
external_call = call_stack[configure_call_index + 1]
|
25
26
|
begin
|
26
27
|
external_file_name = (external_call.match /(.*):\d+:in/)[1]
|
@@ -32,18 +33,12 @@ module LoadPath
|
|
32
33
|
end
|
33
34
|
|
34
35
|
# Helper class to setup the load path
|
35
|
-
class LoadPathSetup
|
36
|
-
attr_accessor :root_dir
|
37
|
-
|
38
|
-
# Construct with a root
|
39
|
-
def initialize(root_dir)
|
40
|
-
@root_dir = root_dir
|
41
|
-
end
|
36
|
+
class LoadPathSetup < PathHelper
|
42
37
|
|
43
38
|
# Require all files matching the pattern in the directory
|
44
39
|
def require_files(directory, file_pattern='*.rb')
|
45
40
|
Dir.glob(File.join(directory, file_pattern)) do |file|
|
46
|
-
require File.basename(file)
|
41
|
+
require File.basename(file.to_s)
|
47
42
|
end
|
48
43
|
end
|
49
44
|
|
@@ -52,21 +47,12 @@ module LoadPath
|
|
52
47
|
$: << directory
|
53
48
|
end
|
54
49
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
# Construct a child relative to the root by name
|
62
|
-
def child_directory(*directory_list)
|
63
|
-
File.join(root_dir, directory_list)
|
64
|
-
end
|
65
|
-
|
66
|
-
# Construct a sibling relative to the root by name
|
67
|
-
def sibling_directory(directory_name)
|
68
|
-
parent_directory(directory_name, up: 0)
|
50
|
+
def path_builder(&block)
|
51
|
+
builder = LoadPath::PathBuilder.new(self.file_path)
|
52
|
+
builder.instance_eval(&block)
|
53
|
+
builder.file_path
|
69
54
|
end
|
70
55
|
end
|
71
56
|
|
72
|
-
end
|
57
|
+
end
|
58
|
+
|
data/lib/path_builder.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
#
|
2
|
+
# A helper for building path on the file system
|
3
|
+
# Author: Nayyara Samuel (nayyara.samuel@opower.com)
|
4
|
+
#
|
5
|
+
module LoadPath
|
6
|
+
class PathHelper
|
7
|
+
attr_accessor :root_dir
|
8
|
+
|
9
|
+
# Construct with a root
|
10
|
+
def initialize(root_dir, expand_path=false)
|
11
|
+
if (expand_path)
|
12
|
+
@root_dir = File.expand_path(root_dir)
|
13
|
+
else
|
14
|
+
@root_dir = root_dir
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Construct a parent relative to the root by name
|
19
|
+
def parent_directory(directory_name, levels_up={up: 1})
|
20
|
+
levels_up = levels_up[:up].to_i + 1
|
21
|
+
File.join(root_dir, levels_up.times.map { |_| '..' }, directory_name)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Construct a child relative to the root by name
|
25
|
+
def child_directory(*directory_list)
|
26
|
+
File.join(root_dir, directory_list)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Construct a sibling relative to the root by name
|
30
|
+
def sibling_directory(directory_name)
|
31
|
+
parent_directory(directory_name, up: 0)
|
32
|
+
end
|
33
|
+
|
34
|
+
def file_path(file_name=nil)
|
35
|
+
if (file_name)
|
36
|
+
File.join(root_dir, file_name)
|
37
|
+
else
|
38
|
+
root_dir
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Builder for path helper
|
44
|
+
class PathBuilder
|
45
|
+
attr_accessor :path_helper
|
46
|
+
|
47
|
+
def initialize(root_dir, expand_path=false)
|
48
|
+
@path_helper = PathHelper.new(root_dir, expand_path)
|
49
|
+
end
|
50
|
+
|
51
|
+
def parent_directory(*args)
|
52
|
+
@path_helper = PathHelper.new(@path_helper.parent_directory(*args))
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
def child_directory(*args)
|
57
|
+
@path_helper = PathHelper.new(@path_helper.child_directory(*args))
|
58
|
+
self
|
59
|
+
end
|
60
|
+
|
61
|
+
def sibling_directory(*args)
|
62
|
+
@path_helper = PathHelper.new(@path_helper.sibling_directory(*args))
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
def file_path(file_name=nil)
|
67
|
+
@path_helper.file_path(file_name)
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_s
|
71
|
+
file_path
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: load_path
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Nayyara Samuel
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-15 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description:
|
15
14
|
email: nayyara.samuel@opower.com
|
@@ -19,11 +18,14 @@ extra_rdoc_files:
|
|
19
18
|
- README.md
|
20
19
|
files:
|
21
20
|
- lib/load_path.rb
|
21
|
+
- lib/path_builder.rb
|
22
|
+
- CHANGELOG.md
|
22
23
|
- Gemfile
|
23
24
|
- Rakefile
|
24
25
|
- README.md
|
25
26
|
homepage: https://github.com/nayyara84/load-path
|
26
27
|
licenses: []
|
28
|
+
metadata: {}
|
27
29
|
post_install_message:
|
28
30
|
rdoc_options:
|
29
31
|
- --title
|
@@ -35,21 +37,19 @@ require_paths:
|
|
35
37
|
- lib
|
36
38
|
- lib
|
37
39
|
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
-
none: false
|
39
40
|
requirements:
|
40
41
|
- - ! '>='
|
41
42
|
- !ruby/object:Gem::Version
|
42
43
|
version: '0'
|
43
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
-
none: false
|
45
45
|
requirements:
|
46
46
|
- - ! '>='
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
requirements: []
|
50
50
|
rubyforge_project:
|
51
|
-
rubygems_version: 1.
|
51
|
+
rubygems_version: 2.1.3
|
52
52
|
signing_key:
|
53
|
-
specification_version:
|
53
|
+
specification_version: 4
|
54
54
|
summary: Cleaner way to setup your load path
|
55
55
|
test_files: []
|