Skip to content

Commit

Permalink
Introduce renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
skryukov committed Sep 27, 2024
1 parent c50c366 commit ead5f79
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/typelizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
7 changes: 1 addition & 6 deletions lib/typelizer/interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
28 changes: 28 additions & 0 deletions lib/typelizer/renderer.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/typelizer/templates/inline_type.ts.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
<%- properties.each do |property| -%>
<%= property.to_s.strip.gsub(/^/, ' ') %>;
<%= indent(property) %>;
<%- end -%>
}
6 changes: 3 additions & 3 deletions lib/typelizer/templates/interface.ts.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%>
Expand Down
5 changes: 2 additions & 3 deletions lib/typelizer/writer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

require "fileutils"
require "erb"

module Typelizer
class Writer
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ead5f79

Please sign in to comment.