Utopia SourceUtopiaControllerActionsAction

class Action

A nested action lookup hash table.

Definitions

WILDCARD_GREEDY = '**'.freeze

Matches 0 or more path components.

WILDCARD = '*'.freeze

Matches any 1 path component.

def apply(path, index = -1, &block)

Given a path, iterate over all actions that match. Actions match from most specific to most general.

Implementation

def apply(path, index = -1, &block)
	# ** is greedy, it always matches if possible and matches all remaining input.
	if match_all = self[WILDCARD_GREEDY] and match_all.callback?
		# It's possible in this callback that path is modified.
		matched = true; yield(match_all)
	end
	
	if name = path[index]
		# puts "Matching #{name} in #{self.keys.inspect}"
		
		if match_name = self[name]
			# puts "Matched against exact name #{name}: #{match_name}"
			matched = match_name.apply(path, index-1, &block) || matched
		end
		
		if match_one = self[WILDCARD]
			# puts "Match against #{WILDCARD}: #{match_one}"
			matched = match_one.apply(path, index-1, &block) || matched
		end
	elsif self.callback?
		# Got to end, matched completely:
		matched = true; yield(self)
	end
	
	return matched
end