项目作者: digital-fabric

项目描述 :
Modulation - explicit dependency management for Ruby
高级语言: Ruby
项目地址: git://github.com/digital-fabric/modulation.git
创建时间: 2018-07-07T05:39:35Z
项目社区:https://github.com/digital-fabric/modulation

开源协议:MIT License

下载


Modulation - Explicit Dependency Management for Ruby

Gem Version
Modulation Test
MIT licensed

Modulation | mɒdjʊˈleɪʃ(ə)n | Music - a change from one key to another in a
piece of music.

INSTALL |
GUIDE |
API |
EXAMPLES |
RDOC

Modulation provides an alternative way of organizing your Ruby code. Modulation
lets you explicitly import and export declarations in order to better control
dependencies in your codebase. Modulation helps you refrain from littering
the global namespace with a myriad modules, or complex multi-level nested
module hierarchies.

Using Modulation, you will always be able to tell where a class or module comes
from, and you’ll have full control over which parts of a module’s code you wish
to expose to the outside world. Modulation can also help you write Ruby code in
a functional style, minimizing boilerplate code.

Note: Modulation is not a replacement for RubyGems. Rather, Modulation is
intended for managing dependencies between source files inside your Ruby
applications. Though it does support loading gems that were written using
Modulation, it is not intended as a comprehensive solution for using
third-party libraries.

Features

Rationale

You’re probably asking yourself “what the **?” , but when your Ruby app grows
and is split into multiple files loaded using #require, you’ll soon hit some
issues:

  • Once a file is #required, any class, module or constant in it is available
    to any other file in your codebase. All “globals” (classes, modules,
    constants) are loaded, well, globally, in a single namespace. Name conflicts
    are easy in Ruby.
  • To avoid class name conflicts, classes need to be nested under a single
    hierarchical tree, sometime reaching 4 levels or more. Just look at Rails.
  • Since a #required class or module can be loaded in any file and then made
    available to all files, it’s easy to lose track of where it was loaded, and
    where it is used.
  • There’s no easy way to hide implementation-specific classes or methods. Yes,
    there’s #private, #private_constant etc, but by default everything is
    #public!
  • Extracting functionality is harder when modules are namespaced and
    dependencies are implicit.
  • Writing reusable functional code requires wrapping it in modules using
    class << self, def self.foo ..., extend self or include Singleton
    (the pain of implementing singletons in Ruby has been
    discussed
    before.)

There’s a recent discussion on the
Ruby bug tracker regarding possible solutions to the problem of top-level
name collision. Hopefully, the present gem could contribute to an eventual
“official” API.

Personally, I have found that managing dependencies with #require in large
codebases is… not as elegant or painfree as I would expect from a
first-class development environment. I also wanted to have a better solution
for writing in a functional style.

So I came up with Modulation, a small gem that takes a different approach to
organizing Ruby code: any so-called global declarations are hidden unless
explicitly exported, and the global namespace remains clutter-free. All
dependencies between source files are explicit, visible, and easy to understand.

Installing Modulation

You can install the Modulation using gem install, or add it to your Gemfile:

  1. gem 'modulation'

Organizing your code with Modulation

Modulation builds on the idea of a Ruby Module as a
“collection of methods and constants”.
Using modulation, each Ruby source file becomes a module. Modules usually
export method and constant declarations (usually an API for a specific,
well-defined functionality) to be shared with other modules. Modules can also
import declarations from other modules. Anything not exported remains hidden
inside the module and normally cannot be accessed from the outside.

Each source file is evaluated in the context of a newly-created Module
instance, with some additional methods for introspection and miscellaneous
operations such as hot reloading.

Modulation provides alternative APIs for loading modules. Instead of using
require and require_relative, we use import, import_map etc, discussed
in detail in the API reference.

Basic Usage

Exporting declarations

Any class, module or constant be exported using #export:

  1. export :User, :Session
  2. class User
  3. ...
  4. end
  5. class Session
  6. ...
  7. end

