Selector

Composable multi-type conditions.

Synopsis

White Lists

selector = Selector.new only: [:foo, :qux]
selector[:foo] # => true
selector[:bar] # => false

selector = Selector.new only: /foo/
selector[:foobar] # => true
selector[:bar]    # => false

selector = Selector.new only: 1..3
selector[2.4] # => true
selector[0]   # => false

selector = Selector.new only: -> value { value.is_a? Hash }
selector[foo: :FOO] # => true
selector[:foo]      # => false

Black Lists

selector = Selector.new except: [:bar, :qux]

selector[:foo] # => true
selector[:bar] # => false

Negation

blacklist = Selector.new except: /bar/
selector = !blacklist

selector[:bar] # => true
selector[:foo] # => false

Algebra

blacklist = Selector.new except: /bar/
whitelist = Selector.new only: /foo/

selector = whitelist & blacklist

selector[:foobaz] # => true
selector[:foobar] # => false
blacklist = Selector.new except: /bar/
whitelist = Selector.new only: /foo/

selector = whitelist - blacklist # = whitelist + !blacklist

selector[:foobar] # => true
selector[:foo] # => false
selector[:bar] # => false
blacklist = Selector.new except: 1..5
whitelist = Selector.new only: 4..8

selector = whitelist | blacklist # = !(!whitelist + !blacklist)
selector[0.5] # => true
selector[5.5] # => true
selector[2.5] # => false

Immutability:

Selector.new.frozen? # => true

Performance

Use the gem when you really need:

The selector is slow. And the simpler the condition, the more the cost in terms of performance, that can vary from x1.45 for functions to x3.5 for simple comparison.

For details see the benchmark and its results.

Installation

Add this line to your application’s Gemfile:

# Gemfile
gem "selector"

Then execute:

bundle

Or add it manually:

gem install selector

Compatibility

Tested under rubies compatible to MRI 1.9.3+.

Uses RSpec 3.0+ for testing and hexx-suit for dev/test tools collection.

Contributing

License

See the MIT LICENSE.