Class: Compo::Finders::Url

Inherits:
Object
  • Object
show all
Defined in:
lib/compo/finders/url.rb

Overview

A method object for finding an item in a composite tree via its URL

These are not thread-safe.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, url, missing_proc: nil) ⇒ Url

Initialises an URL finder

Examples:

Initialises an UrlFinder with default missing resource

handling.
UrlFinder.new(composite, 'a/b/c')

Initialises an UrlFinder returning a default value.

UrlFinder.new(composite, 'a/b/c', missing_proc=->(_) { :default })


26
27
28
29
30
31
32
# File 'lib/compo/finders/url.rb', line 26

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

Examples:

Finds a URL with default missing resource handling.

UrlFinder.find(composite, 'a/b/c') { |item| p item }

Yield Parameters:

  • resource (ModelObject)

    The resource found.

  • args (Array)

    The splat from above.



45
46
47
# File 'lib/compo/finders/url.rb', line 45

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.

Examples:

Runs an UrlFinder, returning the item unchanged.

finder.run { |item| item }
#=> item

Yield Parameters:

  • resource (ModelObject)

    The resource found.

  • args (Array)

    The splat from above.



63
64
65
66
67
68
69
70
# File 'lib/compo/finders/url.rb', line 63

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