Class: Transducer::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/transducer/generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_data, template_path: nil) ⇒ Generator

Returns a new instance of Generator.



10
11
12
13
14
# File 'lib/transducer/generator.rb', line 10

def initialize(parsed_data, template_path: nil)
  @data = parsed_data
  @formatter = Formatter.new
  @template_path = template_path
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/transducer/generator.rb', line 8

def data
  @data
end

#formatterObject (readonly)

Returns the value of attribute formatter.



8
9
10
# File 'lib/transducer/generator.rb', line 8

def formatter
  @formatter
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



8
9
10
# File 'lib/transducer/generator.rb', line 8

def template_path
  @template_path
end

Instance Method Details

#generateObject



16
17
18
19
20
21
22
# File 'lib/transducer/generator.rb', line 16

def generate
  if @template_path
    render_template
  else
    generate_default
  end
end

#to_file(output_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/transducer/generator.rb', line 24

def to_file(output_path)
  require 'fileutils'
  FileUtils.mkdir_p(File.dirname(output_path))

  File.write(output_path, generate)
rescue Errno::EACCES
  raise FileError, "Permission denied: #{output_path}"
rescue StandardError => e
  raise FileError, "Failed to write file: #{e.message}"
end