Add HumanizeVisitor and Calculator#humanize#338
Open
MaximilianoGarciaRoe wants to merge 1 commit into
Open
Conversation
Adds a visitor that produces a natural-language English representation of
a Dentaku expression. Operators are verbalized (>= -> 'is greater than or
equal to'), and identifiers can optionally be substituted with concrete
values via a second argument to Calculator#humanize.
calc = Dentaku::Calculator.new
calc.humanize('days >= min and days <= max', min: 5, max: 20)
# => 'days is greater than or equal to 5 and days is less than or equal to 20'
HumanizeVisitor subclasses PrintVisitor, so any expression PrintVisitor
handles is also handled here; only operator output is overridden.
Contributor
|
Just realized that 2.5 will not be supported later. so i think its just matter up re-syncing it with main |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Why
Apps that expose Dentaku formulas to end users (HR tools, business-rule
engines, approval workflows) have no way to explain what a formula means
in plain language. This fills that gap.
What
HumanizeVisitor < PrintVisitor— overrides only the output, inheritseverything else. Falls back to
PrintVisitorfor any unmapped node, socustom functions still render.
Covers all built-in constructs: arithmetic, comparison, logical and
bitwise operators,
IF/SWITCH/CASE, all numeric, string, andcollection functions, and the full Ruby
Mathmodule.Lazy-loaded — no overhead unless
#humanizeis called.Notes
"times"vs"multiplied by", etc.) is easy to adjust.to externalize later if wanted.
Calculator#humanizeif you prefer the visitor standalone.Happy to contribute and looking forward to any feedback or suggestions!