Class: Yay::RuleSet

Inherits:
Object
  • Object
show all
Defined in:
lib/yay/rule_set.rb

Overview

manages colour rules. including variable substitutions

Instance Method Summary collapse

Constructor Details

#initializeRuleSet

Returns a new instance of RuleSet.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/yay/rule_set.rb', line 6

def initialize
  # the eventual ruleset
  @rules          = []

  # variable = colour
  @var_to_colours = {}
  
  # variable = variable
  @var_to_var     = {}
  
  # string = variable
  @string_to_var   = []
end

Instance Method Details

#add_assignment(strings, variable) ⇒ Object

add a STRING = VARIABLE match rule. the variable will be substuted later



45
46
47
48
49
# File 'lib/yay/rule_set.rb', line 45

def add_assignment strings, variable
  strings.each { |string|
    @string_to_var.push [string, variable]
  }
end

#add_equivalence(x, y) ⇒ Object

add a VARIABLE = VARIABLE equivalence rule. x will be substituted later



52
53
54
# File 'lib/yay/rule_set.rb', line 52

def add_equivalence x, y
  @var_to_var[x] = y
end

#add_match(strings, colours, is_line) ⇒ Object

add a simple STRING = COLOUR match rule



32
33
34
35
36
# File 'lib/yay/rule_set.rb', line 32

def add_match strings, colours, is_line
  strings.each { |string|
    @rules.push [string, colours, is_line]
  }
end

#add_substitution(variable, colours, is_line) ⇒ Object

add a VARIABLE = COLOUR match rule so we can substitute variables later



39
40
41
42
# File 'lib/yay/rule_set.rb', line 39

def add_substitution variable, colours, is_line
  raise AlreadyAssignedError.new variable if @var_to_colours[variable] || @var_to_var[variable]
  @var_to_colours[variable] = [colours, is_line]
end

#get_rulesObject



25
26
27
28
29
# File 'lib/yay/rule_set.rb', line 25

def get_rules
  # ensure we substitute all variables
  substitute_variables if @string_to_var
  @rules
end

#merge(rules) ⇒ Object

merge with another ruleset



21
22
23
# File 'lib/yay/rule_set.rb', line 21

def merge rules
  @rules = @rules | rules
end

#substitute_variable(variable) ⇒ Object

try to find the value of a variable by making substitutions



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yay/rule_set.rb', line 57

def substitute_variable variable

  path    = []
  result  = nil
  current = variable

  while true 
    # detect circular references
    raise CircularReferenceError.new(current, path) unless path.index(current).nil?

    path.push current
    
    # see if this variable has a value
    result  = @var_to_colours[current]
    break if result
    
    # see if this variable is a reference to another variable
    current = @var_to_var[current]
    break if current.nil?
  end
  
  raise UnresolvedSubstitutionError.new variable unless result
  return result
end

#substitute_variablesObject



82
83
84
85
86
87
88
89
90
# File 'lib/yay/rule_set.rb', line 82

def substitute_variables
  @string_to_var.each { |ref| 
    string   = ref[0]
    variable = ref[1]
    result   = substitute_variable(variable)
    add_match([string], result[0], result[1])
  }
  @string_to_var = []
end