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

ProcContext =
Hyde::ProcessorContext
Context =
Hyde::PathContext

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



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hyde/path.rb', line 38

def initialize(path, parent:, &setup)
  super(path, parent: parent)
  # Child nodes array
  @children = []
  # Arrays of preprocessors, postprocessors and filters
  @preprocessors = []
  @postprocessors = []
  @filters = []
  # Contexts setup
  context = self.class::Context.new(self)
  context.instance_exec(&setup)
  @proccontext = self.class::ProcContext.new(self)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



98
99
100
# File 'lib/hyde/path.rb', line 98

def children
  @children
end

#propertiesObject (readonly)

Returns the value of attribute properties.



98
99
100
# File 'lib/hyde/path.rb', line 98

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:



94
95
96
# File 'lib/hyde/path.rb', line 94

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

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

Add a postprocessor to the path.

Parameters:

  • block (#call)

Yield Parameters:



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

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:



78
79
80
# File 'lib/hyde/path.rb', line 78

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hyde/path.rb', line 56

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