Sha256: 2c7cfb11960444738a964f0a2b071f33327f3ced52a5a9725f376ed279c24b28

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

# Represents a logical rule object.
class 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/>
  #  */
  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

  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

  def size
    @outcome_clause_hash.size
  end

  # /**
  #  * Removes the leading and trailing spaces of rule tokens.
  #  *
  #  * @param string rule clause.
  #  * @param separator rule clause token.
  #  */
  def self.remove_spaces(token: nil, separator: '')
    escape = %w[( ) |].include?(separator) ? '\\' : ''
    token.to_s.gsub(Regexp.new("\\s*#{escape}#{separator}\\s*"), separator)
  end

  # /**
  #  * @return the actionList
  #  */
  def outcomes
    @outcome_clause_hash.keys
  end

  # /**
  #  * @param action action which rule we want to retrieve.
  #  * @return the actionToRuleClauses
  #  */
  def clause(outcome: '')
    @outcome_clause_hash[outcome]
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rast-1.0.0 lib/rast/rules/rule.rb