Class: Enroute::Export
- Inherits:
-
Object
- Object
- Enroute::Export
- Defined in:
- lib/enroute/export.rb
Instance Attribute Summary collapse
-
#config_path ⇒ Object
readonly
Returns the value of attribute config_path.
-
#output_path ⇒ Object
readonly
Returns the value of attribute output_path.
Class Method Summary collapse
Instance Method Summary collapse
- #build_ts_args_definition(route) ⇒ Object
- #build_ts_handler_function(route) ⇒ Object
- #build_ts_route_function(route) ⇒ Object
- #call ⇒ Object
- #config ⇒ Object
- #handler_functions ⇒ Object
-
#initialize(output_path, config_path) ⇒ Export
constructor
A new instance of Export.
- #render_template ⇒ Object
- #route_functions ⇒ Object
- #routes ⇒ Object
- #write_template(output_path) ⇒ Object
Constructor Details
#initialize(output_path, config_path) ⇒ Export
Returns a new instance of Export.
11 12 13 14 |
# File 'lib/enroute/export.rb', line 11 def initialize(output_path, config_path) @output_path = output_path @config_path = config_path end |
Instance Attribute Details
#config_path ⇒ Object (readonly)
Returns the value of attribute config_path.
5 6 7 |
# File 'lib/enroute/export.rb', line 5 def config_path @config_path end |
#output_path ⇒ Object (readonly)
Returns the value of attribute output_path.
5 6 7 |
# File 'lib/enroute/export.rb', line 5 def output_path @output_path end |
Class Method Details
.call(output_path, config_path) ⇒ Object
7 8 9 |
# File 'lib/enroute/export.rb', line 7 def self.call(output_path, config_path) new(output_path, config_path).call end |
Instance Method Details
#build_ts_args_definition(route) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/enroute/export.rb', line 56 def build_ts_args_definition(route) args = route[:segments].each_with_object([]) do |segment, buffer| type = route.dig(:typings, segment)&.chomp || config.dig(:typings, :_default, segment)&.chomp || "any" optional = route[:requiredSegments].include?(segment) ? "" : "?" buffer << "#{segment.camelize(:lower)}#{optional}: #{type}" end args << "params?: Record<string, unknown>" "args?: {#{args.join('; ')}}" # (args + ["params: Record<string, unknown> = {}"]).join(", ") end |
#build_ts_handler_function(route) ⇒ Object
74 75 76 77 |
# File 'lib/enroute/export.rb', line 74 def build_ts_handler_function(route) args = JSON.pretty_generate(route.except(:typings)) %[const #{route[:name]}Handler = buildRoute(#{args});] end |
#build_ts_route_function(route) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/enroute/export.rb', line 79 def build_ts_route_function(route) args = build_ts_args_definition(route) segments = route[:segments].map {|segment| segment.camelize(:lower) } destruct = [segments.join(", "), "params"].reject(&:blank?).join(", ") <<~TYPESCRIPT export function #{route[:name]}Url(#{args}): string { const {#{destruct}} = args ?? {}; return buildUrl(#{route[:name]}Handler(#{segments.join(', ')}), params ?? {}).url; } export function #{route[:name]}Path(#{args}): string { const {#{destruct}} = args ?? {}; return buildUrl(#{route[:name]}Handler(#{segments.join(', ')}), params ?? {}).path; } TYPESCRIPT end |
#call ⇒ Object
26 27 28 29 30 |
# File 'lib/enroute/export.rb', line 26 def call FileUtils.mkdir_p(File.dirname(output_path)) write_template(output_path) end |
#config ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/enroute/export.rb', line 16 def config @config ||= if File.file?(config_path) ActiveSupport::HashWithIndifferentAccess.new( YAML.load_file(config_path) ) else {} end end |
#handler_functions ⇒ Object
48 49 50 |
# File 'lib/enroute/export.rb', line 48 def handler_functions routes.map {|route| build_ts_handler_function(route) }.join("\n\n") end |
#render_template ⇒ Object
52 53 54 |
# File 'lib/enroute/export.rb', line 52 def render_template ERB.new(File.read("#{__dir__}/template.ts.erb")).result binding end |
#route_functions ⇒ Object
42 43 44 45 46 |
# File 'lib/enroute/export.rb', line 42 def route_functions routes .map {|route| build_ts_route_function(route) } .join("\n\n") end |
#routes ⇒ Object
32 33 34 |
# File 'lib/enroute/export.rb', line 32 def routes @routes ||= Routes.call(config) end |
#write_template(output_path) ⇒ Object
36 37 38 39 40 |
# File 'lib/enroute/export.rb', line 36 def write_template(output_path) File.open(output_path, "w+") do |file| file << render_template end end |