Class: RubyUIConverter::Template

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(path:, root:, config:) ⇒ Template

Returns a new instance of Template.



8
9
10
11
12
# File 'lib/ruby_ui_converter/template.rb', line 8

def initialize(path:, root:, config:)
  @path = path
  @root = root.to_s
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/ruby_ui_converter/template.rb', line 6

def config
  @config
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/ruby_ui_converter/template.rb', line 6

def path
  @path
end

#rootObject (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

#basenameObject

“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_nameObject



42
43
44
# File 'lib/ruby_ui_converter/template.rb', line 42

def class_name
  Naming.class_name(basename)
end

#dir_relObject



37
38
39
40
# File 'lib/ruby_ui_converter/template.rb', line 37

def dir_rel
  dir = File.dirname(rel_path)
  dir == "." ? "" : dir
end

#filenameObject



18
19
20
# File 'lib/ruby_ui_converter/template.rb', line 18

def filename
  File.basename(path)
end

#full_constObject



50
51
52
# File 'lib/ruby_ui_converter/template.rb', line 50

def full_const
  (namespace_parts + [class_name]).join("::")
end

#init_argsObject

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

#ivarsObject

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

#localsObject



73
74
75
# File 'lib/ruby_ui_converter/template.rb', line 73

def locals
  @locals ||= partial? ? LocalsDetector.new(tree).locals : []
end

#namespace_partsObject



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_filenameObject



54
55
56
# File 'lib/ruby_ui_converter/template.rb', line 54

def output_filename
  "#{basename.sub(/\A_/, "")}.rb"
end

#output_pathObject



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.expand_path(path))
    end

  File.join(target_dir, output_filename)
end

#partial?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ruby_ui_converter/template.rb', line 27

def partial?
  filename.start_with?("_")
end

#rel_pathObject



31
32
33
34
35
# File 'lib/ruby_ui_converter/template.rb', line 31

def rel_path
  expanded = File.expand_path(path)
  base = File.expand_path(root)
  expanded.delete_prefix(base).sub(%r{\A/}, "")
end

#renderObject



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

#sourceObject



14
15
16
# File 'lib/ruby_ui_converter/template.rb', line 14

def source
  @source ||= File.read(path)
end

#treeObject



69
70
71
# File 'lib/ruby_ui_converter/template.rb', line 69

def tree
  @tree ||= Parser.new(source).parse
end