This repository has been archived by the owner. It is now read-only.
A dependency injector for Rails / ActiveSupport
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rvmrc
Gemfile
Gemfile.lock
README.md
Rakefile
dex.gemspec

README.md

Dex

What is dex?

Have you ever seen this?:

# some_class.rb:
class SomeClass
  # ...
end

# some_class_decorator.rb:
module SomeClassDecorator
  # ...
end

# config/initializers/decorators.rb:
config.to_prepare do
  SomeClass.send :include, SomeClassDecorator
  ...
  ...
  ...
end

What's so bad about this?

It destroys Rails' lazy loading. Every Class with a decorator get's preloaded, on every request (in development mode). Not lazy-loaded, preloaded in this initializer, even if it's never being used. That's incredibly slow. And sucks.

So what was dex now?

It's just a lightweight extension to ActiveSupport::Dependencies to support lazy-loaded decorators. You still have to register decorators in an initializer, but it won't cause a preload, but register a callback to load and include the decorator as soon as it's base-class gets lazy-loaded:

# some_class.rb:
class SomeClass
  # ...
end

# some_class_decorator.rb:
module SomeClassDecorator
  # ...
end

# config/initializers/dex.rb
ActiveSupport::Dependencies.register_dex 'SomeClass', 'SomeClassDecorator'
...