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

Is there a way to determine whether a hash's key was given? #408

Closed
nick-merrill opened this issue Mar 2, 2017 · 6 comments · Fixed by #410
Closed

Is there a way to determine whether a hash's key was given? #408

nick-merrill opened this issue Mar 2, 2017 · 6 comments · Fixed by #410
Labels

Comments

@nick-merrill
Copy link

I'm allowing nil as an input for any of these, but I also want to know whether someone specified nil or whether it's just the default because the key was unspecified. That's fine when we're not dealing with a hash, but when we are, I seem to be running into issues.

  hash :attributes, default: {} do
    string :description, default: nil
    string :location, default: nil
  end

I know that I can determine whether attributes was passed by asking given? :attributes, but how can I determine whether attributes[:description] was given?

Thank you for a wonderful gem! The interaction pattern really makes Rails shine!

@nick-merrill
Copy link
Author

Inspecting the code, it seems that the relevant line is here.

    @_interaction_keys = inputs.keys.to_set & self.class.filters.keys

This seems to imply that the feature I'm hoping for does not exist. Maybe I'm missing a way around this though. Any thoughts?

Thanks!

@tfausak
Copy link
Collaborator

tfausak commented Mar 2, 2017

You're right, we don't currently support this.

@AaronLasseigne
Copy link
Owner

I feel like this makes sense to add. We could allow given? to take multiple arguments so that it works like dig. If we do that would we want to support checking Array slots or only hash keys?

@tfausak
Copy link
Collaborator

tfausak commented Mar 2, 2017

I had not heard of dig before. It's new in Ruby 2.3. We could add support by requiring or copying ruby_dig. Seems like we should support array indices as well as hash keys.

@nick-merrill
Copy link
Author

That seems like a really intuitive solution to me. I'm not sure how array indices could not be 'given' if they existed, but I only just starting using Ruby.

@AaronLasseigne
Copy link
Owner

It seems like we all think this is a good direction. I should be able to build this out later this week when I've got some time.

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

Successfully merging a pull request may close this issue.

3 participants