requires 0.2.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +41 -19
  3. data/lib/requires/version.rb +1 -0
  4. data/lib/requires.rb +35 -18
  5. metadata +16 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57f35e16c778fa287257e4ea4b2bbaab9b38f02e56c54b21fe9e85045ead97e2
4
- data.tar.gz: 3a744da142ac5de0a32f44504a3459c1111cf4e6b11a0cf3f6de9745b33872d7
3
+ metadata.gz: 1b0b0f2f8e0b604e3f3061b95a695fe537a0c7286c2d4cfea8a27578ec828019
4
+ data.tar.gz: 60ecc91cb37fb1a90baaed74fcc911f7644cb6195fab02279a39e1608b6dada7
5
5
  SHA512:
6
- metadata.gz: e7f8e780f10352145323af0e402935d12ddb31b7a335fcb8ecc4e63d76f40118a53a6d1cd0127edb5aa9d9bdfef39bc20e3589b5e6bf9905b5d4d7d095e5321b
7
- data.tar.gz: 7d4aa0fb6adcf3bf6725d2b78d2798326e3ba0de6ca83ce155a376348bb56c23efd2d163ed948a068f9b44e0dfe39ba2a5db9090c4958449aa14c7e26446a254
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://travis-ci.com/DannyBen/requires.svg?branch=master)](https://travis-ci.com/DannyBen/requires)
4
+ [![Build Status](https://github.com/DannyBen/requires/workflows/Test/badge.svg)](https://github.com/DannyBen/requires/actions?query=workflow%3ATest)
6
5
 
7
- **Requires** is ruby's missing `require` function.
6
+ A tiny convenience function to require or autoload all ruby files in a directory.
8
7
 
9
- It lets you...
8
+ ## Install
10
9
 
11
- ```ruby
12
- # ...require multiple files
13
- requires 'any_file', 'with/or/without/extension.rb'
10
+ ```bash
11
+ $ gem install requires
12
+ ```
13
+
14
+ ## Usage
14
15
 
15
- # ...require directories
16
- requires 'first/directory', 'second-directory'
16
+ ### `requires`
17
17
 
18
- # ...require with a glob
19
- requires 'base_*', 'directory/*_base'
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
- # ...require any other library
22
- requires 'yaml', 'lib/important', 'lib'
27
+ # ...as well as external gems or built in libraries
28
+ requires 'yaml'
23
29
  ```
24
30
 
25
- Everything is relative to where `requires` is called from.
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
- Installation
31
- --------------------------------------------------
37
+ ```ruby
38
+ require 'requires'
39
+ autoloads 'lib', %i[Asset SomeAPI HTTPClient]
40
+ ```
32
41
 
33
- $ gem install requires
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(*items)
2
- items = ['.'] if items.empty?
3
-
4
- calling_script = caller.first.sub(/:\d+.*/, '')
5
- base_dir = File.dirname calling_script
6
-
7
- Dir.chdir base_dir do
8
- items.each do |item|
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: 0.2.0
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: 2020-01-09 00:00:00.000000000 Z
11
+ date: 2024-07-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A simple gem to require them all!
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
- post_install_message:
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: 2.3.0
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.0.3
41
- signing_key:
46
+ rubygems_version: 3.5.14
47
+ signing_key:
42
48
  specification_version: 4
43
- summary: Ruby's missing require function
49
+ summary: Require all files in a directory
44
50
  test_files: []