class SmlFile

Attributes

contents[R]
path[R]

Public Class Methods

new(path, options={}) click to toggle source
# File lib/sml_file.rb, line 11
def initialize(path, options={})
  @path = path
  @contents = options[:content] || File.read(@path)
  @formatters = options[:formatters] || []

  unless @formatters.all? { |f| f.respond_to?(:format) }
    raise InvalidFormatterInterface
  end
end

Public Instance Methods

compile!(destination) click to toggle source
# File lib/sml_file.rb, line 35
def compile!(destination)
  raise NotSaved unless @path

  @exe_path = destination

  output = `mosmlc #{@path} -o #{@exe_path}`

  unless output.empty?
    raise CannotCompile, output
  end
end
delete!() click to toggle source
# File lib/sml_file.rb, line 55
def delete!
  FileUtils.rm_f(@path)
  @path = nil
end
prepare_tests() click to toggle source
# File lib/sml_file.rb, line 27
def prepare_tests
  formatted_content = @formatters.inject(@contents) do |acc, formatter|
    formatter.format(acc)
  end

  self.class.new(nil, content: formatted_content, formatters: @formatters)
end
run() click to toggle source
# File lib/sml_file.rb, line 47
def run
  if @exe_path
    `./#{@exe_path}`
  else
    raise NotCompiled
  end
end
save_as!(new_path) click to toggle source
# File lib/sml_file.rb, line 21
def save_as!(new_path)
  if File.write(new_path, @contents)
    @path = new_path
  end
end