This gem provides classes to create arbitrary complex conditions, by building an expression tree. Currently only basic operations are implemented.
Add this line to your application's Gemfile:
gem 'criteria_operator'
And then execute:
$ bundle
Or install it yourself as:
$ gem install criteria_operator
For complete usage information, read the docs!
All relevant classes inherit from BaseOperator
. Generally, you'll want to have a root node of this type, which will in turn contain the whole expression tree. Currently, there are the following operators:
The BinaryOperator
is the most commonly used. It represents a binary operation, meaning an operator with a left and right hand side operand, each. The operands can be any BaseOperator
. The operator has to be one of the following types:
- Equal (default)
- Not Equal
- Greater
- Greater or Equal
- Less
- Less or Equal
These types are represented by constants in the module BinaryOperatorType
.
Usage example:
# checks if the operands op1 and op2 are different
operator = CriteriaOperator::BinaryOperator.new op1, op2, CriteriaOperator::BinaryOperatorType::NOT_EQUAL
The GroupOperator
is used to connect an amount of conditions (BaseOperator
s) with either AND
(default) or OR
(see GroupOperatorType
).
Usage example:
# checks if any of the conditions represented by operands op1 through op3 is true
operator = CriteriaOperator::GroupOperator.new [op1, op2, op3], CriteriaOperator::GroupOperatorType::OR
The UnaryOperator
is used to perform simple checks or transform a value represented by an operand (BaseOperator
). It takes on operand as well and has one of the following types:
- NOT (default)
- IS NULL
- Plus
- Minus
These types can be found in the UnaryOperatorType
module.
Usage example:
# invert the sign of the result returned by operand op1
operator = CriteriaOperator::UnaryOperator.new op1, CriteriaOperator::UnaryOperatorType::MINUS
The OperandProperty
is, as the name implies, an operand. This means, it does not work with any other operands like operators do. If presented as a tree, operands always are leaves. The OperandProperty
describes a property through it's name.
Usage example:
# represents a column named 'integer_value' (without quotes)
operand = CriteriaOperator::OperandProperty.new 'integer_value'
The OperandValue
is, just like the OperandProperty
, an operand. It represents any kind of value.
Usage example:
# represents the numeric value forty-two
operand = CriteriaOperator::OperandValue.new 42
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/TheFlow0360/criteria_operator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
Prerequisite to the acceptance of any pull requests is a successful build (this will be checked automatically) as well as test coverage and complete documentation.
The gem is available as open source under the terms of the MIT License.