Class: Landline::Path

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

Overview

Primary building block of request navigation.

Direct Known Subclasses

Server

Constant Summary collapse

Context =
Landline::PathContext

Instance Attribute Summary collapse

Attributes inherited from Node

#remap, #root

Instance Method Summary collapse

Methods inherited from Node

#reject

Constructor Details

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

Returns a new instance of Path.

Parameters:

  • path (Object)

    Object to generate Landline::Pattern from

  • parent (Landline::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/landline/path.rb', line 27

def initialize(path, parent:, **params, &setup)
  super(path, parent: parent, **params)
  # 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)
end

Instance Attribute Details

#bounceObject

Returns the value of attribute bounce.



96
97
98
# File 'lib/landline/path.rb', line 96

def bounce
  @bounce
end

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#pipelineObject

Returns the value of attribute pipeline.



96
97
98
# File 'lib/landline/path.rb', line 96

def pipeline
  @pipeline
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

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:



90
91
92
# File 'lib/landline/path.rb', line 90

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

#go(request) ⇒ Boolean

Try to navigate the path. Run method callback in response.

Parameters:

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/landline/path.rb', line 41

def go(request)
  # This is done to allow pipeline to interject handlers
  # I'm more than willing to admit that this is stupid,
  # but it is well worth the logical flexibility.
  if ['handle.default', 'handle.505'].any? do |x|
       @properties.storage.include? x
     end
    begin
      super(request)
    rescue StandardError => e
      _die(request, 500, backtrace: [e.to_s] + e.backtrace, error: e)
    end
  else
    super(request)
  end
end

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

Add a postprocessor to the path.

Parameters:

  • block (#call)

Yield Parameters:



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

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:



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

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.



62
63
64
65
66
67
68
# File 'lib/landline/path.rb', line 62

def process(request)
  if @pipeline
    @pipeline.call(request) { |inner_req| process_wrapped(inner_req) }
  else
    process_wrapped(request)
  end
end