dry-rb / dry-logic

Predicate logic with rule composition
https://dry-rb.org/gems/dry-logic/
MIT License
179 stars 66 forks source link

Inclusion & exclusion predicates #8

Closed fran-worley closed 8 years ago

fran-worley commented 8 years ago

The current inclusion and exclusion predicates take a list and check that the input is included in that list.

I have just run into a use case where I want to turn this on its head and instead check that the input includes a value.

#current predicate:
predicate(:inclusion?) do |list, input|
  list.include?(input)
end

#what I need...
predicate(:some_new_predicate?) do |value, input|
  input.include?(value)
end

Does this seem like something worth adding to the library?

In terms of names I guess we either go for :includes? and :excludes? the idea being that we are checking if an input includes or excludes something. However they might be a little similar to the current predicates...

solnic commented 8 years ago

Yes it is, you're not the first person who asked for it. IIRC we thought about naming it contains? but I'd be ok with includes? and excludes? although there's a risk people will be confused with inclusion? vs includes? - but I gotta say I never ever liked inclusion?, we picked it up because people are familiar with it. Blah

fran-worley commented 8 years ago

I thought of contains but what do you do for the opposite? I wasn't happy with anything I came up with (does_not_contain, missing, absent, lacks).

You could do:

input.includes?(value) 
input.included_in?(list) #instead of inclusion

input.excludes?(value)
input.excluded_from?(list) #instead of exclusion

We could always create aliases for inclusion and exclusion for backwards compatibility.

fran-worley commented 8 years ago

Or (might be a bit much magic) you could determine what to call .include? on based on the type of argument supplied similar to how the Size predicate works.

Actually, that isn't a good idea please ignore!!

timriley commented 8 years ago

I like your suggestion, @fran-worley:

input.includes?(value) 
input.included_in?(list) #instead of inclusion

input.excludes?(value)
input.excluded_from?(list) #instead of exclusion

It looks a lot clearer to me, and it seems @solnic isn't attached to the current naming. Now might also be a good time to get it in, given we're changing the basic DSL anyway.

Would you be up for making this change (and deprecating the other names) along with https://github.com/dry-rb/dry-logic/pull/9?

fran-worley commented 8 years ago

Yup will do. I'll also PR dry-v and the docs to update .

fran-worley commented 8 years ago

closed via #9