Class: JsRoutes::Route

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Types
Defined in:
lib/js_routes/route.rb

Overview

:nodoc:

Constant Summary collapse

FILTERED_DEFAULT_PARTS =
T.let([:controller, :action].freeze, SymbolArray)
URL_OPTIONS =
T.let([:protocol, :domain, :host, :port, :subdomain].freeze, SymbolArray)
NODE_TYPES =
T.let({
  GROUP: 1,
  CAT: 2,
  SYMBOL: 3,
  OR: 4,
  STAR: 5,
  LITERAL: 6,
  SLASH: 7,
  DOT: 8
}.freeze, T::Hash[Symbol, Integer])

Constants included from Types

Types::Application, Types::ApplicationCaller, Types::BannerCaller, Types::Clusivity, Types::ConfigurationBlock, Types::FileName, Types::JourneyRoute, Types::Literal, Types::Options, Types::Prefix, Types::RouteSpec, Types::SpecNode, Types::StringArray, Types::StringHash, Types::SymbolArray, Types::UntypedArray

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, route, parent_route = nil) ⇒ Route

Returns a new instance of Route.



34
35
36
37
38
# File 'lib/js_routes/route.rb', line 34

def initialize(configuration, route, parent_route = nil)
  @configuration = configuration
  @route = route
  @parent_route = parent_route
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



25
26
27
# File 'lib/js_routes/route.rb', line 25

def configuration
  @configuration
end

#parent_routeObject (readonly)

Returns the value of attribute parent_route.



31
32
33
# File 'lib/js_routes/route.rb', line 31

def parent_route
  @parent_route
end

#routeObject (readonly)

Returns the value of attribute route.



28
29
30
# File 'lib/js_routes/route.rb', line 28

def route
  @route
end

Instance Method Details

#body(absolute) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/js_routes/route.rb', line 54

def body(absolute)
  if @configuration.dts?
    definition_body
  else
    # For tree-shaking ESM, add a #__PURE__ comment informing Webpack/minifiers that the call to `__jsr.r`
    # has no side-effects (e.g. modifying global variables) and is safe to remove when unused.
    # https://webpack.js.org/guides/tree-shaking/#clarifying-tree-shaking-and-sidyeeffects
    pure_comment = @configuration.esm? ? '/*#__PURE__*/ ' : ''
    "#{pure_comment}__jsr.r(#{arguments(absolute).map{|a| json(a)}.join(', ')})"
  end
end

#definition_bodyObject



67
68
69
70
71
72
73
# File 'lib/js_routes/route.rb', line 67

def definition_body
  options_type = optional_parts_type ? "#{optional_parts_type} & RouteOptions" : "RouteOptions"
  predicate = @configuration.optional_definition_params ? '?' : ''
  args = required_parts.map{|p| "#{apply_case(p)}#{predicate}: RequiredRouteParameter"}
  args << "options?: #{options_type}"
  "((\n#{args.join(",\n").indent(2)}\n) => string) & RouteHelperExtras"
end

#helper_typesObject



48
49
50
51
# File 'lib/js_routes/route.rb', line 48

def helper_types
  return [] unless match_configuration?
  @configuration.url_links ? [true, false] : [false]
end

#helpersObject



41
42
43
44
45
# File 'lib/js_routes/route.rb', line 41

def helpers
  helper_types.map do |absolute|
    [ documentation, helper_name(absolute), body(absolute) ]
  end
end

#optional_parts_typeObject



76
77
78
79
80
81
82
# File 'lib/js_routes/route.rb', line 76

def optional_parts_type
  return nil if optional_parts.empty?
  @optional_parts_type ||= T.let(
    "{" + optional_parts.map {|p| "#{p}?: OptionalRouteParameter"}.join(', ') + "}",
    T.nilable(String)
  )
end