Class: SmlFile

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

Defined Under Namespace

Classes: CannotCompile, InvalidFormatterInterface, NotCompiled, NotSaved

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ SmlFile

Returns a new instance of SmlFile.



11
12
13
14
15
16
17
18
19
# 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

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



9
10
11
# File 'lib/sml_file.rb', line 9

def contents
  @contents
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/sml_file.rb', line 9

def path
  @path
end

Instance Method Details

#compile!(destination) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
# 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!Object



55
56
57
58
# File 'lib/sml_file.rb', line 55

def delete!
  FileUtils.rm_f(@path)
  @path = nil
end

#prepare_testsObject



27
28
29
30
31
32
33
# 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

#runObject



47
48
49
50
51
52
53
# File 'lib/sml_file.rb', line 47

def run
  if @exe_path
    `./#{@exe_path}`
  else
    raise NotCompiled
  end
end

#save_as!(new_path) ⇒ Object



21
22
23
24
25
# File 'lib/sml_file.rb', line 21

def save_as!(new_path)
  if File.write(new_path, @contents)
    @path = new_path
  end
end