Class: Zapata::Core::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/zapata/core/writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Writer

Returns a new instance of Writer.



6
7
8
9
10
# File 'lib/zapata/core/writer.rb', line 6

def initialize(filename)
  @filename = filename
  @padding = 0
  clean
end

Instance Method Details

#append_line(line = '') ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/zapata/core/writer.rb', line 18

def append_line(line = '')
  @padding -= 1 if word_exists?(line, 'end')

  padding_to_use = @padding
  padding_to_use = 0 if line.empty?
  file = File.open(@filename, 'ab+')
  file.puts("#{'  ' * padding_to_use}#{line}")
  file.close

  @padding += 1 if word_exists?(line, 'do')
end

#cleanObject



12
13
14
15
16
# File 'lib/zapata/core/writer.rb', line 12

def clean
  file = File.open(@filename, 'w')
  file.write('')
  file.close
end

#word_exists?(string, word) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/zapata/core/writer.rb', line 30

def word_exists?(string, word)
  !!/\b(?:#{word})\b/.match(string)
end