require 'ferret' require 'pyrosoma/database' require 'pyrosoma/pattern' class Pyrosoma class Dispatcher attr_accessor :paths def initialize(db) @db = db db.add_observer self update_all end def update(frame) paths.delete_if { |(f, p)| f == frame[:id] } if frame[:path] paths << [frame[:id], Pattern.new(frame[:path])] end sort! end def update_all self.paths = [] @db.search_each(Ferret::Search::WildcardQuery.new(:path, "/*")) { |frame,_| update @db[frame] } end def sort! self.paths = paths.sort_by { |(f, p)| p.generality } end def dispatch(path) matching_frame, matching_pattern = paths.find { |frame, pattern| pattern =~ path } if matching_frame.nil? return nil # not found end match = matching_pattern.match(path) @db[matching_frame].merge(match) end end end