A module may also expose a set of methods without using class << self, for
example when writing in a functional style:

seq.rb

  1. export :fib, :luc
  2. def fib(n)
  3. (0..1).include?(n) ? n : (fib(n - 1) + fib(n - 2))
  4. end
  5. def luc(n)
  6. (0..1).include?(n) ? (2 - n) : (luc(n - 1) + luc(n - 2))
  7. end

app.rb

  1. require 'modulation'
  2. Seq = import('./seq')
  3. puts Seq.fib(10)

Another way to export methods and constants is by passing a hash to #export:

module.rb

  1. export(
  2. foo: :bar,
  3. baz: -> { 'hello' },
  4. MY_CONST: 42
  5. )
  6. def bar
  7. :baz
  8. end

app.rb

  1. m = import('./module')
  2. m.foo #=> :baz
  3. m.baz #=> 'hello'
  4. m::MY_CONST #=> 42

Any capitalized key will be interpreted as a const, otherwise it will be defined
as a method. If the value is a symbol, Modulation will look for the
corresponding method or const definition and will treat the key as an alias.

The export method can be called multiple times. Its behavior is additive:

  1. # this:
  2. export :foo, :bar
  3. # is the same as this:
  4. export :foo
  5. export :bar

Importing declarations

Declarations from another module can be imported using #import:

  1. require 'modulation'
  2. Models = import('./models')
  3. ...
  4. user = Models::User.new(...)
  5. ...

Alternatively, a module interested in a single declaration from another module
can use the following technique:

  1. require 'modulation'
  2. User = import('./models')::User
  3. ...
  4. user = User.new(...)

A word about paths

Paths given to import are always considered relative to the importing file,
unless they are absolute (e.g. /home/dave/repo/my_app), specify a
tag or reference a
gem. This is true for all Modulation APIs
that accept path arguments.

Advanced Usage

Using tags to designate common subdirectories

Normally, module paths are always relative to the file calling the #import
method, just like #require_relative. This can become a problem once you start
moving your source files around. In addition, in applications where your source
files are arranged in multiple directories, it can quickly become tedious to do
stuff like Post = import('../models/post').

Modulation provides an alternative to relative paths in the form of tagged
sources. A tagged source is simply a path associated with a label. For example,
an application may tag lib/models simply as @models. Once tags are defined,
they can be used when importing files, e.g. import('@models/post').

To define tags, use Modulation.add_tags:

  1. Modulation.add_tags(
  2. models: '../lib/models',
  3. views: '../lib/views'
  4. )
  5. ...
  6. User = import '@models/user'

Importing all source files in a directory

To load all source files in a directory you can use #import_all:

  1. import_all('./ext')

Groups of modules providing a uniform interface can also be loaded using
#import_map:

  1. API = import_map('./math_api') #=> hash mapping filenames to modules
  2. API.keys #=> ['add', 'mul', 'sub', 'div']
  3. API['add'].(2, 2) #=> 4

The #import_map takes an optional block to transform hash keys:

  1. API = import_map('./math_api') { |name, mod| name.to_sym }
  2. API.keys #=> [:add, :mul, :sub, :div]
  3. API[:add].(2, 2) #=> 4

Importing methods into classes and objects

Modulation provides the #extend_from and #include_from methods to include
imported methods in classes and objects:

  1. module Sequences
  2. extend_from('./seq.rb')
  3. end
  4. Sequences.fib(5)
  5. # extend integers
  6. require 'modulation'
  7. class Integer
  8. include_from('./seq.rb')
  9. def seq(kind)
  10. send(kind, self)
  11. end
  12. end
  13. 5.seq(:fib)

The #include_from method accepts an optional list of symbols to import:

  1. class Integer
  2. include_from './seq.rb', :fib
  3. end
  4. 5.fib

Default exports

A module may wish to expose just a single class or constant, in which case it
can use #export_default:

user.rb

  1. export_default :User
  2. class User
  3. ...
  4. end

app.rb

  1. require 'modulation'
  2. User = import('./user')
  3. User.new(...)

