Class: Rundoc::CodeCommand::WriteRunner

Inherits:
Object
  • Object
show all
Includes:
FileUtil
Defined in:
lib/rundoc/code_command/write.rb

Constant Summary collapse

NEWLINE =
Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileUtil

#filename, #mkdir_p

Constructor Details

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

Returns a new instance of WriteRunner.



42
43
44
45
46
47
# File 'lib/rundoc/code_command/write.rb', line 42

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

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



40
41
42
# File 'lib/rundoc/code_command/write.rb', line 40

def contents
  @contents
end

#ioObject (readonly)

Returns the value of attribute io.



40
41
42
# File 'lib/rundoc/code_command/write.rb', line 40

def io
  @io
end

Instance Method Details

#call(env = {}) ⇒ Object



64
65
66
67
68
69
# File 'lib/rundoc/code_command/write.rb', line 64

def call(env = {})
  io.puts "Writing to: '#{filename}'"
  mkdir_p
  File.write(filename, contents)
  contents
end

#render_command?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rundoc/code_command/write.rb', line 49

def render_command?
  @render_command
end

#to_md(env) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/rundoc/code_command/write.rb', line 53

def to_md(env)
  if render_command?
    if env[:commands].any? { |c| c[:visibility].not_hidden? }
      raise "must call write in its own code section"
    end
    env[:before] << "In file `#{filename}` write:"
    env[:before] << NEWLINE
  end
  nil
end