load_path 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Gemfile +3 -0
  2. data/README.md +76 -0
  3. data/Rakefile +1 -0
  4. data/lib/load_path.rb +72 -0
  5. metadata +55 -0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # <img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcRb1vaCQj_2gIgEdX5Fly1vMjfbFKYkuLW1P5c4-3dcFyQ3OqgZoTiOto_8" height="50"> Load Path
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.
4
+
5
+ ===================
6
+
7
+ ### Contributing
8
+
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
+
11
+ ### Installing
12
+
13
+ To build the gem use the rake commands for gem tasks. To install:
14
+
15
+ rake install
16
+
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:
20
+
21
+ ```
22
+ this_dir = File.dirname(__FILE__)
23
+
24
+ #Add the folder lib 2 levels above
25
+ lib_dir = File.join(this_dir, '..', '..', 'lib')
26
+ $: << lib_dir
27
+
28
+ #Add the folder utils to load path
29
+ utils_dir = File.join(this_dir, '..', 'utils')
30
+ $: << utils_dir
31
+
32
+ #Add the folder others under this directory to load path
33
+ others_dir = File.join(this_dir, 'others')
34
+ $: << others_dir
35
+
36
+ ```
37
+
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:
39
+
40
+ ```
41
+ require 'load_path`
42
+
43
+ LoadPath.configure do
44
+ add parent_directory('lib', up: 2)
45
+ add sibling_directory('utils')
46
+ add child_directory('others')
47
+ end
48
+ ```
49
+
50
+ 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
+
52
+ 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:
53
+
54
+ ```
55
+ Dir.glob(File.join(this_dir, '..', '..', 'lib', '*.rb')) do |file|
56
+ require File.basename(file)
57
+ end
58
+ ```
59
+
60
+ Which can be made cleaner with:
61
+
62
+ ```
63
+ LoadPath.configure do
64
+ add parent_directory('lib', up: 2)
65
+ add sibling_directory('utils')
66
+
67
+ require_all parent_directory('lib', up: 2), '*.rb'
68
+ # Default is '*.rb', so missing second option works for Ruby source files
69
+ require_all sibling_directory('utils')
70
+ end
71
+ ```
72
+ ### Examples
73
+
74
+ See also the example under Rspec tests with directory structure:
75
+
76
+ <img src="https://github.va.opower.it/nayyara-samuel/load-path/raw/master/img/test_case.png" height="250">
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/load_path.rb ADDED
@@ -0,0 +1,72 @@
1
+ #
2
+ # This module makes the setting up of the load path for Ruby projects easy
3
+ # by the use of a simple configure() block. Also your statements to setup the load path
4
+ # look cleaner and clearer
5
+ #
6
+ # See the README.md file for detailed examples.
7
+ #
8
+ # Author: Nayyara Samuel (nayyara.samuel@opower.com)
9
+ #
10
+ module LoadPath
11
+
12
+ # Primary method to setup your load path
13
+ def self.configure(&block)
14
+ root = calling_class_path(caller(0))
15
+ # Evaluate the block on an instance of the load path setup
16
+ # that understand the files required
17
+ LoadPathSetup.new(root).instance_eval(&block)
18
+ end
19
+
20
+ # Construct the file location/path of the file that is using this module
21
+ def self.calling_class_path(call_stack)
22
+ me = __FILE__
23
+ configure_call_index = call_stack.rindex { |call| call =~ /#{me}.*\:in.*configure/ }
24
+ external_call = call_stack[configure_call_index + 1]
25
+ begin
26
+ external_file_name = (external_call.match /(.*):\d+:in/)[1]
27
+ external_file_directory = File.expand_path(File.dirname(external_file_name))
28
+ rescue
29
+ raise "Cannot infer calling file's name from #{external_call}"
30
+ end
31
+ return external_file_directory
32
+ end
33
+
34
+ # 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
42
+
43
+ # Require all files matching the pattern in the directory
44
+ def require_files(directory, file_pattern='*.rb')
45
+ Dir.glob(File.join(directory, file_pattern)) do |file|
46
+ require File.basename(file)
47
+ end
48
+ end
49
+
50
+ # Add the given directory to the load path
51
+ def add(directory)
52
+ $: << directory
53
+ end
54
+
55
+ # Construct a parent relative to the root by name
56
+ def parent_directory(directory_name, levels_up={up: 1})
57
+ levels_up = levels_up[:up].to_i + 1
58
+ File.join(root_dir, levels_up.times.map { |_| '..' }, directory_name)
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)
69
+ end
70
+ end
71
+
72
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: load_path
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nayyara Samuel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email: nayyara.samuel@opower.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.md
20
+ files:
21
+ - lib/load_path.rb
22
+ - Gemfile
23
+ - Rakefile
24
+ - README.md
25
+ homepage: https://github.com/nayyara84/load-path
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options:
29
+ - --title
30
+ - load_path
31
+ - --main
32
+ - README.md
33
+ - -ri
34
+ require_paths:
35
+ - lib
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Cleaner way to setup your load path
55
+ test_files: []