The default exported value can also be defined directly thus:

config.rb

  1. export_default(
  2. host: 'localhost',
  3. port: 1234,
  4. ...
  5. )

app.rb

  1. require 'modulation'
  2. config = import('./config')
  3. db.connect(config[:host], config[:port])

Circular dependencies

Circular dependencies, while not the best practice for organizing a code base,
are sometimes useful. Modulation supports circular dependencies, with the
exception of modules with default exports.

Accessing a module’s root namespace from nested modules within itself

The special constant MODULE allows you to access the containing module from
nested modules or classes. This lets you call methods defined in the module’s
root namespace, or otherwise introspect the module:

  1. export :AsyncServer
  2. # Await a promise-like callable
  3. def await
  4. calling_fiber = Fiber.current
  5. p = ->(v = nil) {calling_fiber.resume v}
  6. yield p
  7. Fiber.yield
  8. end
  9. class AsyncServer < SomeTCPServer
  10. def async_read
  11. MODULE.await {|p| on_read {|data| p.(data)}}
  12. end
  13. end

Accessing the global namespace

If you need to access the global namespace inside a module just prefix the
class name with double colons:

  1. class ::GlobalClass
  2. ...
  3. end
  4. ::ENV = { ... }
  5. what_is = ::THE_MEANING_OF_LIFE

Programmatic module creation

In addition to loading modules from files, modules can be created dynamically at
runtime using Modulation.create. You can create modules by supplying a hash
prototype, a string or a block:

  1. # Using a hash prototype
  2. m = Modulation.create(
  3. add: -> x, y { x + y },
  4. mul: -> x, y { x * y }
  5. )
  6. m.add(2, 3)
  7. m.mul(2, 3)
  8. # Using a string
  9. m = Modulation.create <<~RUBY
  10. export :foo
  11. def foo
  12. :bar
  13. end
  14. RUBY
  15. m.foo
  16. # Using a block
  17. m = Modulation.create do { |mod|
  18. export :foo
  19. def foo
  20. :bar
  21. end
  22. class mod::BAZ
  23. ...
  24. end
  25. }
  26. m.foo

The creation of a objects using a hash prototype is also available as a separate
gem called eg.

Unit testing modules

Methods and constants that are not exported can be tested using the #__expose!
method. Thus you can keep implementation details hidden, while being able to
easily test them:

parser.rb

  1. export :parse
  2. def parse(inp)
  3. split(inp).map(&:to_sym)
  4. end
  5. # private method
  6. def split(inp)
  7. inp.split(',').map(&:strip)
  8. end

test_seq.rb

  1. require 'modulation'
  2. require 'minitest/autorun'
  3. Parser = import('../lib/parser').__expose!
  4. class FibTest < Minitest::Test
  5. def test_that_split_trims_split_parts
  6. assert_equal(%w[abc def ghi], Parser.split(' abc ,def , ghi '))
  7. end
  8. end

Mocking modules

Modules loaded by Modulation can be easily mocked when running tests or specs,
using Modulation.mock:

  1. require 'minitest/autorun'
  2. require 'modulation'
  3. module MockStorage
  4. extend self
  5. def get_user(user_id)
  6. {
  7. user_id: user_id,
  8. name: 'John Doe',
  9. email: 'johndoe@gmail.com'
  10. }
  11. end
  12. end
  13. class UserControllerTest < Minitest::Test
  14. def test_user_storage
  15. Modulation.mock('../lib/storage', MockStorage) do
  16. controller = UserController.new
  17. ...
  18. end
  19. end
  20. end

Modulation.mock accepts a module path and a receiver, and the module stays
mocked within the given block.

Lazy Loading

Modulation allows the use of lazy-loaded modules - loading of modules only once
they’re needed by the application, in similar fashion to Module#auto_load. To
lazy load modules use the #auto_import method, which takes a constant name and
a path:

  1. export :foo
  2. auto_import :BAR, './bar'
  3. def foo
  4. # the bar module will only be loaded once this method is called
  5. MODULE::BAR
  6. end

