# Rulz Rulz is a rule engine for Ruby, allowing developers to define rules in their classes. ## Installation Add this line to your application's Gemfile: gem 'rulz' And then execute: $ bundle Or install it yourself as: $ gem install rulz ## Usage In the model(s) which you want to apply rules to, include the rulz module (you may need to `require` it first): include Rulz ### Defining Rules #### Conditions and Actions define_rulz do condition "less than" do |other| it < other end condition "contains" do |other| it.contains? other end action "assign to user" do |user| its.user = user end end When defining conditions, it is possible to reference other condition: condition "not equal to" do |other| opposite_of "equal to", other end Actions can be combined in a similar way: action "put on shoes" do action "put on socks" its.feet << "shoes" end The following conditions come predefined with rulz: * less than * greater than * equal to * not equal to * contains * does not contain * matches * does not match * like * not like To load these conditions for the whole class `include` any of the following modules * Rulz::Conditions::Comparison * Rulz::Conditions::Container * Rulz::Conditions::String To load these conditions for a cetain attribute, call the `type` method inside the attribute block: define_rulz do attribute :foo do type :integer end end The following types are allowed: * integer * float * string * array * hash #### It "it" refers to the object that the conditions/rules are being applied to. For readability, "it" is aliased to "its" #### Rules class Number < Fixnum define_rulz do condition "even" do it % 2 == 0 end condition "odd" do opposite_of "even" end action "make even" do it += 1 end rule do where("odd") { apply! "make even" } end end end Conditions can also be combined in `where` statements: rule do where(["odd", "AND", "prime"]) { apply! "make even" } end Where statements can also accept the attribute method: rule do where :id => "odd" do apply! "make even" end end rule do where :id => ["multiple of", 5] do apply! "make even" end end When options need to be combined: rule do where [{:id => ["multiple of", 5]}, "AND", "odd"] do apply! "make even" end end ### Applying Rules To apply rules to a single object, call the `apply_rules!` method like so: m = Melon.new m.apply_rules! ## 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