modulation 0.17 → 0.18

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: 5daf7340b5dc5c4e5efb09a751b1e1fe6bd65fc79a76182bf665174b3287e95c
4
- data.tar.gz: 6840631ce8268a819d47796b61d4b74dbabed5f5ad6e368c55705fdce40997aa
3
+ metadata.gz: 69a7acdf23627c8484110710afefe863049dec51ddf433d1dd607bd9ee9d4a56
4
+ data.tar.gz: fcef74b2bd0a0fd7450ab0d91b837c341d7c9dfd7b3964afa6bb4e15132ca974
5
5
  SHA512:
6
- metadata.gz: 5f89f42981f3418d165af0c978291259b21ffc2480f0dd4506f1701daa23fbde3f355950962ad081479e9d1ebc9dd68e11f02a23c644d90c8200cece2d372e80
7
- data.tar.gz: 760c2cfbfb460712f122782b57edbef1cb12ab030dbf82b5399ccc6c84a20056d70ecb8ce9eaae4b820d33305e60908b55df61ecbad24033e0f0e01d14214c6c
6
+ metadata.gz: 8e102e59feae6d84de4da96ce88ad45f389c678c883446e03db74d327c09045d7f0d22fca14e03aff5884f85cea56d8d2f6240335c7a75379f80fbc94df5c1c9
7
+ data.tar.gz: 3821d9bfc8d48939b24e3f7d94b134a28faabd595978747eae8628d50f9e5744015df7d1092576dfcaa377ce3911d28d11f1ede13d20492700d84ff754eba657
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ 0.18 2018-12-30
2
+ ---------------
3
+
4
+ * Add auto_import feature for lazy loading of modules
5
+
1
6
  0.17 2018-11-22
2
7
  ---------------
3
8
 
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Modulation - Explicit Dependency Management for Ruby
2
2
 
3
+ > Modulation | mɒdjʊˈleɪʃ(ə)n | *Music* - a change from one key to another in a
4
+ > piece of music.
5
+
3
6
  [INSTALL](#installing-modulation) |
4
7
  [GUIDE](#organizing-your-code-with-modulation) |
5
8
  [EXAMPLES](examples) |
@@ -31,11 +34,14 @@ a functional style, minimizing boilerplate code.
31
34
  - Supports circular dependencies.
32
35
  - Supports [default exports](#default-exports) for modules exporting a single
33
36
  class or value.
34
- - Can [reload](#reloading-modules) modules at runtime without breaking your
37
+ - Modules can be [reloaded](#reloading-modules) at runtime without breaking your
35
38
  code in wierd ways.
39
+ - Modules can be [lazy loaded](#lazy-loading) to improve start up time and
40
+ memory consumption.
36
41
  - Allows [mocking of dependencies](#mocking-dependencies) for testing purposes.
37
42
  - Can be used to [write gems](#writing-gems-using-modulation).
38
- - Facilitates [unit-testing](#unit-testing-modules) of private methods and constants.
43
+ - Facilitates [unit-testing](#unit-testing-modules) of private methods and
44
+ constants.
39
45
 
40
46
  ## Rationale
41
47
 
@@ -373,6 +379,44 @@ settings = import('settings')
373
379
  settings = settings.__reload!
374
380
  ```
375
381
 
382
+ ## Lazy Loading
383
+
384
+ Modulation allows the use of lazy-loaded modules - loading of modules only once
385
+ they're needed by the application, in similar fashion to `Module#auto_load`. To
386
+ lazy load modules use the `#auto_import` method, which takes a constant name and
387
+ a path:
388
+
389
+ ```ruby
390
+ export :foo
391
+
392
+ auto_import :BAR, './bar'
393
+
394
+ def foo
395
+ # the bar module will only be loaded once this method is called
396
+ MODULE::BAR
397
+ end
398
+ ```
399
+
400
+ > Lazy-loaded constants must always be qualified. When referring to a
401
+ > lazy-loaded constant from the module's top namespace, use the `MODULE`
402
+ > namespace, as shown above.
403
+
404
+ The `auto_import` method can also take a hash mapping constant names to paths.
405
+ This is especially useful when multiple concerns are grouped under a single
406
+ namespace:
407
+
408
+ ```ruby
409
+ export_default :SuperNet
410
+
411
+ module SuperNet
412
+ auto_import(
413
+ HTTP1: './http1',
414
+ HTTP2: './http2',
415
+ WebSockets: './websockets'
416
+ )
417
+ end
418
+ ```
419
+
376
420
  ## Writing gems using Modulation
377
421
 
378
422
  Modulation can be used to write gems, providing fine-grained control over your
@@ -13,6 +13,22 @@ end
13
13
 
14
14
  # Module extensions
15
15
  class Module
16
+ # Registers a constant to be lazy-loaded upon lookup
17
+ # @param sym [Symbol, Hash] constant name or hash mapping names to paths
18
+ # @param path [String] path if sym is Symbol
19
+ # @return [void]
20
+ def auto_import(sym, path = nil, caller_location = caller(1..1).first)
21
+ unless @__auto_import_registry
22
+ a = @__auto_import_registry = {}
23
+ define_auto_import_const_missing_method(@__auto_import_registry)
24
+ end
25
+ if path
26
+ @__auto_import_registry[sym] = [path, caller_location]
27
+ else
28
+ sym.each { |k, v| @__auto_import_registry[k] = [v, caller_location] }
29
+ end
30
+ end
31
+
16
32
  # Extends the receiver with exported methods from the given file name
17
33
  # @param path [String] module filename
18
34
  # @return [void]
@@ -32,6 +48,15 @@ class Module
32
48
  add_module_constants(mod, self)
33
49
  end
34
50
 
51
+ private
52
+
53
+ def define_auto_import_const_missing_method(auto_import_hash)
54
+ singleton_class.define_method(:const_missing) do |sym|
55
+ (path, caller_location) = auto_import_hash[sym]
56
+ path ? const_set(sym, import(path, caller_location)) : super
57
+ end
58
+ end
59
+
35
60
  def add_module_methods(mod, target)
36
61
  mod.singleton_class.instance_methods(false).each do |sym|
37
62
  target.send(:define_method, sym, &mod.method(sym))
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Modulation
4
- VERSION = '0.17'
4
+ VERSION = '0.18'
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.17'
4
+ version: '0.18'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-22 00:00:00.000000000 Z
11
+ date: 2018-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -38,12 +38,10 @@ dependencies:
38
38
  - - '='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 4.0.1
41
- description: |
42
- Modulation provides an better way to organize Ruby code. Modulation lets
43
- you explicitly import and export declarations in order to better control
44
- dependencies in your codebase. Modulation helps you refrain from littering
45
- the global namespace with a myriad modules, or declaring complex nested
46
- class hierarchies.
41
+ description: " Modulation provides an alternative way of organizing your Ruby code.
42
+ Modulation\nlets you explicitly import and export declarations in order to better
43
+ control \ndependencies in your codebase. Modulation helps you refrain from littering\nthe
44
+ global namespace with a myriad modules, or complex multi-level nested\nmodule hierarchies.\n"
47
45
  email: ciconia@gmail.com
48
46
  executables:
49
47
  - rbm
@@ -87,9 +85,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
85
  - !ruby/object:Gem::Version
88
86
  version: '0'
89
87
  requirements: []
90
- rubyforge_project:
91
- rubygems_version: 2.7.3
88
+ rubygems_version: 3.0.1
92
89
  signing_key:
93
90
  specification_version: 4
94
- summary: 'Modulation: better dependency management for Ruby'
91
+ summary: 'Modulation: explicit dependency management for Ruby'
95
92
  test_files: []