Skip to content
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

Support procs for required_if; add present_if #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

smcgivern
Copy link
Contributor

This is an experimental PR with two separate changes:

  1. required_if can 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 be required_if: proc, where the proc gets access to more than one attribute at a time.
  2. present_if will make an attribute of a model nil when 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 our required_if attributes: this is always required if it's present, and never present if it's not required.

Sean McGivern 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 <[email protected]>
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 <[email protected]>
@smcgivern
Copy link
Contributor Author

@blanquer are you interested in either of these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant