Class: Utopia::Controller::Actions::Action
- Inherits:
-
Hash
- Object
- Hash
- Utopia::Controller::Actions::Action
- 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
-
#callback ⇒ Object
Returns the value of attribute callback.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#==(other) ⇒ Object
-
#apply(path, index = -1,, &block) ⇒ Object
Given a path, iterate over all actions that match.
-
#callback? ⇒ Boolean
-
#define(path, **options, &callback) ⇒ Object
-
#eql?(other) ⇒ Boolean
-
#hash ⇒ Object
-
#initialize(options = {}, &block) ⇒ Action
constructor
A new instance of Action.
-
#inspect ⇒ Object
-
#matching(path, &block) ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ Action
Returns a new instance of Action
37 38 39 40 41 42 |
# File 'lib/utopia/controller/actions.rb', line 37 def initialize( = {}, &block) @options = @callback = block super() end |
Instance Attribute Details
#callback ⇒ Object
Returns the value of attribute callback
44 45 46 |
# File 'lib/utopia/controller/actions.rb', line 44 def callback @callback end |
#options ⇒ Object
Returns the value of attribute options
44 45 46 |
# File 'lib/utopia/controller/actions.rb', line 44 def @options end |
Instance Method Details
#==(other) ⇒ Object
58 59 60 |
# File 'lib/utopia/controller/actions.rb', line 58 def == other super and @callback == other.callback and @options == other. end |
#apply(path, index = -1,, &block) ⇒ Object
Given a path, iterate over all actions that match. Actions match from most specific to most general.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/utopia/controller/actions.rb', line 70 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
46 47 48 |
# File 'lib/utopia/controller/actions.rb', line 46 def callback? @callback != nil end |
#define(path, **options, &callback) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/utopia/controller/actions.rb', line 101 def define(path, **, &callback) # puts "Defining path: #{path.inspect}" current = self path.reverse_each do |name| current = (current[name] ||= Action.new) end current. = current.callback = callback return current end |
#eql?(other) ⇒ Boolean
50 51 52 |
# File 'lib/utopia/controller/actions.rb', line 50 def eql? other super and @callback.eql? other.callback and @options.eql? other. end |
#hash ⇒ Object
54 55 56 |
# File 'lib/utopia/controller/actions.rb', line 54 def hash [super, @callback, @options].hash end |
#inspect ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/utopia/controller/actions.rb', line 115 def inspect if callback? "<action " + super + ":#{callback.source_location}(#{})>" else "<action " + super + ">" end end |
#matching(path, &block) ⇒ Object
97 98 99 |
# File 'lib/utopia/controller/actions.rb', line 97 def matching(path, &block) to_enum(:apply, path).to_a end |