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, RUNDOC_DEFAULT_ERB_BINDING, RUNDOC_ERB_BINDINGS

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



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rundoc/code_command/file_command/remove.rb', line 19

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
15
16
17
# File 'lib/rundoc/code_command/file_command/remove.rb', line 9

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

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