Skip to content

Commit

Permalink
whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Aug 17, 2011
1 parent 8ef996a commit 600303f
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 107 deletions.
24 changes: 12 additions & 12 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ Rule is a state machine / rule engine. It does things.
3. create a file at app/rule_engines/my_awesome_rule_engine.rb

class MyAwesomeRuleEngine < Rule::Engine::Base

state :new
state :ongoing
state :closed

initial_state :new

terminal_state :closed

transition :new, :ongoing
validate IsInProgressRule
end
end

transition :ongoing, :closed
assert_presence_of object.closed_at, "Closed At"
end
end
assert_presence_of object.closed_at, "Closed At"
end

end

4. Create a file at app/rules/is_in_progress_rule.rb

Expand All @@ -43,15 +43,15 @@ Rule is a state machine / rule engine. It does things.
@object.in_progress.present?
end
end

5. Set your application up to run the rules.

# wherever it makes sense...
my_object.run_rules # advances state as far as possible and automatically saves

== Explain all that stuff.

Okay.
Okay.

The Rule::Engine::Base subclass defines states and transitions. Each transition block contains a number of assertions and an optional priority. If any of the assertions does not pass, the state will not follow this transition. The priority decides which transition to use in the case of two possible transitions out of the current state both being valid. Use it by specifying "priority :high" or "priority :low" in the transition block.

Expand Down
6 changes: 3 additions & 3 deletions lib/rule/active_record_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ class ActiveRecord::Base
def self.rule_engine(engine, opts={})
include(Rule::Engine::ActiveRecord)
column = opts[:column] || :state

@__rule_engine = engine.new(column)
end
end
end
end

6 changes: 3 additions & 3 deletions lib/rule/allow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Rule
class Allow < Base
def validate
return true
end
end
end
end
end
end



18 changes: 9 additions & 9 deletions lib/rule/assertions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module Assertions
def assert(assertion, message)
unless assertion
add_error message
end
end
end
end

def assert_presence_of(thing, name)
unless thing.present?
add_error "#{name} must be provided"
end
end
end
end

def assert_absence_of(thing, name)
unless thing.blank?
add_error "#{name} must be blank"
end
end
end
end
end
end
end
end
14 changes: 7 additions & 7 deletions lib/rule/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ class Base
def initialize(object)
@object = object
@errors = []
end
end

def add_error(error)
@errors << error
end
end

def pass?
validate != false and @errors.none?
end
end

include Rule::Assertions
end
end
end
end

6 changes: 3 additions & 3 deletions lib/rule/disallow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Rule
class Disallow < Base
def validate
return false
end
end
end
end
end
end



24 changes: 12 additions & 12 deletions lib/rule/engine/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ module ActiveRecord
extend ActiveSupport::Concern

included do
end

end

module InstanceMethods
def run_rules
engine = self.class.instance_variable_get("@__rule_engine")
engine.run!(self)
end
end
end
end

module ClassMethods
end
end
end
end

end

end
end
end


42 changes: 21 additions & 21 deletions lib/rule/engine/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ class Base

def initialize(column)
@column = column
end
end

def self.state(state)
find_or_create_state(state)
end
end

def self.initial_state(state)
@initial_state = find_or_create_state(state)
end
end

def self.terminal_state(state)
@terminal_states ||= []
@terminal_states << find_or_create_state(state)
end
end

def self.transition(from, to, &blk)
from_state = find_state!(from)
to_state = find_state!(to)

from_state.add_transition(to_state, blk)
end
end

def run!(object)
# state = self.class.find_state(object.send(@column).try(:to_sym)) || self.class.instance_variable_get("@initial_state")
Expand All @@ -34,30 +34,30 @@ def run!(object)
prev_state = state
state = prev_state.next_state(object)
break if prev_state == state
end
end

object.send("#{@column}=", state.name)
end
end

private #################################################################

def self.find_or_create_state(name)
@states ||= []
unless state = @states.find { |state| state.name == name }
state = Rule::Engine::State.new(name)
@states << state
end
end
state
end
end

def self.find_state(name)
@states.find { |state| state.name == name }
end
end

def self.find_state!(name)
find_state(name) or raise "State #{name} not declared"
end
end
end
end
end

end
end
end
4 changes: 2 additions & 2 deletions lib/rule/engine/invalid_transition.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Rule::Engine::InvalidTransition < StandardError
end

end
28 changes: 14 additions & 14 deletions lib/rule/engine/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ module Engine
class State

attr_reader :name

def initialize(name)
@name = name
@transitions = []
end
end

def add_transition(to, blk)
@transitions << Transition.new(self, to, blk)
end
end

def valid_transitions(object)
@transitions.map { |transition|
begin
transition.run!(object)
rescue Rule::Engine::InvalidTransition
nil
else
else
transition
end
end
}.compact
end
end

def next_state(object)
transitions = valid_transitions(object)
Expand All @@ -32,15 +32,15 @@ def next_state(object)
sort_by{|transition| transition.instance_variable_get("@priority")}.
last.
to_state
else
else
return self
end
end
end
end



end
end
end
end
end
end



38 changes: 19 additions & 19 deletions lib/rule/engine/transition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class Transition
attr_reader :from, :to, :action, :object
alias_method :to_state, :to
alias_method :from_state, :from
PRIORITIES = {

PRIORITIES = {
:min => 0,
:very_low => 1,
:low => 3,
Expand All @@ -14,45 +14,45 @@ class Transition
:very_high => 9,
:max => 10
}

def initialize(from, to, action)
@from = from
@to = to
@action = action
@priority = PRIORITIES[:normal]
end
end

def run!(object)
@object = object
action.bind(self).call
end
end

def add_error(error)
raise Rule::Engine::InvalidTransition
end
end

include Rule::Assertions

def validate(rule_klass)
unless rule_klass.new(@object).pass?
raise Rule::Engine::InvalidTransition
end
end
end
end

def priority(priority)
case priority
when Numeric
@priority = priority
when Symbol
@priority = PRIORITIES[:normal]
else
else
raise "Invalid priority"
end
end
end
end
end
end
end

end
end
end



Expand Down
Loading

0 comments on commit 600303f

Please sign in to comment.