Class: Rundoc::CodeCommand::FileCommand::RemoveRunner

Inherits:
Object
  • Object
show all
Includes:
Rundoc::CodeCommand::FileUtil
Defined in:
lib/rundoc/code_command/file_command/remove.rb

Constant Summary collapse

NEWLINE =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Rundoc::CodeCommand::FileUtil

#filename, #mkdir_p

Constructor Details

#initialize(user_args:, render_command:, render_result:, io:, contents: nil) ⇒ RemoveRunner

Returns a new instance of RemoveRunner.



25
26
27
28
29
# File 'lib/rundoc/code_command/file_command/remove.rb', line 25

def initialize(user_args:, render_command:, render_result:, io:, contents: nil)
  @filename = user_args.filename
  @io = io
  @contents = contents.dup if contents && !contents.empty?
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



23
24
25
# File 'lib/rundoc/code_command/file_command/remove.rb', line 23

def contents
  @contents
end

#ioObject (readonly)

Returns the value of attribute io.



23
24
25
# File 'lib/rundoc/code_command/file_command/remove.rb', line 23

def io
  @io
end

Instance Method Details

#call(env = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rundoc/code_command/file_command/remove.rb', line 41

def call(env = {})
  io.puts "Deleting '#{contents.strip}' from #{filename}"
  raise "#{filename} does not exist" unless File.exist?(filename)

  regex = /^\s*#{Regexp.quote(contents)}/
  doc = File.read(filename)
  doc.sub!(regex, "")

  File.write(filename, doc)
  contents
end

#to_md(env) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rundoc/code_command/file_command/remove.rb', line 31

def to_md(env)
  if env[:commands].any? { |c| c[:visibility].not_hidden? }
    raise "Must call remove in its own code section"
  end

  env[:before] << "In file `#{filename}` remove:"
  env[:before] << NEWLINE
  nil
end