|
1 | | -[](https://semaphoreci.com/simplybusiness/rulezilla) |
2 | | -[](https://codeclimate.com/repos/53ecc0416956800c1d01f6bf/feed) |
3 | | -[](http://badge.fury.io/rb/rulezilla) |
4 | | - |
5 | | -rulezilla |
6 | | -========= |
7 | | - |
8 | | -This provides a DSL to implement rules for various tasks. In the current version we are still relying on user to have a certain level of Ruby knowledge |
9 | | -in order to be able to use this DSL. The ultimate goal is for people without prior Ruby knowledge to be able to change and even write the Rule. |
10 | | - |
11 | | - |
12 | | -# Installation |
13 | | - |
14 | | -## Using `Gemfile` |
15 | | - |
16 | | -Add the following line to your `Gemfile`: |
17 | | - |
18 | | - gem 'rulezilla' |
19 | | - |
20 | | -Then run: |
21 | | - |
22 | | - bundle install |
23 | | - |
24 | | -## Without `Gemfile` |
25 | | - |
26 | | -On your command line run the following: |
27 | | - |
28 | | - gem install 'rulezilla' |
29 | | - |
30 | | -## Usage |
31 | | - |
32 | | -### Rules |
33 | | - |
34 | | -Rules are defined in Ruby. Rules are classes that include the `Rulezilla::DSL`. |
35 | | - |
36 | | -#### Ruby |
37 | | - |
38 | | -You can use plain Ruby to define the rule classes. But you will need to include the `Rulezilla::DSL` module. That will give you access to the DSL used to define rules. |
39 | | - |
40 | | -Here is an example: |
41 | | - |
42 | | - class RoboticsRule |
43 | | - include Rulezilla::DSL |
44 | | - |
45 | | - group :may_not_injure_human do |
46 | | - condition { not_injure_human? } |
47 | | - |
48 | | - group :obey_human do |
49 | | - condition { do_as_human_told? } |
50 | | - |
51 | | - define :protect_its_own_existence do |
52 | | - condition { protect_itself? } |
53 | | - result(true) |
54 | | - end |
55 | | - end |
56 | | - end |
57 | | - |
58 | | - default(false) |
59 | | - |
60 | | - end |
61 | | - |
62 | | -Please refer to the [feature](spec/features/rulezilla_dsl_framework.feature) for further details of the DSL. |
63 | | - |
64 | | -### Support Module |
65 | | - |
66 | | -The support module will be automatically included if its name is `"#{rule_class_name}Support"` |
67 | | - |
68 | | -e.g. if the rule class name is `RoboticsRule`, then the support would be `RoboticsRuleSupport` |
69 | | - |
70 | | - module RoboticsRuleSupport |
71 | | - def protect_itself? |
72 | | - in_danger? && not_letting_itself_be_detroyed? |
73 | | - end |
74 | | - end |
75 | | - |
76 | | -### How to execute the rule |
77 | | - |
78 | | -If the entity is: |
79 | | - |
80 | | - entity = { |
81 | | - not_injure_human?: true, |
82 | | - do_as_human_told?: true, |
83 | | - in_danger?: true, |
84 | | - not_letting_itself_be_detroyed?: true |
85 | | - } |
86 | | - |
87 | | -#### To get the first matching result output |
88 | | - |
89 | | - RoboticsRule.apply(entity) #=> true |
90 | | - |
91 | | -#### To get all matching result outputs |
92 | | - |
93 | | - RoboticsRule.all(entity) #=> [true, false] |
94 | | - |
95 | | -Note that `false` is the result outcome coming out from `default(false)` on top level, which is also called `root node`. The `root` node does not have any condition and hence |
96 | | -it is considered to be matching. This means, by consequence, that its result (`default(false)`) is included in the list of matching result outputs which `#all(entity)` above |
97 | | -returns. |
98 | | - |
99 | | -#### To get the trace of all nodes |
100 | | - |
101 | | - RoboticsRule.trace(entity) |
102 | | - #=> all the nodes instance: [root, may_not_injure_human, obey_human, protect_its_own_existence] in sequence order. |
103 | | - |
104 | | -#### To get all results from the Rule |
105 | | - |
106 | | - RoboticsRule.results #=> [true, false] |
107 | | - |
108 | | - |
109 | | -### Syntax |
110 | | - |
111 | | -Please refer to the features for DSL syntax: |
112 | | - |
113 | | -[DSL Feature](spec/features/rulezilla_dsl_framework.feature), |
114 | | - |
115 | | -[Default Support Methods Feature](spec/features/default_support_methods.feature) |
| 1 | +All documentation is now in the `docs/` subdirectory, or can be viewed in rendered form on Backstage. Start with the [index](docs/index.md) |
0 commit comments