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

Extend Dash to accept only predefined values #61

Open
Spaceghost opened this issue Oct 26, 2012 · 7 comments
Open

Extend Dash to accept only predefined values #61

Spaceghost opened this issue Oct 26, 2012 · 7 comments

Comments

@Spaceghost
Copy link

Hey,

I was wanting the ability to pass a method, collection, or lambda/proc to a hashie property. What it should do is limit the options that the property can take.

property :gender, options: [:male, :female, :meat_popsicle]
property :age, options: age_range
property :hair_color, options: ->{ hex_color? }

def age_range
18...25
#more logic, I guess
end

Or something similar.

@jch
Copy link
Contributor

jch commented Feb 13, 2013

That sounds like a good custom subclass. Would you be interested in trying to implement it and submit a pull request?

@Spaceghost
Copy link
Author

I'll take a peek into it.

@dblock
Copy link
Member

dblock commented Mar 31, 2014

Bump.

@dblock
Copy link
Member

dblock commented Jun 11, 2020

I think this can be a new feature for Dash

property :gender, values: [:male, :female, :meat_popsicle]

@dblock dblock changed the title Hashie attribute accepting only predefined options Extend Dash to accept only predefined values Jun 11, 2020
@caalberts
Copy link
Contributor

Should this be implemented as a validation instead? It would serve the 2 examples above, but also open to other validations in the future.

property :gender, validate: { within: [:male, :female, :meat_popsicle] }
property :age, validate: { within: age_range }
property :hair_color, validate: { with: ->(colour) { hex_color? }  }

@dblock
Copy link
Member

dblock commented Sep 14, 2020

Feel like an overkill because validate serves no other purpose than wrap with vs. within vs. something else and creates unnecessary mental overhead. For the first two we can make it equally predictable with a single keyword, values:.

property :gender, values: [:male, :female, :meat_popsicle]
property :age, values: 1..150 # a Range

I think a PR for the above would be super valuable and solve 99% of the scenarios.

The other half of this problem is that you don't always want values, but types. So I'd want

property :possible, type: Boolean
property :offset, type: Integer

The third, color, gets is a bit tricky, I think I'd want something more OO, which lets me define higher level classes, and would borrow from Grape, which is battle tested on this type of checks, extensibility, etc. It could look like this:

class Color
  attr_reader :value

  def initialize(color)
    @value = color
  end

  def self.parse(value)
    fail 'Invalid color' unless hex_color?(value)
    new(value)
  end

  def hex_color?(value)
   # ...
  end
end

property :hair_color, type: Color

Thinking out loud we would really want the entire coercion, types, checks system that is implemented in Grape using dry-types. Maybe as a separate gem even.

Thoughts?

@caalberts
Copy link
Contributor

Starting with values sounds like a good idea. I'm not sure how large is the need for a type validation compared to predefined values.

I think a PR for the above would be super valuable and solve 99% of the scenarios.

💯

caalberts added a commit to caalberts/hashie that referenced this issue Sep 19, 2020
Extends a Dash with the ability to
accept only predefined values on a property.

hashie#61
caalberts added a commit to caalberts/hashie that referenced this issue Sep 19, 2020
Extends a Dash with the ability to
accept only predefined values on a property.

hashie#61
caalberts added a commit to caalberts/hashie that referenced this issue Sep 19, 2020
Extends a Dash with the ability to
accept only predefined values on a property.

hashie#61
caalberts added a commit to caalberts/hashie that referenced this issue Sep 19, 2020
Extends a Dash with the ability to
accept only predefined values on a property.

hashie#61
caalberts added a commit to caalberts/hashie that referenced this issue Sep 20, 2020
Extends a Dash with the ability to
accept only predefined values on a property.

hashie#61
caalberts added a commit to caalberts/hashie that referenced this issue Sep 20, 2020
Extends a Dash with the ability to
accept only predefined values on a property.

hashie#61
caalberts added a commit to caalberts/hashie that referenced this issue Sep 20, 2020
Extends a Dash with the ability to
accept only predefined values on a property.

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

No branches or pull requests

4 participants