Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/michaelherold/module_builder
Dynamically build customized Ruby modules
https://github.com/michaelherold/module_builder
Last synced: 7 days ago
JSON representation
Dynamically build customized Ruby modules
- Host: GitHub
- URL: https://github.com/michaelherold/module_builder
- Owner: michaelherold
- License: mit
- Created: 2015-11-17T19:43:35.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-12-26T17:17:36.000Z (almost 6 years ago)
- Last Synced: 2024-04-24T19:11:45.595Z (7 months ago)
- Language: Ruby
- Homepage:
- Size: 45.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ModuleBuilder
[![Build Status](https://travis-ci.org/michaelherold/module_builder.svg)][travis]
[![Code Climate](https://codeclimate.com/github/michaelherold/module_builder/badges/gpa.svg)][codeclimate]
[![Inline docs](http://inch-ci.org/github/michaelherold/module_builder.svg?branch=master)][inch][codeclimate]: https://codeclimate.com/github/michaelherold/module_builder
[inch]: http://inch-ci.org/github/michaelherold/module_builder
[travis]: https://travis-ci.org/michaelherold/module_builderModuleBuilder gives you the ability to create modules that are customizable for
different situations. Are you creating a module that has an adapter, but don't
want to expose a setter on the including class because it's not a public API?
ModuleBuilder can help with that! Do you have two implementations of some
behavior that have different tradeoffs? Do you want to offer both through one
easy-to-use syntax? ModuleBuilder can do that too!Come see what ModuleBuilder will help you with today!
## Installation
Add this line to your application's Gemfile:
```ruby
gem "module_builder"
```And then execute:
$ bundle
Or install it yourself as:
$ gem install module_builder
## Usage
ModuleBuilder revolves around the concept of Builders that are responsible for
building modules based on your specification. For convenience, there is a
`Buildable` module that you can mix in to give your module building
superpowers.You configure Builders through two channels: class-level configuration and
instance-level state. There are three types of class-level configuration:
stateless inclusions, stateless extensions, and defined hooks.Stateless methods do not give much more power than just using a standard set of
`include`s and `extend`s. However, they help you organize your code in an
easily extensible fashion.You can use instance-level state within defined hooks to customize the behavior
of the built module. If you need to conditionally define a method based on the
configuration, you want to look here.### Stateless Inclusions
The builder simply `include`s stateless inclusions into the built module. There
are no customization hooks here, just a single place to specify all the modules
you want to `include` in your built module.```ruby
class StatelessInclusionBuilder < ModuleBuilder::Builder
def inclusions
[Comparable, Enumerable]
end
endStatelessInclusionBuilder.new.module.ancestors
#=> [#, Enumerable, Comparable]
```### Stateless Extensions
The builder adds all stateless extensions into the `Module#extended` hook of
the built module so they are extended onto anything that extends the built
module.```ruby
module Quack
def quack
"quack"
end
endclass QuackingBuilder < ModuleBuilder::Builder
def extensions
[Quack]
end
endclass Duck
extend QuackingBuilder.new.module
endDuck.quack #=> "quack"
```### Defined Hooks
Defined hooks are where you can do the heavy customization when building a
module. They are arbitrary methods that the builder invokes during its
initialization. Add any behavior that you want to make customizable via the
state that you give to the builder as a defined hook.```ruby
module Walk
def walk
"step, step, step"
end
endclass WalkingBuilder < ModuleBuilder::Builder
def hooks
[:rename_walk]
enddef inclusions
[Walk]
endprivate
def rename_walk
return unless @walk_method@module.__send__(:alias_method, @walk_method, :walk)
@module.__send__(:undef_method, :walk)
end
endclass Duck
include WalkingBuilder.new(:walk_method => :waddle).module
endDuck.new.waddle #=> "step, step, step"
Duck.new.walk #=> NoMethodError
```### Buildable Module
Explicitly instantiating a Builder and accessing the module that it built is
clunky. To gain easy access to a consistent syntax for your module, you can
include the `Buildable` module and specify the builder you want to use when
building your module.```ruby
class MyBuilder < ModuleBuilder::Builder
endmodule BuildableExample
include ModuleBuilder::Buildablebuilder MyBuilder
endmodule IncludingModule
include BuildableExample.new(:state => "value")
end
````Buildable` defaults to using a `Builder` defined within the current module.
You can rely on that instead of using the DSL for specifying the builder.```ruby
module OtherBuildableExample
include ModuleBuilder::Buildableclass Builder < ModuleBuilder::Builder
end
endmodule OtherIncludingModule
include OtherBuildableExample.new(:my_config_value => "awesome")
end
```When a `Buildable` module is included without the use of its constructor, the
default version of the module is included in the descendant class or module.```ruby
module BuildableExample
include ModuleBuilder::Buildableclass Builder < ModuleBuilder::Builder
end
endmodule IncludingModule
include BuildableExample
end
```## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run
`rake spec` to run the tests. You can also run `bin/console` for an interactive
prompt that will allow you to experiment.When writing code, you can use the helper application [Guard][guard] to
automatically run tests and coverage tools whenever you modify and save a file.
This helps to eliminate the tedium of running tests manually and reduces the
change that you will accidentally forget to run the tests. To use Guard, run
`bundle exec guard`.Before committing code, run `rake` to check that the code conforms to the style
guidelines of the project, that all of the tests are green (if you're writing a
feature; if you're only submitting a failing test, then it does not have to
pass!), and that the changes are sufficiently documented.To install this gem onto your local machine, run `bundle exec rake install`. To
release a new version, update the version number in `version.rb`, and then run
`bundle exec rake release`, which will create a git tag for the version, push
git commits and tags, and push the `.gem` file to [rubygems.org][rubygems].[guard]: http://guardgem.org
[rubygems]: https://rubygems.org## Contributing
Bug reports and pull requests are welcome on GitHub at
https://github.com/michaelherold/module_builder. This project is intended to be
a safe, welcoming space for collaboration, and contributors are expected to
adhere to the [Contributor Covenant][covenant] code of conduct.[covenant]: http://contributor-covenant.org
## Supported Ruby Versions
This library aims to support and is [tested against][travis] the following Ruby
versions:* Ruby 1.9.3
* Ruby 2.0
* Ruby 2.1
* Ruby 2.2
* JRuby 1.7 (in Ruby 1.9 mode)
* JRuby 9.0If something doesn't work on one of these versions, it's a bug.
This library may inadvertently work (or seem to work) on other Ruby versions,
however support will only be provided for the versions listed above.If you would like this library to support another Ruby version or
implementation, you may volunteer to be a maintainer. Being a maintainer
entails making sure all tests run and pass on that implementation. When
something breaks on your implementation, you will be responsible for providing
patches in a timely fashion. If critical issues for a particular implementation
exist at the time of a major release, support for that Ruby version may be
dropped.## Versioning
This library aims to adhere to [Semantic Versioning 2.0.0][semver]. Violations
of this scheme should be reported as bugs. Specifically, if a minor or patch
version is released that breaks backward compatibility, that version should be
immediately yanked and/or a new version should be immediately released that
restores compatibility. Breaking changes to the public API will only be
introduced with new major versions. As a result of this policy, you can (and
should) specify a dependency on this gem using the [Pessimistic Version
Constraint][pessimistic] with two digits of precision. For example:spec.add_dependency "module_builder", "~> 0.1"
[pessimistic]: http://guides.rubygems.org/patterns/#pessimistic-version-constraint
[semver]: http://semver.org/spec/v2.0.0.html## Credits
The original implementation of this library was based on the [Builder][builder]
within the [virtus] gem by Piotr Solnica, which I used for inspiration. Pieces
of it live on, but the current product expands on the original.The idea for the library came from [a conversation][conversation] about the
best way to configure a module once it is included in a class. [Grégory
Horion][gregory]'s comment lead me down the path of using `Module.new` as the
base for this library.[builder]: https://github.com/solnic/virtus/blob/3248a465643b86d7fcb0c16fe6937293adbd1055/lib/virtus/builder.rb
[conversation]: https://github.com/intridea/hashie/pull/262
[gregory]: http://gregory.io
[piotr]: http://solnic.eu
[virtus]: https://github.com/solnic/virtus## License
The gem is available as open source under the terms of the [MIT License][license].
[license]: http://opensource.org/licenses/MIT.