modulation 0.19 → 0.20

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 213cf5d64fdb238b965b7c625c0bc513da79cc9866c52c4051c02bc309c93eec
4
- data.tar.gz: fffb3da5ee7f10934e6386da7fa01611934ba217ee6df33a6e6d6f7715148352
3
+ metadata.gz: e540dd13f308e46a50219db2bafb9b72c692dfc04a43404dcddc85c6117c5f7b
4
+ data.tar.gz: 64de165287b53651385fb6e520f15d493e360599afb85c90ca90b91af1aa49e8
5
5
  SHA512:
6
- metadata.gz: 11c410d6b8eacff67ba95484189121e7d6488c88e3a2278e4fa6b241f341a17c5dd5a4c281809567e321a6d2933d0fd519685b890eb0419d8db2e4b4aed60a5c
7
- data.tar.gz: c0f58c853854d576325b9fdf45b9dba78d69cf8e373807b6d656733fd7d4192673eb3bdf6cb6ccce8b33ae131b3c70ac2f98da1d00fa0625fe2d55fcc686338e
6
+ metadata.gz: 3b36c16543e39b4f00afe5129e80f430b56f757258b49785c7a22b6abed52896951b43a6fdc97397807bc95da188829622ff398dfbdcf7ea0e48e701aaa4ece6
7
+ data.tar.gz: ff3a3c843a5c2f5e2401fb55c16f19b39281d05acf71c4e1a9d5b7838e7de571be4c4e41657db438724709c768909a077a43d3aaac826689fa485c2ac05a65c3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ 0.20 2019-01-16
2
+ ---------------
3
+
4
+ * Add import_all method
5
+
1
6
  0.19 2019-01-05
2
7
  ---------------
3
8
 
data/README.md CHANGED
@@ -82,6 +82,7 @@ easy to understand.
82
82
  - Can be used to [write gems](#writing-gems-using-modulation).
83
83
  - Facilitates [unit-testing](#unit-testing-modules) of private methods and
84
84
  constants.
85
+ - Can load all source files in directory at once.
85
86
 
86
87
  ## Installing Modulation
87
88
 
@@ -171,6 +172,39 @@ user = User.new(...)
171
172
  > **Note about paths**: module paths are always relative to the file
172
173
  > calling the `import` method, just like `require_relative`.
173
174
 
175
+ ### Importing all source files in a directory
176
+
177
+ To load all source files in a directory you can use `import_all`:
178
+
179
+ ```ruby
180
+ import_all('./ext') # will load ./ext/kernel, ./ext/socket etc
181
+ ```
182
+
183
+ ### Importing methods into classes and modules
184
+
185
+ Modulation provides the `extend_from` and `include_from` methods to include
186
+ imported methods in classes and modules:
187
+
188
+ ```ruby
189
+ module Sequences
190
+ extend_from('./seq.rb')
191
+ end
192
+
193
+ Sequences.fib(5)
194
+
195
+ # extend integers
196
+ require 'modulation'
197
+ class Integer
198
+ include_from('./seq.rb')
199
+
200
+ def seq(kind)
201
+ send(kind, self)
202
+ end
203
+ end
204
+
205
+ 5.seq(:fib)
206
+ ```
207
+
174
208
  ### Default exports
175
209
 
176
210
  A module may wish to expose just a single class or constant, in which case it
@@ -210,31 +244,6 @@ config = import('./config')
210
244
  db.connect(config[:host], config[:port])
211
245
  ```
212
246
 
213
- ### Importing methods into classes and modules
214
-
215
- Modulation provides the `extend_from` and `include_from` methods to include
216
- imported methods in classes and modules:
217
-
218
- ```ruby
219
- module Sequences
220
- extend_from('./seq.rb')
221
- end
222
-
223
- Sequences.fib(5)
224
-
225
- # extend integers
226
- require 'modulation'
227
- class Integer
228
- include_from('./seq.rb')
229
-
230
- def seq(kind)
231
- send(kind, self)
232
- end
233
- end
234
-
235
- 5.seq(:fib)
236
- ```
237
-
238
247
  ### Accessing a module's root namespace from nested modules within itself
239
248
 
240
249
  The special constant `MODULE` allows you to access the containing module from
@@ -45,6 +45,17 @@ module Modulation
45
45
  end
46
46
  end
47
47
 
48
+ # Imports all source files in given directory
49
+ # @ param path [String] relative directory path
50
+ # @param caller_location [String] caller location
51
+ # @return [Array] array of module objects
52
+ def import_all(path, caller_location = caller(1..1).first)
53
+ abs_path = Paths.absolute_dir_path(path, caller_location)
54
+ Dir["#{abs_path}/**/*.rb"].map do |fn|
55
+ @loaded_modules[fn] || create_module_from_file(fn)
56
+ end
57
+ end
58
+
48
59
  # Creates a new module from a source file
49
60
  # @param path [String] source file name
50
61
  # @return [Module] module
@@ -2,13 +2,21 @@
2
2
 
3
3
  # Kernel extensions
4
4
  module Kernel
5
- # Returns an encapsulated imported module.
5
+ # Imports a module
6
6
  # @param path [String] module file name
7
7
  # @param caller_location [String] caller location
8
- # @return [Class] module facade
8
+ # @return [Module] module object
9
9
  def import(path, caller_location = caller(1..1).first)
10
10
  Modulation.import(path, caller_location)
11
11
  end
12
+
13
+ # Imports all modules in given directory
14
+ # @param path [String] directory path
15
+ # @param caller_location [String] caller location
16
+ # @return [Array] array of module objects
17
+ def import_all(path, caller_location = caller(1..1).first)
18
+ Modulation.import_all(path, caller_location)
19
+ end
12
20
  end
13
21
 
14
22
  # Module extensions
@@ -20,6 +20,18 @@ module Modulation
20
20
  check_path(path)
21
21
  end
22
22
 
23
+ # Computes and verifies the absolute directory path
24
+ # @param path String] unqualified path
25
+ # @param caller_location [String] caller location
26
+ # @return [String] absolute directory path
27
+ def absolute_dir_path(path, caller_location)
28
+ caller_file = caller_location[CALLER_FILE_REGEXP, 1]
29
+ return nil unless caller_file
30
+
31
+ path = File.expand_path(path, File.dirname(caller_file))
32
+ File.directory?(path) ? path : (raise "Invalid directory #{path}")
33
+ end
34
+
23
35
  # Checks that the given path references an existing file, adding the .rb
24
36
  # extension if needed
25
37
  # @param path [String] absolute file path (with/without .rb extension)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Modulation
4
- VERSION = '0.19'
4
+ VERSION = '0.20'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modulation
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.19'
4
+ version: '0.20'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-05 00:00:00.000000000 Z
11
+ date: 2019-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest