Support procs for required_if; add present_if#115
Open
smcgivern wants to merge 2 commits into
Open
Conversation
added 2 commits
March 20, 2015 11:33
These were supported in AttributeResolver, but not in Attribute itself. The proc will receive one argument, which is the loaded object. Signed-off-by: Sean McGivern <sean.mcgivern@rightscale.com>
When dumping a model, each attribute will be replaced with nil if it has a present_if condition and that condition is not met. The allowed conditions are the same as for required_if: * A string key, which passes if that key's value is not nil. * A one-item hash, whose key is the path in the object, and whose value is any ===-compatible object (string, regex, proc, etc.). * A proc, which is passed the object. Like required_if, this cannot be used in conjunction with the required option. It can be used with the required_if option, but it should only be so in a way that they are compatible (for instance, if both have the same condition, then the attribute will only be present when it is required). Signed-off-by: Sean McGivern <sean.mcgivern@rightscale.com>
Contributor
Author
|
@blanquer are you interested in either of these? |
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.
This is an experimental PR with two separate changes:
required_ifcan now be a proc itself. Previously it could be a proc, but only with a key as part of a hash condition:required_if: {key => proc}. Now it can just berequired_if: proc, where the proc gets access to more than one attribute at a time.present_ifwill make an attribute of a modelnilwhen dumping if it's set and the condition evaluates to false. We need this, or something like it, because we want to express a two-way constraint for some of ourrequired_ifattributes: this is always required if it's present, and never present if it's not required.