UtopiaSourceUtopiaControllerActionsAction

class Action

A nested action lookup hash table.

Definitions

def initialize(options = {}, &block)

Initialize an action lookup node.

Signature

parameter options Hash

Metadata associated with this action.

Implementation

def initialize(options = {}, &block)
	@options = options
	@callback = block
	
	super()
end

def callback?

Check whether this action has a callback.

Signature

returns Boolean

Whether this action has a callback.

Implementation

def callback?
	@callback != nil
end

def eql?(other)

Check whether this object is equivalent to another object.

Signature

parameter other Object

The object to compare.

returns Boolean

Whether the action mappings, callback, and options are equal.

Implementation

def eql? other
	super and @callback.eql? other.callback and @options.eql? other.options
end

def hash

Compute the hash value for this object.

Signature

returns Integer

The resulting integer.

Implementation

def hash
	[super, @callback, @options].hash
end

def ==(other)

Compare this object with another object.

Signature

parameter other Object

The object to compare.

returns Boolean

Whether the action mappings, callback, and options are equal.

Implementation

def == other
	super and @callback == other.callback and @options == other.options
end

WILDCARD_GREEDY = "**".freeze

Matches 0 or more path components.

WILDCARD = "*".freeze

Matches any 1 path component.

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

Yield all actions matching a path, from most specific to most general.

Signature

parameter path Array(String)

The path components.

parameter index Integer

The component index currently being matched.

yields {|action| ...}

Each matching action.

returns Boolean | Nil

true if any action matched, otherwise nil.

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

def matching(path, &block)

Collect the actions matching the given path.

Signature

parameter path Utopia::Path | String

The path.

returns Array(Action)

The matching actions.

Implementation

def matching(path, &block)
	to_enum(:apply, path).to_a
end

def define(path, **options, &callback)

Define an action at the given path.

Signature

parameter path Array(String)

The path components in reverse matching order.

parameter options Hash

Metadata associated with the action.

returns Action

The defined action.

Implementation

def define(path, **options, &callback)
	# puts "Defining path: #{path.inspect}"
	current = self
	
	path.reverse_each do |name|
		current = (current[name] ||= Action.new)
	end
	
	current.options = options
	current.callback = callback
	
	return current
end

def inspect

Generate a debug representation of this object.

Signature

returns String

The resulting string.

Implementation

def inspect
	if callback?
		"<action " + super + ":#{callback.source_location}(#{options})>"
	else
		"<action " + super + ">"
	end
end