Class: ZodRails::Generation::FileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/zod_rails/generation/file_writer.rb

Constant Summary collapse

IMPORTS_BLOCK_RE =
%r{^// ZOD_RAILS:CUSTOM:IMPORTS:BEGIN\n.*?^// ZOD_RAILS:CUSTOM:IMPORTS:END\n}m
TAIL_BLOCK_RE =
%r{^// ZOD_RAILS:CUSTOM:BEGIN\n.*?^// ZOD_RAILS:CUSTOM:END\n}m
ZOD_IMPORT_RE =
/^(import \{ z \} from "zod";\n)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_dir:) ⇒ FileWriter

Returns a new instance of FileWriter.



14
15
16
# File 'lib/zod_rails/generation/file_writer.rb', line 14

def initialize(output_dir:)
  @output_dir = output_dir
end

Instance Attribute Details

#output_dirObject (readonly)

Returns the value of attribute output_dir.



12
13
14
# File 'lib/zod_rails/generation/file_writer.rb', line 12

def output_dir
  @output_dir
end

Instance Method Details

#output_path_for(model_name) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/zod_rails/generation/file_writer.rb', line 30

def output_path_for(model_name)
  parts = model_name.split("::")
  filename = underscore(parts.pop)
  path_parts = parts.map { |p| underscore(p) }
  path_parts << "#{filename}.ts"
  path_parts.join("/")
end

#preview(filename:, content:) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



26
27
28
# File 'lib/zod_rails/generation/file_writer.rb', line 26

def preview(filename:, content:) # rubocop:disable Lint/UnusedMethodArgument
  content
end

#write(filename:, content:) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/zod_rails/generation/file_writer.rb', line 18

def write(filename:, content:)
  full_path = File.join(output_dir, filename)
  FileUtils.mkdir_p(File.dirname(full_path))

  final = File.exist?(full_path) ? splice_custom_blocks(content, File.read(full_path)) : content
  File.write(full_path, final)
end