Class: Hyde::Path

Inherits:
Node
  • Object
show all
Defined in:
lib/hyde/path.rb

Overview

Primary building block of request navigation.

Direct Known Subclasses

Server

Constant Summary collapse

Binding =
Hyde::PathBinding

Instance Attribute Summary collapse

Attributes inherited from Node

#remap, #root

Instance Method Summary collapse

Methods inherited from Node

#go, #reject

Constructor Details

#initialize(path, parent:, &setup) ⇒ Path

Returns a new instance of Path.

Parameters:

  • path (Object)

    Object to generate Hyde::Pattern from

  • parent (Hyde::Node)

    Parent object to inherit properties to

  • setup (#call)

    Setup block



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hyde/path.rb', line 27

def initialize(path, parent:, &setup)
  super(path, parent: parent)
  # Child nodes array
  @children = []
  # Arrays of preprocessors, postprocessors and filters
  @preprocessors = []
  @postprocessors = []
  @filters = []

  binding = Binding.new(self)
  binding.instance_exec(&setup)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



86
87
88
# File 'lib/hyde/path.rb', line 86

def children
  @children
end

#propertiesObject (readonly)

Returns the value of attribute properties.



86
87
88
# File 'lib/hyde/path.rb', line 86

def properties
  @properties
end

Instance Method Details

#filter(&block) {|request| ... } ⇒ Object

Add a filter to the path. Blocks path access if a filter returns false.

Parameters:

  • block (#call)

Yield Parameters:



82
83
84
# File 'lib/hyde/path.rb', line 82

def filter(&block)
  @filters.append(block)
end

#postprocess(&block) {|request, response| ... } ⇒ Object

Add a postprocessor to the path.

Parameters:

  • block (#call)

Yield Parameters:



74
75
76
# File 'lib/hyde/path.rb', line 74

def postprocess(&block)
  @postprocessors.append(block)
end

#preprocess(&block) {|request| ... } ⇒ Object

Add a preprocessor to the path. Does not modify path execution.

Parameters:

  • block (#call)

Yield Parameters:



66
67
68
# File 'lib/hyde/path.rb', line 66

def preprocess(&block)
  @preprocessors.append(block)
end

#process(request) ⇒ Boolean

Method callback on successful request navigation. Finds the next appropriate path to go to.

Returns:

  • (Boolean)

    true if further navigation will be done

Raises:

  • (UncaughtThrowError)

    by default throws :response if no matches found.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hyde/path.rb', line 44

def process(request)
  return false unless run_filters(request)

  run_preprocessors(request)
  enqueue_postprocessors(request)
  @children.each do |x|
    if (value = x.go(request))
      return value
    end
  end
  value = index(request)
  return value if value

  _die(404)
rescue StandardError => e
  _die(500, backtrace: [e.to_s] + e.backtrace)
end