Class: Rundoc::CodeCommand::FileCommand::AppendRunner

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

Constant Summary collapse

NEWLINE =
Rundoc::CodeCommand::WriteRunner::NEWLINE

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) ⇒ AppendRunner

Returns a new instance of AppendRunner.



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

def initialize(user_args:, render_command:, render_result:, io:, contents: nil)
  @filename, line = user_args.filename.split("#")
  @line_number = if line
    Integer(line)
  end
  @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.



17
18
19
# File 'lib/rundoc/code_command/file_command/append.rb', line 17

def contents
  @contents
end

#ioObject (readonly)

Returns the value of attribute io.



17
18
19
# File 'lib/rundoc/code_command/file_command/append.rb', line 17

def io
  @io
end

Instance Method Details

#call(env = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rundoc/code_command/file_command/append.rb', line 81

def call(env = {})
  mkdir_p
  doc = File.read(filename)
  if @line_number
    io.puts "Writing to: '#{filename}' line #{@line_number} with: #{contents.inspect}"
    doc = insert_contents_into_at_line(doc)
  else
    io.puts "Appending to file: '#{filename}' with: #{contents.inspect}"
    doc = concat_with_newline(doc, contents)
  end

  File.write(filename, doc)
  contents
end

#concat_with_newline(str1, str2) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/rundoc/code_command/file_command/append.rb', line 57

def concat_with_newline(str1, str2)
  result = +""
  result << str1
  result << "\n" unless ends_in_newline?(result)
  result << str2
  result << "\n" unless ends_in_newline?(result)
  result
end

#ends_in_newline?(string) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rundoc/code_command/file_command/append.rb', line 53

def ends_in_newline?(string)
  last_char_of(string) == "\n"
end

#insert_contents_into_at_line(doc) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rundoc/code_command/file_command/append.rb', line 66

def insert_contents_into_at_line(doc)
  lines = doc.lines
  raise "Expected #{filename} to have at least #{@line_number} but only has #{lines.count}" if lines.count < @line_number
  result = []
  lines.each_with_index do |line, index|
    line_number = index.next
    if line_number == @line_number
      result << contents
      result << "\n" unless ends_in_newline?(contents)
    end
    result << line
  end
  result.flatten.join("")
end

#last_char_of(string) ⇒ Object



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

def last_char_of(string)
  string[-1, 1]
end

#render_command?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rundoc/code_command/file_command/append.rb', line 29

def render_command?
  @render_command
end

#to_md(env) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rundoc/code_command/file_command/append.rb', line 33

def to_md(env)
  return unless render_command?

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

  env[:before] << if @line_number
    "In file `#{filename}`, on line #{@line_number} add:"
  else
    "At the end of `#{filename}` add:"
  end
  env[:before] << NEWLINE
  nil
end