module RulesEngine module Rule class <%=rule_class%> < RulesEngine::Rule::Definition ################################################################## # class options self.options = { :group => 'Twitter', :display_name => 'Twitter Filter Retweets', :help_partial => '/re_rule_definitions/<%=rule_name%>/help', :new_partial => '/re_rule_definitions/<%=rule_name%>/new', :edit_partial => '/re_rule_definitions/<%=rule_name%>/edit' } ################################################################## # set the rule data def data= data if data.nil? @title = nil else @title, ignore = ActiveSupport::JSON.decode(data) end end ################################################################## # get the rule attributes def title @title end def summary "Filter out retweets" end def data [title].to_json end def expected_outcomes [ {:outcome => RulesEngine::Rule::Outcome::NEXT}, {:outcome => RulesEngine::Rule::Outcome::STOP_SUCCESS, :title => "Stop if retweet found"} ] end ################################################################## # set the rule attributes def attributes=(params) param_hash = params.symbolize_keys @title = param_hash[:<%=rule_name%>_title] end ################################################################## # validation and errors def valid? @errors = {} @errors[:<%=rule_name%>_title] = "Title required" if title.blank? return @errors.empty? end ################################################################## # callbacks when the rule is added and removed from a workflow def before_create() end def before_update() end def before_destroy() end ################################################################## # execute the rule # if a match is found procees to the expected outcome # it gets the data parameter :tweet def process(process_id, data) tweet = data[:tweet] || data["tweet"] if tweet.blank? return RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::NEXT) end if /\bRT\b/ =~ tweet RulesEngine::Process.auditor.audit(process_id, "Found retweet", RulesEngine::Process::AUDIT_INFO) return RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::STOP_SUCCESS) end RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::NEXT) end end end end