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.



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

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



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

def accepted_patterns
  @accepted_patterns
end

#excluded_patternsObject

Mostly for easier specs



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

def excluded_patterns
  @excluded_patterns
end

#excluded_symbolsObject

Mostly for easier specs



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

def excluded_symbols
  @excluded_symbols
end

Instance Method Details

#==(oth) ⇒ Object

Mostly a spec method



294
295
296
297
298
299
# File 'lib/rufus/treechecker.rb', line 294

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



223
224
225
226
# File 'lib/rufus/treechecker.rb', line 223

def accept_pattern(pat)

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

#check(sexp) ⇒ Object



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

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] || []
    return false if pats.find { |pat| check_pattern(sexp, pat) }

    pats = @excluded_patterns[sexp.first] || []

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

  true
end

#exclude_pattern(pat, message) ⇒ Object



228
229
230
231
232
# File 'lib/rufus/treechecker.rb', line 228

def exclude_pattern(pat, message)

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

#exclude_symbol(s, message) ⇒ Object



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

def exclude_symbol(s, message)

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

#freezeObject



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rufus/treechecker.rb', line 258

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



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

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