Class: Sawtooth::Rules::Set
- Inherits:
-
Object
- Object
- Sawtooth::Rules::Set
- Defined in:
- lib/sawtooth/rules/set.rb
Overview
Provides a set of routes and all the matching and globbing.
Rules are matched using ‘File.fnmatch`.
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Accessor for the array of rules.
Instance Method Summary collapse
-
#add(path, rule) ⇒ Object
Adds a new ‘RuleEntry`.
-
#find(*path) ⇒ Object
Find a rule matching the supplied path (or path array), if not an array, then it must be separated by ‘/`.
-
#initialize ⇒ Set
constructor
Creates a new rule set.
-
#print_rules ⇒ Object
Pretty print rules.
Constructor Details
#initialize ⇒ Set
Creates a new rule set.
39 40 41 |
# File 'lib/sawtooth/rules/set.rb', line 39 def initialize @items = [] end |
Instance Attribute Details
#items ⇒ Object (readonly)
Accessor for the array of rules.
36 37 38 |
# File 'lib/sawtooth/rules/set.rb', line 36 def items @items end |
Instance Method Details
#add(path, rule) ⇒ Object
Adds a new ‘RuleEntry`.
44 45 46 |
# File 'lib/sawtooth/rules/set.rb', line 44 def add(path, rule) self.items << RuleEntry.new(path, rule) end |
#find(*path) ⇒ Object
Find a rule matching the supplied path (or path array), if not an array, then it must be separated by ‘/`.
If no matching rule is found, ‘default` is returned.
52 53 54 55 56 |
# File 'lib/sawtooth/rules/set.rb', line 52 def find(*path) path = path.flatten.join('/') match = self.items.find { |rule| rule.matches?(path) } match.rule if match end |
#print_rules ⇒ Object
Pretty print rules.
59 60 61 |
# File 'lib/sawtooth/rules/set.rb', line 59 def print_rules items.inject('') { |str, entry| str << "#{entry.orig_path} => #{entry.rule.print_rule}\n" } end |