-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce dispatchers for adding syntax sugar to helpers
- Loading branch information
Showing
4 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Dry::Initializer | ||
# | ||
# @private | ||
# | ||
# Dispatchers allow adding syntax sugar to `.param` and `.option` methods. | ||
# | ||
# Every dispatcher should convert the source hash of options into | ||
# the resulting hash so that you can send additional keys to the helpers. | ||
# | ||
# @example Add special dispatcher | ||
# | ||
# # Define a dispatcher for key :integer | ||
# dispatcher = proc do |opts| | ||
# opts.merge(type: proc(&:to_i)) if opts[:integer] | ||
# end | ||
# | ||
# # Register a dispatcher | ||
# Dry::Initializer::Dispatchers << dispatcher | ||
# | ||
# # Now you can use option `integer: true` instead of `type: proc(&:to_i)` | ||
# class Foo | ||
# extend Dry::Initializer | ||
# param :id, integer: true | ||
# end | ||
# | ||
module Dispatchers | ||
class << self | ||
def <<(item) | ||
list << item | ||
self | ||
end | ||
|
||
def [](options) | ||
list.inject(options) { |opts, item| item.call(opts) } | ||
end | ||
|
||
private | ||
|
||
def list | ||
@list ||= [] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
describe "custom dispatchers" do | ||
subject { Test::Foo.new "123" } | ||
|
||
before do | ||
dispatcher = ->(op) { op[:integer] ? op.merge(type: proc(&:to_i)) : op } | ||
Dry::Initializer::Dispatchers << dispatcher | ||
end | ||
|
||
context "with extend syntax" do | ||
before do | ||
class Test::Foo | ||
extend Dry::Initializer | ||
param :id, integer: true | ||
end | ||
end | ||
|
||
it "adds syntax sugar" do | ||
expect(subject.id).to eq 123 | ||
end | ||
end | ||
|
||
context "with include syntax" do | ||
before do | ||
class Test::Foo | ||
include Dry::Initializer.define -> do | ||
param :id, integer: true | ||
end | ||
end | ||
end | ||
|
||
it "adds syntax sugar" do | ||
expect(subject.id).to eq 123 | ||
end | ||
end | ||
end |