Simple Business Rules Processor.
Add this line to your application's Gemfile:
gem 'rules_processor'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rules_processor
Add to /config/initializers/rules_processor.rb
class_name which will be hold actions. This actions will be executed after rule conditions are met.
require 'rules_processor'
RulesProcessor.configure do |config|
config.actions_class = ::Rule::ActionsProcessor # Example class
end
Array of rules designed to process. Rules order has importance.
Rule has conditions
and actions
.
Conditions are grouped in to logical groups: meet_all
and meet_any
.
Logical group | Description |
---|---|
meet_all |
All conditions in group should match. |
meet_any |
Any condition in group should match. |
[
{
meet_all: [],
meet_any: [
{field: 'ticket_subject', operator: 'include', value: 'return'},
{field: 'comment_content', operator: 'include', value: 'return'}
],
actions: [
{action_name: 'ticket_set_priority', value: '1'},
{action_name: 'ticket_assign_group', value: '3'}
]
}
]
Conditions has three attributes: :field
, :operator
, :value
Attribute | Description |
---|---|
field |
Proper construction of field is prefix_ + method_name , when prefix is record's class_name. Example: ticket_subject , ticket_assignee_id , comment_content |
operator |
[List of supported operators] (#supported-operations) |
value |
- |
Example condition:
{ field: 'ticket_replies', operator: 'greater_than', value: '6' }
Action has three attributes :action_name
, :value
.
Example action:
{ action_name: 'set_status', value: '2' }
Public interface:
RulesProcessor::Processor.new(records: {ticket: ticket, comment: comment},
options: {},
ruleset: ruleset).process
You can compare against any method which is defined in class of comparable record.
For example if you want to set condition:
{ field: 'ticket_replies', operator: 'greater_than', value: '6' }
Just create method on your record class:
def replies
replies.size
end
Operator | Description |
---|---|
is |
Equality comparison for string and integers |
is_not |
Equality comparison for string and integers |
include |
Contains at least one of words |
not_include |
Contains none of the words |
less_than |
Compare integers |
less_than_equal |
Compare integers |
greater_than |
Compare integers |
greater_than_equal |
Compare integers |
There are three groups of operators:
Group | Operators |
---|---|
base |
is , is_not |
includable |
include , not_include |
comparable |
less_than , less_than_equal , greater_than , greater_than_equal |
There is a module which returns predefined operators sets.
You can use it in such way:
operators = RulesProcessor::OperatorsToSelect
def ticket_subject_options
operators.base + operators.includable
end
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
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/rgrabowski/rules_processor. 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.
The gem is available as open source under the terms of the MIT License.