Lazy-loaded constants must always be qualified. When referring to a
lazy-loaded constant from the module’s top namespace, use the MODULE
namespace, as shown above.

The #auto_import method can also take a hash mapping constant names to paths.
This is especially useful when multiple concerns are grouped under a single
namespace:

  1. export_default :SuperNet
  2. module SuperNet
  3. auto_import(
  4. HTTP1: './http1',
  5. HTTP2: './http2',
  6. WebSockets: './websockets'
  7. )
  8. end
  9. SuperNet::HTTP1 #=> loads the http1 module

Reloading modules

Modules can be reloaded at run-time for easy hot code reloading:

  1. require 'modulation'
  2. SQL = import('./sql')
  3. ...
  4. SQL.__reload!

Another way to reload modules is using Modulation.reload, which accepts a
module or a filename:

  1. require 'filewatcher'
  2. FileWatcher.new(['lib']).watch do |fn, event|
  3. if(event == :changed)
  4. Modulation.reload(fn)
  5. end
  6. end

When a module is reloaded, its entire content - constants and methods - will
be replaced. That means that any code using that module could continue to use
it without even being aware it was reloaded, providing its API has not
changed.

Reloading of modules with default exports is also possible. Modulation will
extend the exported value with a #__reload! method. The value will need to be
reassigned:

  1. require 'modulation'
  2. settings = import('settings')
  3. ...
  4. settings = settings.__reload!

Please note that Modulation does not include a directory watcher that
automatically reloads changed modules. This is due to multiple considerations
that include the chosen threading model, or the reactor engine in use, or even
the chosen solution for watching files (whether it’s an external gem or an
internal tool).

It is, however, quite trivial to watch files using
directory_watcher:

  1. require 'directory_watcher'
  2. dw = DirectoryWatcher.new 'lib', glob: '**/*.rb', interval: 2, pre_load: true
  3. dw.add_observer do |*events|
  4. events.each do |e|
  5. next unless e.type == :modified
  6. Modulation.reload e.path
  7. end
  8. end
  9. dw.start

Retaining state between reloads

Before a module is reloaded, all of its methods and constants are removed. In
some cases, a module might need to retain state across reloads. You can do this
by simply using instance variables:

  1. export :value, :inc
  2. @counter ||= 0
  3. def value
  4. @counter
  5. end
  6. def incr
  7. @counter += 1
  8. end

Care must be taken not to reassign values outside of methods, as this will
overwrite any value retained in the instance variable. To assign initial values,
use the ||= operator as in the example above. See also the
reload example.

Dependency introspection

Modulation allows runtime introspection of dependencies between modules. You can
interrogate a module’s dependencies (i.e. the modules it imports) by calling
#__depedencies:

m1.rb

  1. import ('./m2')

app.rb

  1. m1 = import('./m1')
  2. m1.__depedencies #=> [<Module m2>]

You can also iterate over a module’s entire dependency tree by using
#__traverse_dependencies:

  1. m1 = import('./m1')
  2. m1.__traverse_dependencies { |mod| ... }

To introspect reverse dependencies (modules using a particular module), use
#__dependent_modules:

  1. m1 = import('./m1')
  2. m1.__depedencies #=> [<Module m2>]
  3. m1.__dependencies.first.__dependent_modules #=> [<Module m1>]

Running Modulation-based applications

Modulation provides a binary script for running Modulation-based applications.
mdl is a wrapper around Ruby that loads your application’s main file as a
module, and then runs your application’s entry point method. Let’s look at a
sample application:

app.rb

  1. def greet(name)
  2. puts "Hello, #{name}!"
  3. end
  4. def main
  5. print "Enter your name: "
  6. name = gets
  7. greet(name)
  8. end

To run this application, execute mdl app.rb, or mdl run app.rb. mdl will
automatically require the modulation gem and call the application’s entry
point, #main.

Packing applications with Modulation

