From ead5f79634370fa8c900720c2c2a1ac5fc79b770 Mon Sep 17 00:00:00 2001 From: Svyatoslav Kryukov Date: Fri, 27 Sep 2024 14:46:02 +0300 Subject: [PATCH] Introduce renderer --- lib/typelizer.rb | 1 + lib/typelizer/interface.rb | 7 +----- lib/typelizer/renderer.rb | 28 ++++++++++++++++++++++ lib/typelizer/templates/inline_type.ts.erb | 2 +- lib/typelizer/templates/interface.ts.erb | 6 ++--- lib/typelizer/writer.rb | 5 ++-- 6 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 lib/typelizer/renderer.rb diff --git a/lib/typelizer.rb b/lib/typelizer.rb index e91d3a7..f43cd9b 100644 --- a/lib/typelizer.rb +++ b/lib/typelizer.rb @@ -4,6 +4,7 @@ require_relative "typelizer/config" require_relative "typelizer/property" require_relative "typelizer/interface" +require_relative "typelizer/renderer" require_relative "typelizer/writer" require_relative "typelizer/generator" diff --git a/lib/typelizer/interface.rb b/lib/typelizer/interface.rb index e67ec3c..9abcfbd 100644 --- a/lib/typelizer/interface.rb +++ b/lib/typelizer/interface.rb @@ -17,7 +17,7 @@ def inline? def name if inline? - render_template("inline_type.ts.erb", properties: properties).strip + Renderer.new("inline_type.ts.erb").call(properties: properties).strip else config.serializer_name_mapper.call(serializer).tr_s(":", "") end @@ -105,10 +105,5 @@ def model_class def model_plugin @model_plugin ||= config.model_plugin.new(model_class: model_class, config: config) end - - def render_template(template, **context) - ERB.new(File.read(File.join(File.dirname(__FILE__), "templates/#{template}")), trim_mode: "-") - .result_with_hash(context) - end end end diff --git a/lib/typelizer/renderer.rb b/lib/typelizer/renderer.rb new file mode 100644 index 0000000..511bb05 --- /dev/null +++ b/lib/typelizer/renderer.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +require "erb" + +module Typelizer + class Renderer + def initialize(template) + @erb = ERB.new(File.read(File.join(File.dirname(__FILE__), "templates/#{template}")), trim_mode: "-") + end + + def call(**context) + b = binding + context.each_pair do |key, value| + b.local_variable_set(key, value) + end + erb.result(b) + end + + private + + attr_reader :erb + + def indent(content, multiplier = 2) + spaces = " " * multiplier + content.to_s.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join + end + end +end diff --git a/lib/typelizer/templates/inline_type.ts.erb b/lib/typelizer/templates/inline_type.ts.erb index 3930f05..a6fbc8e 100644 --- a/lib/typelizer/templates/inline_type.ts.erb +++ b/lib/typelizer/templates/inline_type.ts.erb @@ -1,5 +1,5 @@ { <%- properties.each do |property| -%> -<%= property.to_s.strip.gsub(/^/, ' ') %>; +<%= indent(property) %>; <%- end -%> } diff --git a/lib/typelizer/templates/interface.ts.erb b/lib/typelizer/templates/interface.ts.erb index 705fc61..1daee04 100644 --- a/lib/typelizer/templates/interface.ts.erb +++ b/lib/typelizer/templates/interface.ts.erb @@ -5,20 +5,20 @@ import type {<%= interface.imports.join(", ") %>} from '<%= interface.config.typ <%- if interface.root_key -%> type <%= interface.name %>Data = { <%- interface.properties.each do |property| -%> -<%= property.to_s.strip.gsub(/^/, ' ') %>; +<%= indent(property) %>; <%- end -%> } type <%= interface.name %> = { <%= interface.root_key %>: <%= interface.name %>Data; <%- interface.meta_fields&.each do |property| -%> -<%= property.to_s.strip.gsub(/^/, ' ') %>; +<%= indent(property) %>; <%- end -%> } <%- else -%> type <%= interface.name %> = { <%- interface.properties.each do |property| -%> -<%= property.to_s.strip.gsub(/^/, ' ') %>; +<%= indent(property) %>; <%- end -%> } <%- end -%> diff --git a/lib/typelizer/writer.rb b/lib/typelizer/writer.rb index 3fa43e5..b1b4ff8 100644 --- a/lib/typelizer/writer.rb +++ b/lib/typelizer/writer.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true require "fileutils" -require "erb" module Typelizer class Writer @@ -54,8 +53,8 @@ def write_file(filename, fingerprint) end def render_template(template, **context) - template_cache[template] ||= ERB.new(File.read(File.join(File.dirname(__FILE__), "templates/#{template}")), trim_mode: "-") - template_cache[template].result_with_hash(context) + template_cache[template] ||= Renderer.new(template) + template_cache[template].call(**context) end def cleanup_output_dir