class Swgr2rb::RubyFileGenerator

RubyFileGenerator is meant to be used as an abstract class to be inherited from. It contains methods that create a Ruby file and write to it the return value of generate_lines method. The method is implemented in its child classes, EndpointClassGenerator and SchemaModuleGenerator.

Public Class Methods

new(class_config, opts) click to toggle source

opts can include:

name: class/module name
rewrite: if set to true, rewrite the existing file if it already exists
target_dir: directory where the class/module will be created
update_only: if set to true, do not create new file,
             only update existing
# File lib/endpoint_class_generator/ruby_file_generator.rb, line 21
def initialize(class_config, opts)
  @config = class_config
  @opts = opts
end

Public Instance Methods

generate_file() click to toggle source
# File lib/endpoint_class_generator/ruby_file_generator.rb, line 26
def generate_file
  if (File.exist?(filename) && !@opts[:rewrite]) ||
     (!File.exist?(filename) && @opts[:update_only])
    return
  end

  File.open(filename, 'w') do |file|
    file.write(generate_lines.join("\n"))
  end
end

Private Instance Methods

create_target_dir(dir_str) click to toggle source
# File lib/endpoint_class_generator/ruby_file_generator.rb, line 48
def create_target_dir(dir_str)
  FileUtils.mkdir_p(dir_str) unless Dir.exist?(dir_str)
end
filename() click to toggle source
# File lib/endpoint_class_generator/ruby_file_generator.rb, line 39
def filename
  unless @filename
    create_target_dir(@opts[:target_dir])
    @filename = File.join(@opts[:target_dir],
                          "#{RubyFileGeneratorConstants::CAMEL_CASE_TO_SNAKE_CASE.call(@opts[:name])}.rb")
  end
  @filename
end