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

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

Constant Summary collapse

WILDCARD_GREEDY =
'**'.freeze
WILDCARD =
'*'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Action.



31
32
33
34
35
36
# File 'lib/utopia/controller/actions.rb', line 31

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

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



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

def callback
  @callback
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object



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

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

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

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

Returns:

  • nil if nothing matched, or true if something matched.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/utopia/controller/actions.rb', line 61

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

Returns:

  • (Boolean)


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

def callback?
	@callback != nil
end

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



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/utopia/controller/actions.rb', line 92

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

Returns:

  • (Boolean)


44
45
46
# File 'lib/utopia/controller/actions.rb', line 44

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

#hashObject



48
49
50
# File 'lib/utopia/controller/actions.rb', line 48

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

#inspectObject



106
107
108
109
110
111
112
# File 'lib/utopia/controller/actions.rb', line 106

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

#matching(path, &block) ⇒ Object



88
89
90
# File 'lib/utopia/controller/actions.rb', line 88

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