Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/restorando/rbrules
Declare rule sets to check your objects against them later
https://github.com/restorando/rbrules
Last synced: about 1 month ago
JSON representation
Declare rule sets to check your objects against them later
- Host: GitHub
- URL: https://github.com/restorando/rbrules
- Owner: restorando
- License: mit
- Created: 2014-03-14T19:01:35.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-09-01T14:14:09.000Z (over 9 years ago)
- Last Synced: 2024-04-20T17:43:22.385Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 206 KB
- Stars: 12
- Watchers: 34
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# RbRules
This library simplifies a rule set definition that can later be used to check if they
are satisfied for a given object or to find the rule that a given object doesn't satisfy.## Installation
Add this line to your application's Gemfile:
gem 'rbrules'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rbrules
## Usage
Define your ruleset:
```ruby
MY_HOUSE_MY_RULES = RbRules.new do |rules|
rules.rule(:smoke) { |age| age > 21 }
rules.rule(:sleep) { |age| age.even? }
end
```Test your object
```ruby
MY_HOUSE_MY_RULES.all? 22 # => true
MY_HOUSE_MY_RULES.none? 12 # => false
```If you don't want to pollute your global namespace to define global rules, you can give
your a name to your rule set like this:```ruby
RbRules[:salute].rule(:hawaiian) {|string| string =~ /aloha/i }
RbRules[:salute].rule(:english) {|string| string =~ /hello|good bye/i }RbRules[:salute].any? "Aloha world!"
```You can also define your custom rules (which should respond to `#call(obj)`) in case
you need to take different actions when different rules failFor example:
```ruby
class MagicNumber < Struct.new(:magic_number)def call(obj)
magic_number == obj
endend
RbRules[:random_rules].rule MagicNumber.new(3)
matching_rule = RbRules[:random_rules].any?(3)
matching_rule.magic_number # => 3
```Adding new rules to an existing one
You can add new rules to existing ones using the `ruby + ` operator.
```ruby
new_rule = RbRules.new do |rules|
rules.rule(:alive) { |age| age < 1000 }
endNEW_HOUSE_RULES = MY_HOUSE_MY_RULES + new_rule
NEW_HOUSE_RULES.all? 19 # => false
NEW_HOUSE_RULES.all? 22 # => false
NEW_HOUSE_RULES.any? 95 # => true
NEW_HOUSE_RULES.all? 94 # => true
```## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request