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)
JS_RESERVED_WORDS =

JavaScript reserved words that are invalid as function parameter names.

T.let(%w[
  abstract arguments async await boolean break byte case
  catch char class const continue debugger default delete
  do double else enum eval export extends false
  final finally float for function goto if implements
  import in instanceof int interface let long native
  new null package private protected public return short
  static super switch synchronized this throw throws transient
  true try typeof using var void volatile while
  with yield
].freeze, T::Array[String])
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.



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

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.



40
41
42
# File 'lib/js_routes/route.rb', line 40

def configuration
  @configuration
end

#parent_routeObject (readonly)

Returns the value of attribute parent_route.



46
47
48
# File 'lib/js_routes/route.rb', line 46

def parent_route
  @parent_route
end

#routeObject (readonly)

Returns the value of attribute route.



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

def route
  @route
end

Instance Method Details

#body(absolute) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/js_routes/route.rb', line 69

def body(absolute)
  if @configuration.dts?
    definition_body
  else
    # For tree-shaking ESM, add a #__PURE__ comment informing js bundlers that the call 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__*/ ' : ''
    call = '__route'
    "#{pure_comment}#{call}(#{arguments(absolute).map{|a| json(a)}.join(', ')})"
  end
end

#definition_bodyObject



83
84
85
86
87
88
89
# File 'lib/js_routes/route.rb', line 83

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

#helper_typesObject



63
64
65
66
# File 'lib/js_routes/route.rb', line 63

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

#helpersObject



56
57
58
59
60
# File 'lib/js_routes/route.rb', line 56

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

#optional_parts_typeObject



92
93
94
95
96
97
98
# File 'lib/js_routes/route.rb', line 92

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