Scaffolds the current project's dependency from a variable, defined outside.
The module is used inside the hexx collection of scaffolders.
Suppose we need the user_class
dependency that should be initialized by dummy application as User
taken from the external users
module.
The dependency should be called as a core module getter:
MyModule.user_class # => User from 'users' gem (in the test environment)
To do it start the scaffolder:
Hexx::Dependencies::CLI.start %w(user_class -i User -g users)
At first the scaffolder provides base loader mechanism (if it is absent):
- dummy app
spec/dummy
. - dummy loader
spec/spec_helper.rb
. - the module configurator
lib/my_module/configurator.rb
. - the module initializer
spec/dummy/config/initializers/my_module.rb
. - the configurator loaders from
lib/my_module.rb
.
See the Loading Order part below for details how it works.
Then the scaffolder adds required settings:
user_class
attribute inlib/my_module/configurator.rb
.users
development dependency in the Gemfile.users
loader in thespec/dummy/initializers.rb
.user_class = User
injection in thespec/dummy/initializers.rb
.
The module is loaded in the following order:
- The dummy application takes the control:
# spec/spec_helper.rb
require_relative "dummy/lib/dummy"
The dummy app:
- sets the pointer to initializers folder
ENV['PATH_TO_INITIALIZERS']
. - gives control to the module.
# spec/dummy/lib/dummy.rb
ENV['PATH_TO_INITIALIZERS'] = File.expand_path(
"../../config/initializers", __FILE__
)
require "my_module"
lib/my_module.rb
:
- declares its
Configurator
. - takes its own initializers from dummy
config/initializers
folder. - loads the rest of the code.
# lib/my_module.rb
require_relative "my_module/configurator"
load File.join(ENV['PATH_TO_INITIALIZERS'], "my_module.rb")
# ...loads the rest of application
# lib/my_module/configurator.rb
module MyModule
class << self
def configure
instance_eval(yield) if block_given?
end
attr_accessor :user_class
end
end
# spec/dummy/config/initializers/my_module.rb
require "users"
MyModule.configure do |config|
user_class = User
end
Add this line to your application's Gemfile:
# Gemfile
gem "hexx-dependencies"
Then execute:
bundle
Or add it manually:
gem install hexx-dependencies
Tested under rubies compatible to MRI 2.0 API:
- MRI 2.0+
- Rubinius 2+ (2.0+ modes)
- JRuby-head (9000) (2.0+ modes)
Uses [RSpec] 3.0+ for testing and hexx-suit for dev/test tools collection.
- Fork the project.
- Read the STYLEGUIDE.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with Rakefile or version (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
See the MIT LICENSE.