Class: Utopia::Controller::Actions::Action

Inherits:
Hash
  • Object
show all
Defined in:
lib/utopia/controller/actions.rb

Overview

A nested action lookup hash table.

Constant Summary collapse

WILDCARD_GREEDY =

Matches 0 or more path components.

"**".freeze
WILDCARD =

Matches any 1 path component.

"*".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Action

Initialize an action lookup node.



28
29
30
31
32
33
# File 'lib/utopia/controller/actions.rb', line 28

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

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



35
36
37
# File 'lib/utopia/controller/actions.rb', line 35

def callback
  @callback
end

#optionsObject

Returns the value of attribute options.



35
36
37
# File 'lib/utopia/controller/actions.rb', line 35

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object

Compare this object with another object.



59
60
61
# File 'lib/utopia/controller/actions.rb', line 59

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

#apply(path, index = -1,, &block) ⇒ Object

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/utopia/controller/actions.rb', line 74

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?Boolean

Check whether this action has a callback.

Returns:

  • (Boolean)


39
40
41
# File 'lib/utopia/controller/actions.rb', line 39

def callback?
	@callback != nil
end

#define(path, **options, &callback) ⇒ Object

Define an action at the given path.



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/utopia/controller/actions.rb', line 113

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) ⇒ Boolean

Check whether this object is equivalent to another object.

Returns:

  • (Boolean)


46
47
48
# File 'lib/utopia/controller/actions.rb', line 46

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

#hashObject

Compute the hash value for this object.



52
53
54
# File 'lib/utopia/controller/actions.rb', line 52

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

#inspectObject

Generate a debug representation of this object.



129
130
131
132
133
134
135
# File 'lib/utopia/controller/actions.rb', line 129

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

#matching(path, &block) ⇒ Object

Collect the actions matching the given path.



104
105
106
# File 'lib/utopia/controller/actions.rb', line 104

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