Note: application packing is at the present time an experimental feature.
There might be security concerns for packaging your app, such as leaking
filenames from the developer’s machine.

Modulation can also be used to package your entire application into a single
portable file that can be copied to another machine and run as is. To package
your app, use mdl pack. This command will perform a dynamic analysis of all
the app’s dependencies and will put them together into a single Ruby file.

For more information have a look at the app example.

Writing gems using Modulation

Modulation can be used to write gems, providing fine-grained control over your
gem’s public APIs and letting you hide any implementation details. In order to
allow loading a gem using either #require or #import, code your gem’s main
file normally, but add require 'modulation/gem' at the top, and export your
gem’s main namespace as a default export, e.g.:

  1. require 'modulation/gem'
  2. export_default :MyGem
  3. module MyGem
  4. ...
  5. MyClass = import('my_gem/my_class')
  6. ...
  7. end

Importing gems using Modulation

Gems written using modulation can also be loaded using #import. If modulation
does not find the module specified by the given relative path, it will attempt
to load a gem by the same name. It is also possible to load specific files
inside modules by specifying a sub-path:

  1. require 'modulation'
  2. MyFeature = import 'my_gem/my_feature'

Note: Since there’s not much of a point in #importing gems that do not
use Modulation to export symbols, Modulation will refuse to import any gem
that does not depend on Modulation.

Writing modules that patch external classes or modules

It is generally recommended you refrain from causing side effects or patching
external code in your modules. When you do have to patch external classes or
modules (i.e. core, stdlib, or some third-party code) in your module, it’s
useful to remember that any module may be eventually reloaded by the application
code. This means that any patching done during the loading of your module must
be idempotent, i.e. have the same
effect when performed multiple times. Take for example the following module
code:

  1. module ::Kernel
  2. # aliasing #sleep more than once will break your code
  3. alias_method :orig_sleep, :sleep
  4. def sleep(duration)
  5. STDERR.puts "Going to sleep..."
  6. orig_sleep(duration)
  7. STDERR.puts "Woke up!"
  8. end
  9. end

Running the above code more than once would cause an infinite loop when calling
Kernel#sleep. In order to prevent this situation, modulation provides the
Module#alias_method_once method, which prevents aliasing the original method
more than once:

  1. module ::Kernel
  2. # alias_method_once is idempotent
  3. alias_method_once :orig_sleep, :sleep
  4. def sleep(duration)
  5. STDERR.puts "Going to sleep..."
  6. orig_sleep(duration)
  7. STDERR.puts "Woke up!"
  8. end
  9. end

Coding style recommendations

  • Import modules into constants, not variables:

    1. Settings = import('./settings')
  • Place your exports at the top of your module, followed by #requires,
    followed by #imports:

    1. export :foo, :bar, :baz
    2. require 'json'
    3. Core = import('./core')
    4. ...

API Reference

Kernel

Kernel#auto_import_map(path, options = {})

Returns a hash mapping keys to corresponding module files inside the given
directory path. Modules are loaded automatically upon accessing hash keys.

Kernel#import(path)

Returns a loaded module identified by the given path. The path can contain
tags

Kernel#import_all(path)

Kernel#import_map(path, options = {})

Module

Module#__module_info

Returns a hash containing information about the module. This currently includes
the following entries:

location|Absolute module file path
exported_symbols|Array containing all symbols exported by the module

Module#__reload!

Module#alias_method_once(new_name, old_name)

Module#auto_import(sym, path)

Module#export(*symbols)

Module#export_default(value)

Module#export_from_receiver(receiver)

Module#extend_from(path)

Module#include_from(path, *symbols)

Module::MODULE

Modulation

Modulation.full_backtrace!

Modulation.reload

Why you should not use Modulation

  • Modulation is not production-ready.
  • Modulation is not thread-safe.
  • Modulation doesn’t play well with rdoc/yard.
  • Modulation (probably) doesn’t play well with Marshal.
  • Modulation (probably) doesn’t play well with code-analysis tools.