Class: Rufus::TreeChecker::RuleSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuleSet

Returns a new instance of RuleSet.



216
217
218
219
220
221
# File 'lib/rufus/treechecker.rb', line 216

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 Attribute Details

#accepted_patternsObject

Mostly for easier specs



214
215
216
# File 'lib/rufus/treechecker.rb', line 214

def accepted_patterns
  @accepted_patterns
end

#excluded_patternsObject

Mostly for easier specs



214
215
216
# File 'lib/rufus/treechecker.rb', line 214

def excluded_patterns
  @excluded_patterns
end

#excluded_symbolsObject

Mostly for easier specs



214
215
216
# File 'lib/rufus/treechecker.rb', line 214

def excluded_symbols
  @excluded_symbols
end

Instance Method Details

#==(oth) ⇒ Object

Mostly a spec method



308
309
310
311
312
313
# File 'lib/rufus/treechecker.rb', line 308

def ==(oth)

  @excluded_symbols == oth.instance_variable_get(:@excluded_symbols) &&
  @accepted_patterns == oth.instance_variable_get(:@accepted_patterns) &&
  @excluded_patterns == oth.instance_variable_get(:@excluded_patterns)
end

#accept_pattern(pat) ⇒ Object



238
239
240
241
# File 'lib/rufus/treechecker.rb', line 238

def accept_pattern(pat)

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

#check(sexp) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/rufus/treechecker.rb', line 249

def check(sexp)

  if sexp.is_a?(Symbol) and m = @excluded_symbols[sexp]

    raise SecurityError.new(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



223
224
225
226
227
228
229
230
231
# File 'lib/rufus/treechecker.rb', line 223

def clone

  rs = RuleSet.new
  rs.excluded_symbols = @excluded_symbols.dup
  rs.accepted_patterns = @accepted_patterns.dup
  rs.excluded_patterns = @excluded_patterns.dup

  rs
end

#exclude_pattern(pat, message) ⇒ Object



243
244
245
246
247
# File 'lib/rufus/treechecker.rb', line 243

def exclude_pattern(pat, message)

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

#exclude_symbol(s, message) ⇒ Object



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

def exclude_symbol(s, message)

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

#freezeObject



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rufus/treechecker.rb', line 272

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



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/rufus/treechecker.rb', line 284

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