require_dir 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,13 @@
1
- sudo: false
2
1
  language: ruby
2
+ env:
3
+ - CODECLIMATE_REPO_TOKEN=703cb4759e977bdd9502305ae454a0acdf7bd0d51d418e88048110f4735c4a80
3
4
  rvm:
5
+ - 2.2.3
4
6
  - 2.3.0
5
- before_install: gem install bundler -v 1.12.4
7
+ script: "bundle exec rspec"
8
+ notifications:
9
+ email:
10
+ recipients:
11
+ - kigster@gmail.com
12
+ on_success: change
13
+ on_failure: always
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
-
3
2
  # Specify your gem's dependencies in require_dir.gemspec
4
3
  gemspec
data/README.md CHANGED
@@ -1,4 +1,15 @@
1
- # RequireDir
1
+ # RequireDir
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/require_dir.svg)](https://badge.fury.io/rb/require_dir)
4
+ [![Downloads](http://ruby-gem-downloads-badge.herokuapp.com/require_dir?type=total)](https://rubygems.org/gems/require_dir)
5
+
6
+ <br />
7
+
8
+ [![Build Status](https://travis-ci.org/kigster/require_dir.svg?branch=master)](https://travis-ci.org/kigster/warp-dir)
9
+ [![Code Climate](https://codeclimate.com/github/kigster/require_dir/badges/gpa.svg)](https://codeclimate.com/githb/kigster/require_dir)
10
+ [![Test Coverage](https://codeclimate.com/github/kigster/require_dir/badges/coverage.svg)](https://codeclimate.com/github/kigster/require_dir/coverage)
11
+ [![Issue Count](https://codeclimate.com/github/kigster/require_dir/badges/issue_count.svg)](https://codeclimate.com/github/kigster/require_dir)
12
+
2
13
 
3
14
  This gem provides an easy way to require all file from a folder – recursively, or not.
4
15
 
@@ -6,7 +17,7 @@ Unlike other gems, such as `require_all`, this gem does suffer from module clobb
6
17
 
7
18
  ## Author
8
19
 
9
- This library is the work of [Konstantin Gredeskoul](http:/kig.re), &copy; 2016.
20
+ This library is the work of [Konstantin Gredeskoul](http:/kig.re), &copy; 2016, distributed under the MIT license.
10
21
 
11
22
  ## Installation
12
23
 
@@ -33,7 +44,7 @@ Recommended usage is to include this gem's module into the top level module of y
33
44
  require 'require_dir'
34
45
  module Mylib
35
46
  extend RequireDir
36
- init_from_source __FILE__
47
+ init __FILE__
37
48
  end
38
49
 
39
50
  Mylib.dir('mylib/subfolder') # loads all files in the folder 'lib/mylib/subfolder/*.rb'
@@ -41,6 +52,43 @@ Mylib.dir_r('mylib/subfolder') # recursive load from 'lib/mylib/subfolder/**/*.r
41
52
 
42
53
  ```
43
54
 
55
+ ### Offset
56
+
57
+ You can optionally load the library using `init_with_offset` method, which allows you
58
+ to initialize the library from a file located not directly under lib, but further down below.
59
+
60
+ Say you want to initialize the library from `lib/mylib/subfolder/mymodule.rb`. But you want to
61
+ be able to later use RequireDir to load files relative to `lib`. This is how you would do that:
62
+
63
+
64
+ ```ruby
65
+ # file 'lib/mylib/subfolder/mymodule.rb' -- top level file for a gem 'mylib'
66
+ require 'require_dir'
67
+ module Mylib
68
+ module SubFolder
69
+ extend RequireDir
70
+ init_with_offset(__FILE__, 1)
71
+ end
72
+ end
73
+
74
+ Mylib.dir('mylib/subfolder') # loads all files in the folder 'lib/mylib/subfolder/*.rb'
75
+ Mylib.dir_r('mylib/subfolder') # recursive load from 'lib/mylib/subfolder/**/*.rb'
76
+ ```
77
+
78
+ ### Load Errors
79
+
80
+ The library attempts to catch load errors and print out (to STDERR) meaningful and colorful messages.
81
+
82
+ But sometimes that's not enough – and you may want to see which file was RequireDir loading *just before* it
83
+ exploded. You can enable debugging output using two methods:
84
+
85
+ * Set environment variable `REQUIRE_DIR_DEBUG`
86
+ * Initialize library with options hash, setting:
87
+
88
+ ```ruby
89
+ extend RequireDir
90
+ init __FILE__, debug: true
91
+ ```
44
92
 
45
93
  ## Development
46
94
 
@@ -50,7 +98,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
50
98
 
51
99
  ## Contributing
52
100
 
53
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/require_dir.
101
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kigster/require_dir.
54
102
 
55
103
  ## License
56
104
 
@@ -10,11 +10,17 @@ module RequireDir
10
10
  File.dirname(File.expand_path(source + dirs_up))
11
11
  end
12
12
 
13
- def init_from_source(source, offset = 0, options = {})
13
+ def init_with_offset(source, offset = 0, options = {})
14
14
  project_folder = project_folder_from(source: source, offset: offset)
15
15
  self.loader = RequireDir::Loader.new(project_folder, options)
16
16
  end
17
17
 
18
+ def init(source, options = {})
19
+ init_with_offset(source, 0, options)
20
+ end
21
+
22
+ alias_method :init_from_source, :init_with_offset
23
+
18
24
  extend Forwardable
19
25
  def_delegators :@loader, :dir, :dir_r
20
26
 
@@ -14,13 +14,29 @@ module RequireDir
14
14
  self.options = options
15
15
  end
16
16
 
17
+ def debug?
18
+ options[:debug] || ENV['REQUIRE_DIR_DEBUG']
19
+ end
20
+
17
21
  def dir(folder, recursive = false)
18
22
  folder = "/#{folder}" unless folder.start_with? '/'
19
- loader = self
20
23
  ::Dir.glob(project_root + folder + (recursive ? '/**/*.rb' : '/*.rb') ) do |file|
21
- puts "Loading #{file}" if loader.options[:debug]
22
- Kernel.require(file)
24
+ puts "Loading #{file}" if debug?
25
+ begin
26
+ Kernel.require(file)
27
+ rescue SyntaxError, LoadError => e
28
+ len = file.length + 6
29
+ STDERR.puts '—' * len
30
+ STDERR.puts "⇨ #{file.bold.yellow} ⇦".bold.white
31
+ STDERR.puts '—' * len
32
+ STDERR.puts e.message.bold.red
33
+ STDERR.puts '—' * len
34
+ STDERR.puts e.backtrace.join("\n").bold.black if e.backtrace && !e.backtrace.empty?
35
+ exit 1
36
+ end
23
37
  end
38
+
39
+
24
40
  end
25
41
 
26
42
  def dir_r(folder)
@@ -1,3 +1,3 @@
1
1
  module RequireDir
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency 'bundler', '~> 1.12'
21
21
  spec.add_development_dependency 'rake', '~> 10.0'
22
22
  spec.add_development_dependency 'rspec', '~> 3.0'
23
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.5'
23
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: require_dir
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Gredeskoul
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-03 00:00:00.000000000 Z
11
+ date: 2016-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codeclimate-test-reporter
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.5'
55
69
  description: 'Easily and non-intrusively require files from sub-folders. Without polluting
56
70
  global namespace, or having modules clobber each other, include RequireDir and initialize
57
71
  it to get access to #dir and #dir_r'
@@ -61,8 +75,10 @@ executables: []
61
75
  extensions: []
62
76
  extra_rdoc_files: []
63
77
  files:
78
+ - ".codeclimate.yml"
64
79
  - ".gitignore"
65
80
  - ".rspec"
81
+ - ".rubocop.yml"
66
82
  - ".travis.yml"
67
83
  - Gemfile
68
84
  - LICENSE.txt