-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use trailblazer-option over declarative-option and upgrade declarative-builder #492
Merged
yogeshjain999
merged 2 commits into
trailblazer:master
from
yogeshjain999:use-trb-option
Jun 11, 2021
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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 |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apotonick This is identical to Representable::Option.
Should we move
Cell::Option
andCell::Options
in trb-option instead ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, do we have use on a generic level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, kind of.
Cell::Options
isn't generic. We can keep it here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question is, do we have the static requirement anywhere else (e.g. Reform)? If yes, we can extract it. On the other hand, we can always do that later. 🥱
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, we've the static requirement in reform, disposable and representer. Currently, it is done in representer itself and it's being used in other 2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On the other hand, supporting
Callable
objects between these 2 types of gems (representable, reform, cells etc and trb-activity, DSL etc) is little different i.e.Uber::Callable
for identifying callable object.So other way is we should not mix these 2 types in trb-option but keep separate. If it's ok, then we can keep
Cell::Option
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uber::Callable
, that one we should dump ASAP. Isn't it better if we always treat an objectcall
able, anyway?Option
s, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm yeah. The problem with treating every object callable is we can't distinguish between them and static options (some static options also includes modules, classes etc), apart from
call
method.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh, ok right, so that was why I initially introduced
Uber::Callable
, damn! Hm... how could we do that in the future?Ok, in that case, we won't move that behavior to TRB, for now. 🍰
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct (i.e. distinction between static and callable objects isn't needed in TRB). So we can merge this PR.
Uber::Callable
is a nice alternative to mark callables forcells
andrepresentable friends
for now.In future versions, maybe we can have restrictions on static options (or ways to avoid them) ?