-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #512 from trailblazer/trailblazer-option
Declarative option is replaced by trailblazer option
- Loading branch information
Showing
4 changed files
with
43 additions
and
9 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
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 @@ | ||
require "trailblazer/option" | ||
require "uber/callable" | ||
|
||
module Cell | ||
# Extend `Trailblazer::Option` to make static values as callables too. | ||
class Option < ::Trailblazer::Option | ||
def self.build(value) | ||
callable = case value | ||
when Proc, Symbol, Uber::Callable | ||
value | ||
else | ||
->(*) { value } # Make non-callable value to callable. | ||
end | ||
|
||
super(callable) | ||
end | ||
end | ||
|
||
class Options < Hash | ||
# Evaluates every element and returns a hash. Accepts arbitrary arguments. | ||
def call(*args, **options, &block) | ||
Hash[ collect { |k,v| [k,v.(*args, **options, &block) ] } ] | ||
end | ||
end | ||
|
||
def self.Option(value) | ||
::Cell::Option.build(value) | ||
end | ||
|
||
def self.Options(options) | ||
Options.new.tap do |hsh| | ||
options.each { |k,v| hsh[k] = Option(v) } | ||
end | ||
end | ||
end |