Class: Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/rast/rules/rule.rb

Overview

Represents a logical rule object.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rules: {}) ⇒ Rule

/**

* TODO: Validation of the output_clause_src parameter.
*
* Instantiate a rule with the given clause. <br/>
* <br/>
* <b>Parameter Example:</b><br/>
* Visible:Proposed|Approved<br/>
* <br/>
*/


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

#outcomesObject

/**

* @return the actionList
*/


51
52
53
# File 'lib/rast/rules/rule.rb', line 51

def outcomes
  @outcome_clause_hash.keys
end

#sizeObject



33
34
35
# File 'lib/rast/rules/rule.rb', line 33

def size
  @outcome_clause_hash.size
end