Class: Rundoc::CodeCommand::FileCommand::Remove

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

Constant Summary

Constants inherited from Rundoc::CodeCommand

NEWLINE

Instance Attribute Summary

Attributes inherited from Rundoc::CodeCommand

#command, #contents, #keyword, #original_args, #render_command, #render_result

Instance Method Summary collapse

Methods included from Rundoc::CodeCommand::FileUtil

#filename, #mkdir_p

Methods inherited from Rundoc::CodeCommand

#hidden?, #not_hidden?, #push

Constructor Details

#initialize(filename) ⇒ Remove

Returns a new instance of Remove.



5
6
7
# File 'lib/rundoc/code_command/file_command/remove.rb', line 5

def initialize(filename)
  @filename = filename
end

Instance Method Details

#call(env = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rundoc/code_command/file_command/remove.rb', line 16

def call(env = {})
  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



9
10
11
12
13
14
# File 'lib/rundoc/code_command/file_command/remove.rb', line 9

def to_md(env)
  raise "must call write in its own code section" unless env[:commands].empty?
  env[:before] << "In file `#{filename}` remove:"
  env[:before] << NEWLINE
  nil
end