Class: Compo::UrlFinder
- Inherits:
-
Object
- Object
- Compo::UrlFinder
- Defined in:
- lib/compo/url_finder.rb
Overview
An ‘UrlFinder’ finds an object in a composite tree via its URL
It is a method object that implements only the finding of a specific URL. UrlFinders are not thread-safe.
Class Method Summary collapse
-
.find(*args) {|resource, args| ... } ⇒ Object
Finds the model object at a URL, given a model root.
Instance Method Summary collapse
-
#initialize(root, url, missing_proc: nil) ⇒ UrlFinder
constructor
Initialises a UrlFinder.
-
#run {|resource, args| ... } ⇒ Object
Attempts to find a child resource with the given partial URL.
Constructor Details
#initialize(root, url, missing_proc: nil) ⇒ UrlFinder
Initialises a UrlFinder
25 26 27 28 29 30 31 |
# File 'lib/compo/url_finder.rb', line 25 def initialize(root, url, missing_proc: nil) @root = root @url = url @missing_proc = missing_proc || method(:default_missing_proc) reset end |
Class Method Details
.find(*args) {|resource, args| ... } ⇒ Object
Finds the model object at a URL, given a model root
44 45 46 |
# File 'lib/compo/url_finder.rb', line 44 def self.find(*args, &block) new(*args).run(&block) end |
Instance Method Details
#run {|resource, args| ... } ⇒ Object
Attempts to find a child resource with the given partial URL
If the resource is found, it will be yielded to the attached block; otherwise, an exception will be raised.
62 63 64 65 66 67 68 69 |
# File 'lib/compo/url_finder.rb', line 62 def run # We're traversing down the URL by repeatedly splitting it into its # head (part before the next /) and tail (part after). While we still # have a tail, then the URL still needs walking down. reset descend until hit_end_of_url? yield @resource end |