Class: Landline::Node Abstract

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

Overview

This class is abstract.

Abstract class that reacts to request navigation. Does nothing by default, behaviour should be overriden through #reject and #process

Direct Known Subclasses

Path, Probe

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, parent:, filepath: true) ⇒ Node

Returns a new instance of Node.

Parameters:

  • path (Object)
  • filepath (Boolean) (defaults to: true)

    should Node modify request.filepath.



13
14
15
16
17
18
19
# File 'lib/landline/node.rb', line 13

def initialize(path, parent:, filepath: true)
  @pattern = Pattern.new(path).freeze
  @properties = Landline::Util::Lookup.new(parent&.properties)
  @root = nil
  @remap = false
  @modify_filepath = filepath
end

Instance Attribute Details

#remapObject

Returns the value of attribute remap.



71
72
73
# File 'lib/landline/node.rb', line 71

def remap
  @remap
end

#rootObject

Returns the value of attribute root.



71
72
73
# File 'lib/landline/node.rb', line 71

def root
  @root
end

Instance Method Details

#go(request) ⇒ Boolean

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

Parameters:

Returns:

  • (Boolean)


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

def go(request)
  # rejected at pattern
  return reject(request) unless @pattern.match?(request.path)

  request.push_state
  path, splat, param = @pattern.match(request.path)
  do_filepath(request, request.path.delete_suffix(path))
  request.path = path
  request.splat.append(*splat)
  request.param.merge!(param)
  value = process(request)
  # rejected at callback - restore state
  request.pop_state unless value
  # finally, return process value
  value
end

#process(_request) ⇒ Object

Method callback on successful request navigation

Parameters:

Returns:

  • true



67
68
69
# File 'lib/landline/node.rb', line 67

def process(_request)
  true
end

#reject(_request) ⇒ Object

Method callback on failed request navigation

Parameters:

Returns:

  • false



60
61
62
# File 'lib/landline/node.rb', line 60

def reject(_request)
  false
end