Method: Impression::Resource#initialize

Defined in:
lib/impression/resource.rb

#initialize(parent: nil, path:, &block) ⇒ void

Initalizes a new resource instance. If a block is given, it is used as the request handler instead of the default one, which returns ‘404 NOT FOUND`.

Parameters:

  • parent (Impression::Resource, nil) (defaults to: nil)

    the parent resource (or nil)

  • path (String)

    the resource’s relative path

  • &block (Proc)

    default request handler



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/impression/resource.rb', line 36

def initialize(parent: nil, path:, &block)
  @parent = parent
  @path = normalize_route_path(path)
  @route_regexp = @path == '/' ? nil : /^#{@path}(\/.*)?$/.freeze
  @children = {}

  @parent&.add_child(self)

  if block
    singleton_class.define_method(:call, &block)
  end
end