class Utopia::Controller::Actions::Action

A nested action lookup hash table.

Constants

WILDCARD

Matches any 1 path component.

WILDCARD_GREEDY

Matches 0 or more path components.

Attributes

callback[RW]
options[RW]

Public Class Methods

new(options = {}, &block) click to toggle source
Calls superclass method
# File lib/utopia/controller/actions.rb, line 39
def initialize(options = {}, &block)
        @options = options
        @callback = block
        
        super()
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/utopia/controller/actions.rb, line 60
def == other
        super and @callback == other.callback and @options == other.options
end
apply(path, index = -1) { |match_all| ... } click to toggle source

Given a path, iterate over all actions that match. Actions match from most specific to most general. @return nil if nothing matched, or true if something matched.

# File lib/utopia/controller/actions.rb, line 72
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
callback?() click to toggle source
# File lib/utopia/controller/actions.rb, line 48
def callback?
        @callback != nil
end
define(path, **options, &callback) click to toggle source
# File lib/utopia/controller/actions.rb, line 103
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
eql?(other) click to toggle source
Calls superclass method
# File lib/utopia/controller/actions.rb, line 52
def eql? other
        super and @callback.eql? other.callback and @options.eql? other.options
end
hash() click to toggle source
Calls superclass method
# File lib/utopia/controller/actions.rb, line 56
def hash
        [super, @callback, @options].hash
end
inspect() click to toggle source
Calls superclass method
# File lib/utopia/controller/actions.rb, line 117
def inspect
        if callback?
                "<action " + super + ":#{callback.source_location}(#{options})>"
        else
                "<action " + super + ">"
        end
end
matching(path, &block) click to toggle source
# File lib/utopia/controller/actions.rb, line 99
def matching(path, &block)
        to_enum(:apply, path).to_a
end