Class: JsRoutes::Instance

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

Overview

:nodoc:

Constant Summary

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(**options) ⇒ Instance

Returns a new instance of Instance.



19
20
21
22
# File 'lib/js_routes/instance.rb', line 19

def initialize(**options)
  options = T.let(options, Options)
  @configuration = T.let(JsRoutes.configuration.merge(options), JsRoutes::Configuration)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



13
14
15
# File 'lib/js_routes/instance.rb', line 13

def configuration
  @configuration
end

Instance Method Details



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/js_routes/instance.rb', line 56

def banner
  banner = @configuration.banner
  banner = banner.call if banner.is_a?(Proc)
  return "" if banner.blank?
  [
    "/**",
    *banner.split("\n").map { |line| " * #{line}" },
    " */",
    "",
  ].join("\n")
end

#generateObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/js_routes/instance.rb', line 25

def generate
  # Ensure routes are loaded. If they're not, load them.

  application = T.unsafe(self.application)
  if routes_from(application).empty?
    if application.is_a?(Rails::Application)
      if JsRoutes::Utils.rails_version >= Gem::Version.new("8.0.0")
        T.unsafe(application).reload_routes_unless_loaded
      else
        T.unsafe(application).reload_routes!
      end
    end
  end

  unless @configuration.module_type == "NIL"
    banner + jsr + routes_export
  else
    # Strip the empty IMPORT_ROUTER statement (comment + semicolon) left after substitution
    jsr.sub(/\A(\/\/[^\n]+\n)*;\n/, "")
  end

end

#generate!Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/js_routes/instance.rb', line 69

def generate!
  # Some libraries like Devise did not load their routes yet
  # so we will wait until initialization process finishes
  # https://github.com/railsware/js-routes/issues/7
  T.unsafe(Rails).configuration.after_initialize do
    file_path = Rails.root.join(@configuration.output_file)
    source_code = generate

    # We don't need to rewrite file if it already exist and have same content.
    # It helps asset pipeline or webpack understand that file wasn't changed.
    next if File.exist?(file_path) && File.read(file_path) == source_code

    File.open(file_path, 'w') do |f|
      f.write source_code
    end
  end
end

#packageObject



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

def package
  raise "Package generation requires module_type: 'PKG'" unless @configuration.pkg?

  jsr
end

#package!Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/js_routes/instance.rb', line 88

def package!
  raise "Package generation requires module_type: 'PKG'" unless @configuration.pkg?

  file_path = Rails.root.join(@configuration.output_file)
  source_code = package

  # We don't need to rewrite file if it already exist and have same content.
  # It helps asset pipeline or webpack understand that file wasn't changed.
  return if File.exist?(file_path) && File.read(file_path) == source_code

  File.open(file_path, 'w') do |f|
    f.write source_code
  end
end

#remove!Object



104
105
106
107
108
# File 'lib/js_routes/instance.rb', line 104

def remove!
  path = Rails.root.join(@configuration.output_file)
  FileUtils.rm_rf(path)
  FileUtils.rm_rf(path.sub(%r{\.js\z}, '.d.ts'))
end