Class: Ruleby::Steel::WhenBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/steel.rb

Overview

< RulebookHelper

Instance Method Summary collapse

Constructor Details

#initializeWhenBuilder

Returns a new instance of WhenBuilder.



79
80
81
82
# File 'lib/dsl/steel.rb', line 79

def initialize()
  @pattern_hash = Hash.new
  @pattern_keys = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dsl/steel.rb', line 84

def method_missing(method_id, *args, &block)
  method = method_id.to_sym
  wi = nil
  if @pattern_hash.key? method
    wi = @pattern_hash[method]
  elsif :not == method
    @pattern_keys.push method
    return self
  else
    wi = WhenInternal.new method, args[0]
    @pattern_hash[method] = wi
    @pattern_keys.push method
  end
  return wi
end

Instance Method Details

#and_pattern(operands) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dsl/steel.rb', line 124

def and_pattern(operands)
  # TODO raise exception if referenceAtoms from the right do not
  # have the values they referenece in the left
  # TODO raise exception if there are repeated tags?
  left = nil
  operands.each do |operand|
    if left.nil?
      left = operand
    else           
      right = operand
      left = Ruleby::Core::AndPattern.new(left, right)
    end
  end
  left
end

#or_pattern(operands) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dsl/steel.rb', line 140

def or_pattern(operands)
  # TODO raise exception if referenceAtoms from the right do not
  # have the values they referenece in the left
  # TODO raise exception if there are repeated tags?
  left = nil
  operands.each do |operand|
    if left.nil?
      left = operand
    else           
      right = operand
      left = Ruleby::Core::OrPattern.new(left, right)
    end
  end
  left
end

#patternObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dsl/steel.rb', line 100

def pattern
  operands = []
  nt = false
  @pattern_keys.each do |key|
    if :not != key
      wi = @pattern_hash[key]
      tag = wi.tag
      type = wi.type
      atoms = wi.to_atoms
      p = nil
      if nt
        p = Ruleby::Core::NotPattern.new(tag, type, atoms)
        nt = false
      else
        p = Ruleby::Core::ObjectPattern.new(tag, type, atoms)
      end
      operands = operands + [p]
    else
      nt = true
    end
  end
  return and_pattern(operands)
end