requires 0.2.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +41 -19
- data/lib/requires/version.rb +1 -0
- data/lib/requires.rb +35 -18
- metadata +16 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b0b0f2f8e0b604e3f3061b95a695fe537a0c7286c2d4cfea8a27578ec828019
|
4
|
+
data.tar.gz: 60ecc91cb37fb1a90baaed74fcc911f7644cb6195fab02279a39e1608b6dada7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d58d1cb6fe90169fe7fae10fac2a7c5c7af8394544b21d3b488d6277012f00dc85e8dee880a1d49c3ac826e144be237bb5cf2d46bbb11953f6e4eb54d11a6af
|
7
|
+
data.tar.gz: 5af9b748b2bff2349b798e9cc60f2ad43eee55c546dc93b9bc0089b5ce1073fc832c87bb1e15e040d24d1de0392ddf794c02bb89da0936d9e728a2734e2138dc
|
data/README.md
CHANGED
@@ -1,35 +1,57 @@
|
|
1
|
-
Requires
|
2
|
-
==================================================
|
1
|
+
# Requires
|
3
2
|
|
4
3
|
[![Gem Version](https://badge.fury.io/rb/requires.svg)](https://badge.fury.io/rb/requires)
|
5
|
-
[![Build Status](https://
|
4
|
+
[![Build Status](https://github.com/DannyBen/requires/workflows/Test/badge.svg)](https://github.com/DannyBen/requires/actions?query=workflow%3ATest)
|
6
5
|
|
7
|
-
|
6
|
+
A tiny convenience function to require or autoload all ruby files in a directory.
|
8
7
|
|
9
|
-
|
8
|
+
## Install
|
10
9
|
|
11
|
-
```
|
12
|
-
|
13
|
-
|
10
|
+
```bash
|
11
|
+
$ gem install requires
|
12
|
+
```
|
13
|
+
|
14
|
+
## Usage
|
14
15
|
|
15
|
-
|
16
|
-
requires 'first/directory', 'second-directory'
|
16
|
+
### `requires`
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
```ruby
|
19
|
+
# Require a directory (recursively)
|
20
|
+
require 'requires'
|
21
|
+
requires 'lib'
|
22
|
+
|
23
|
+
# Individual files can also be loaded (with or without extension)
|
24
|
+
requires 'lib/base'
|
25
|
+
requires 'lib/base.rb'
|
20
26
|
|
21
|
-
# ...
|
22
|
-
requires 'yaml'
|
27
|
+
# ...as well as external gems or built in libraries
|
28
|
+
requires 'yaml'
|
23
29
|
```
|
24
30
|
|
25
|
-
|
31
|
+
All paths are relative to the location of the file that calls `requires`.
|
26
32
|
|
27
|
-
|
33
|
+
### `autoloads`
|
28
34
|
|
35
|
+
Autoload en masse, with paths relative to the one calling `autuoloads`.
|
29
36
|
|
30
|
-
|
31
|
-
|
37
|
+
```ruby
|
38
|
+
require 'requires'
|
39
|
+
autoloads 'lib', %i[Asset SomeAPI HTTPClient]
|
40
|
+
```
|
32
41
|
|
33
|
-
|
42
|
+
which is equivalent to these native autoload statements:
|
34
43
|
|
44
|
+
```ruby
|
45
|
+
autoload :Asset, './lib/asset'
|
46
|
+
autoload :SomeAPI, './lib/some_api'
|
47
|
+
autoload :HTTPClient, './lib/HTTPClient'
|
48
|
+
```
|
35
49
|
|
50
|
+
In case you wish to autoload from the same directory, you can omit the first
|
51
|
+
argument:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
autoloads %i[Asset SomeAPI HTTPClient]
|
55
|
+
# which is the same as
|
56
|
+
# autoloads '.', %i[Asset SomeAPI HTTPClient]
|
57
|
+
```
|
@@ -0,0 +1 @@
|
|
1
|
+
VERSION = '1.1.0'
|
data/lib/requires.rb
CHANGED
@@ -1,21 +1,38 @@
|
|
1
|
-
def requires(
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
if item.include? '*'
|
10
|
-
item += ".rb" unless item.end_with? '.rb'
|
11
|
-
Dir["#{item}"].sort.each { |file| require "./#{file}" }
|
12
|
-
elsif File.directory? item
|
13
|
-
Dir["#{item}/**/*.rb"].sort.each { |file| require "./#{file}" }
|
14
|
-
elsif File.file? "#{item}.rb" or File.file? item
|
15
|
-
require "./#{item}"
|
16
|
-
else
|
17
|
-
require item
|
18
|
-
end
|
1
|
+
def requires(item)
|
2
|
+
base_path = caller_locations(1..1).first.path
|
3
|
+
base_dir = File.dirname base_path
|
4
|
+
path = File.expand_path item, base_dir
|
5
|
+
|
6
|
+
if File.directory? path
|
7
|
+
Dir["#{path}/**/*.rb"].each do |file|
|
8
|
+
require file
|
19
9
|
end
|
10
|
+
|
11
|
+
elsif File.file?("#{path}.rb") || File.file?(path.to_s)
|
12
|
+
require path
|
13
|
+
|
14
|
+
else
|
15
|
+
require item
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def autoloads(base, symbols = nil)
|
21
|
+
unless symbols
|
22
|
+
symbols = base
|
23
|
+
base = '.'
|
24
|
+
end
|
25
|
+
|
26
|
+
base_path = caller_locations(1..1).first.path
|
27
|
+
base_dir = File.dirname base_path
|
28
|
+
path = File.expand_path base, base_dir
|
29
|
+
|
30
|
+
symbols.each do |symbol|
|
31
|
+
file = symbol.to_s
|
32
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') # Break consecutive uppercase letters before the last capital
|
33
|
+
.gsub(/([a-z\d])([A-Z])/, '\1_\2') # Break lowercase or digits before an uppercase letter
|
34
|
+
.downcase
|
35
|
+
|
36
|
+
autoload symbol, "#{path}/#{file}"
|
20
37
|
end
|
21
38
|
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: requires
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Adds functions that allow requiring a direcotry
|
14
14
|
email: db@dannyben.com
|
15
15
|
executables: []
|
16
16
|
extensions: []
|
@@ -18,11 +18,17 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- README.md
|
20
20
|
- lib/requires.rb
|
21
|
+
- lib/requires/version.rb
|
21
22
|
homepage: https://github.com/dannyben/requires
|
22
23
|
licenses:
|
23
24
|
- MIT
|
24
|
-
metadata:
|
25
|
-
|
25
|
+
metadata:
|
26
|
+
bug_tracker_uri: https://github.com/DannyBen/requires/issues
|
27
|
+
changelog_uri: https://github.com/DannyBen/requires/blob/master/CHANGELOG.md
|
28
|
+
homepage_uri: https://requires.dannyb.co/
|
29
|
+
source_code_uri: https://github.com/DannyBen/requires
|
30
|
+
rubygems_mfa_required: 'true'
|
31
|
+
post_install_message:
|
26
32
|
rdoc_options: []
|
27
33
|
require_paths:
|
28
34
|
- lib
|
@@ -30,15 +36,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
36
|
requirements:
|
31
37
|
- - ">="
|
32
38
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
39
|
+
version: '3.0'
|
34
40
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
41
|
requirements:
|
36
42
|
- - ">="
|
37
43
|
- !ruby/object:Gem::Version
|
38
44
|
version: '0'
|
39
45
|
requirements: []
|
40
|
-
rubygems_version: 3.
|
41
|
-
signing_key:
|
46
|
+
rubygems_version: 3.5.14
|
47
|
+
signing_key:
|
42
48
|
specification_version: 4
|
43
|
-
summary:
|
49
|
+
summary: Require all files in a directory
|
44
50
|
test_files: []
|