Class: Rule
- Inherits:
-
Object
- Object
- Rule
- Defined in:
- lib/rast/rules/rule.rb
Overview
Represents a logical rule object.
Class Method Summary collapse
-
.remove_spaces(token: nil, separator: '') ⇒ Object
/** * Removes the leading and trailing spaces of rule tokens.
- .sanitize(clause: '') ⇒ Object
Instance Method Summary collapse
-
#clause(outcome: '') ⇒ Object
/** * @param action action which rule we want to retrieve.
-
#initialize(rules: {}) ⇒ Rule
constructor
/** * TODO: Validation of the output_clause_src parameter.
-
#outcomes ⇒ Object
/** * @return the actionList */.
- #size ⇒ Object
Constructor Details
#initialize(rules: {}) ⇒ Rule
/**
- TODO: Validation of the output_clause_src parameter.
- Instantiate a rule with the given clause.
-
- Parameter Example:
- Visible:Proposed|Approved
-
*/
14 15 16 17 18 19 20 21 |
# File 'lib/rast/rules/rule.rb', line 14 def initialize(rules: {}) raise 'Must not have empty rules' if rules.empty? @outcome_clause_hash = {} rules.each do |outcome, clause| @outcome_clause_hash[outcome.to_s] = Rule.sanitize(clause: clause) end end |
Class Method Details
.remove_spaces(token: nil, separator: '') ⇒ Object
/**
- Removes the leading and trailing spaces of rule tokens.
- @param string rule clause.
- @param separator rule clause token. */
43 44 45 46 |
# File 'lib/rast/rules/rule.rb', line 43 def self.remove_spaces(token: nil, separator: '') escape = %w[( ) |].include?(separator) ? '\\' : '' token.to_s.gsub(Regexp.new("\\s*#{escape}#{separator}\\s*"), separator) end |
.sanitize(clause: '') ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/rast/rules/rule.rb', line 23 def self.sanitize(clause: '') return clause if clause.is_a?(Array) cleaner = Rule.remove_spaces(token: clause, separator: '(') cleaner = Rule.remove_spaces(token: cleaner, separator: ')') cleaner = Rule.remove_spaces(token: cleaner, separator: '&') cleaner = Rule.remove_spaces(token: cleaner, separator: '|') Rule.remove_spaces(token: cleaner, separator: '!').strip end |
Instance Method Details
#clause(outcome: '') ⇒ Object
/**
- @param action action which rule we want to retrieve.
- @return the actionToRuleClauses */
59 60 61 |
# File 'lib/rast/rules/rule.rb', line 59 def clause(outcome: '') @outcome_clause_hash[outcome] end |
#outcomes ⇒ Object
/**
- @return the actionList */
51 52 53 |
# File 'lib/rast/rules/rule.rb', line 51 def outcomes @outcome_clause_hash.keys end |
#size ⇒ Object
33 34 35 |
# File 'lib/rast/rules/rule.rb', line 33 def size @outcome_clause_hash.size end |