Class: Rufus::TreeChecker::RuleSet

Inherits:
Object
  • Object
show all
Defined in:
lib/rufus/treechecker.rb

Instance Method Summary collapse

Constructor Details

#initializeRuleSet

Returns a new instance of RuleSet.



206
207
208
209
210
211
# File 'lib/rufus/treechecker.rb', line 206

def initialize

  @excluded_symbols = {} # symbol => exclusion_message
  @accepted_patterns = {} # 1st elt of pattern => pattern
  @excluded_patterns = {} # 1st elt of pattern => pattern, excl_message
end

Instance Method Details

#accept_pattern(pat) ⇒ Object



226
227
228
229
# File 'lib/rufus/treechecker.rb', line 226

def accept_pattern(pat)

  (@accepted_patterns[pat.first] ||= []) << pat
end

#check(sexp) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/rufus/treechecker.rb', line 237

def check(sexp)

  if sexp.is_a?(Symbol)

    m = @excluded_symbols[sexp]
    raise SecurityError.new(m) if m

  elsif sexp.is_a?(Array)

    # accepted patterns are evaluated before excluded patterns
    # if one is found the excluded patterns are skipped

    pats = @accepted_patterns[sexp.first]
    pats.each { |pat| return if check_pattern(sexp, pat) } if pats

    pats = @excluded_patterns[sexp.first]
    return unless pats

    pats.each do |pat, msg|
      raise SecurityError.new(msg) if check_pattern(sexp, pat)
    end
  end
end

#cloneObject



213
214
215
216
217
218
219
# File 'lib/rufus/treechecker.rb', line 213

def clone
  rs = RuleSet.new
  rs.instance_variable_set(:@excluded_symbols, @excluded_symbols.dup)
  rs.instance_variable_set(:@accepted_patterns, @accepted_patterns.dup)
  rs.instance_variable_set(:@excluded_patterns, @excluded_patterns.dup)
  rs
end

#exclude_pattern(pat, message) ⇒ Object



231
232
233
234
235
# File 'lib/rufus/treechecker.rb', line 231

def exclude_pattern(pat, message)

  (@excluded_patterns[pat.first] ||= []) << [
    pat, message || "#{pat.inspect} is excluded" ]
end

#exclude_symbol(s, message) ⇒ Object



221
222
223
224
# File 'lib/rufus/treechecker.rb', line 221

def exclude_symbol(s, message)

  @excluded_symbols[s] = (message || ":#{s} is excluded")
end

#freezeObject



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/rufus/treechecker.rb', line 261

def freeze

  super

  @excluded_symbols.freeze
  @excluded_symbols.each { |k, v| k.freeze; v.freeze }
  @accepted_patterns.freeze
  @accepted_patterns.each { |k, v| k.freeze; v.freeze }
  @excluded_patterns.freeze
  @excluded_patterns.each { |k, v| k.freeze; v.freeze }
end

#to_sObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/rufus/treechecker.rb', line 273

def to_s

  s = "#{self.class} (#{self.object_id})\n"
  s << "  excluded symbols :\n"
  @excluded_symbols.each do |k, v|
    s << "    - #{k.inspect}, #{v}\n"
  end
  s << "  accepted patterns :\n"
  @accepted_patterns.each do |k, v|
    v.each do |p|
      s << "    - #{k.inspect}, #{p.inspect}\n"
    end
  end
  s << "  excluded patterns :\n"
  @excluded_patterns.each do |k, v|
    v.each do |p|
      s << "    - #{k.inspect}, #{p.inspect}\n"
    end
  end
  s
end