Class: JsRoutes::Instance
- Inherits:
-
Object
- Object
- JsRoutes::Instance
- 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
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
Instance Method Summary collapse
- #banner ⇒ Object
- #generate ⇒ Object
- #generate! ⇒ Object
-
#initialize(**options) ⇒ Instance
constructor
A new instance of Instance.
- #remove! ⇒ Object
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(**) = T.let(, Options) @configuration = T.let(JsRoutes.configuration.merge(), JsRoutes::Configuration) end |
Instance Attribute Details
#configuration ⇒ Object (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
#banner ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/js_routes/instance.rb', line 50 def = @configuration. = .call if .is_a?(Proc) return "" if .blank? [ "/**", *.split("\n").map { |line| " * #{line}" }, " */", "", ].join("\n") end |
#generate ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# 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 Rails.version >= "8.0.0" T.unsafe(application).reload_routes_unless_loaded else T.unsafe(application).reload_routes! end end end content = File.read(@configuration.source_file) unless @configuration.dts? content = js_variables.inject(content) do |js, (key, value)| js.gsub!("RubyVariables.#{key}", value.to_s) || raise("Missing key #{key} in JS template") end end + content + routes_export + prevent_types_export end |
#generate! ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/js_routes/instance.rb', line 63 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 |
#remove! ⇒ Object
82 83 84 85 86 |
# File 'lib/js_routes/instance.rb', line 82 def remove! path = Rails.root.join(@configuration.output_file) FileUtils.rm_rf(path) FileUtils.rm_rf(path.sub(%r{\.js\z}, '.d.ts')) end |