Class: RubyUIConverter::Template
- Inherits:
-
Object
- Object
- RubyUIConverter::Template
- Defined in:
- lib/ruby_ui_converter/template.rb
Overview
Represents a single .erb file and knows how to render its .rb equivalent.
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#basename ⇒ Object
“index.html.erb” -> “index”; “_form.html.erb” -> “_form”.
- #class_name ⇒ Object
- #dir_rel ⇒ Object
- #filename ⇒ Object
- #full_const ⇒ Object
-
#init_args ⇒ Object
Keyword arguments the generated initializer/props expose: bare locals for partials (referenced through attr_readers), controller ivars for top-level views (referenced directly as @ivars).
-
#initialize(path:, root:, config:) ⇒ Template
constructor
A new instance of Template.
-
#ivars ⇒ Object
Top-level views read controller instance variables (‘@products`); partials take their data as bare locals instead.
- #locals ⇒ Object
- #namespace_parts ⇒ Object
- #output_filename ⇒ Object
- #output_path ⇒ Object
- #partial? ⇒ Boolean
- #rel_path ⇒ Object
- #render ⇒ Object
- #source ⇒ Object
- #tree ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/ruby_ui_converter/template.rb', line 6 def config @config end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/ruby_ui_converter/template.rb', line 6 def path @path end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
6 7 8 |
# File 'lib/ruby_ui_converter/template.rb', line 6 def root @root end |
Instance Method Details
#basename ⇒ Object
“index.html.erb” -> “index”; “_form.html.erb” -> “_form”
23 24 25 |
# File 'lib/ruby_ui_converter/template.rb', line 23 def basename filename.split(".").first end |
#class_name ⇒ Object
42 43 44 |
# File 'lib/ruby_ui_converter/template.rb', line 42 def class_name Naming.class_name(basename) end |
#dir_rel ⇒ Object
37 38 39 40 |
# File 'lib/ruby_ui_converter/template.rb', line 37 def dir_rel dir = File.dirname(rel_path) dir == "." ? "" : dir end |
#filename ⇒ Object
18 19 20 |
# File 'lib/ruby_ui_converter/template.rb', line 18 def filename File.basename(path) end |
#full_const ⇒ Object
50 51 52 |
# File 'lib/ruby_ui_converter/template.rb', line 50 def full_const (namespace_parts + [class_name]).join("::") end |
#init_args ⇒ Object
Keyword arguments the generated initializer/props expose: bare locals for partials (referenced through attr_readers), controller ivars for top-level views (referenced directly as @ivars).
86 87 88 |
# File 'lib/ruby_ui_converter/template.rb', line 86 def init_args partial? ? locals : ivars end |
#ivars ⇒ Object
Top-level views read controller instance variables (‘@products`); partials take their data as bare locals instead.
79 80 81 |
# File 'lib/ruby_ui_converter/template.rb', line 79 def ivars @ivars ||= partial? ? [] : LocalsDetector.new(tree).ivars end |
#locals ⇒ Object
73 74 75 |
# File 'lib/ruby_ui_converter/template.rb', line 73 def locals @locals ||= partial? ? LocalsDetector.new(tree).locals : [] end |
#namespace_parts ⇒ Object
46 47 48 |
# File 'lib/ruby_ui_converter/template.rb', line 46 def namespace_parts Naming.namespace_parts(dir_rel, config.base_namespace) end |
#output_filename ⇒ Object
54 55 56 |
# File 'lib/ruby_ui_converter/template.rb', line 54 def output_filename "#{basename.sub(/\A_/, "")}.rb" end |
#output_path ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby_ui_converter/template.rb', line 58 def output_path target_dir = if config.output_root File.join(config.output_root, dir_rel) else File.dirname(File.(path)) end File.join(target_dir, output_filename) end |
#partial? ⇒ Boolean
27 28 29 |
# File 'lib/ruby_ui_converter/template.rb', line 27 def partial? filename.start_with?("_") end |
#rel_path ⇒ Object
31 32 33 34 35 |
# File 'lib/ruby_ui_converter/template.rb', line 31 def rel_path = File.(path) base = File.(root) .delete_prefix(base).sub(%r{\A/}, "") end |
#render ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/ruby_ui_converter/template.rb', line 90 def render builder = CodeBuilder.new(indent: config.indent) builder.line("# frozen_string_literal: true") builder.line builder.line("# Auto-generated by ruby_ui_converter") builder.line("# Source: #{rel_path}") builder.line namespace_parts.each do |segment| builder.line("module #{segment}") builder.indent end builder.line("class #{class_name} < #{config.base_class}") builder.indent emit_initializer(builder) builder.line("def #{config.template_method}") builder.indent Transformer.new(config: config, template: self).emit(tree, builder) builder.dedent builder.line("end") emit_readers(builder) builder.dedent builder.line("end") namespace_parts.each do builder.dedent builder.line("end") end builder.to_s end |
#source ⇒ Object
14 15 16 |
# File 'lib/ruby_ui_converter/template.rb', line 14 def source @source ||= File.read(path) end |