Use a separate DSL class for evaluation context #35
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.
This avoids exposing the implementation internals (e.g. instance variables, additional non-dsl methods) from the Stylesheet class, that might conflict with code written by the user.
I took this approach from Ruby Ate My DSL (17 minutes).
The problem was that all the methods and instance variables in the
Stylesheetclass were exposed to the DSL context. That means that if a user accidentally uses an instance variable with the same name (e.g.@layers,@paramsetc) or uses or defines any methods that clash with anything internal in the Stylesheet class, then everything blows up. This is particularly a problem if we want to add any new instance variables, or new internal method definitions, in the future, that might conflict with what people have written in their stylesheets.Instead, we can create a slimmed down StylesheetDSL class, that only contains the methods offered by the DSL. We then evaluate the user's code in the context of that class (
@dsl.instance_eval(...)). This means that things are less likely to break. For example, the user can use any instance variables they like (except@__impl!) without breaking anything, and we can add more instance variables to the Stylesheet class without hesitation.I intend to do something similar for the Layer class too.