Class: Sawtooth::Rules::Set

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeSet

Creates a new rule set.



39
40
41
# File 'lib/sawtooth/rules/set.rb', line 39

def initialize
  @items = []
end

Instance Attribute Details

#itemsObject (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

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