class MarkdownRubyDocumentation::WriteMarkdownToDisk

Attributes

dir[R]
relative_dir[R]
skip_if_blank[R]

Public Class Methods

new(dir:, skip_if_blank: false, relative_dir:) click to toggle source
# File lib/markdown_ruby_documentation/write_markdown_to_disk.rb, line 6
def initialize(dir:, skip_if_blank: false, relative_dir:)
  @dir           = dir
  @skip_if_blank = skip_if_blank
  @relative_dir  = relative_dir
end

Public Instance Methods

call(name:, text:) click to toggle source
# File lib/markdown_ruby_documentation/write_markdown_to_disk.rb, line 12
def call(name:, text:)
  return if skip_save?(text, name)
  name = name.gsub(dir, "").underscore
  file = "#{name}.md"
  path = File.join(dir, file)

  return if file_exists_with?(text, path)
  write_file(path, text)
end

Private Instance Methods

file_exists_with?(text, path) click to toggle source
# File lib/markdown_ruby_documentation/write_markdown_to_disk.rb, line 24
def file_exists_with?(text, path)
  File.exist?(path) && Digest::MD5.new.update(File.open(path).read) == Digest::MD5.new.update(text)
end
skip_save?(text, _name) click to toggle source
# File lib/markdown_ruby_documentation/write_markdown_to_disk.rb, line 33
def skip_save?(text, _name)
  if skip_if_blank
    return true if Array(text.split("\n")[2..-1]).all?(&:blank?)
  end
end
write_file(path, text) click to toggle source
# File lib/markdown_ruby_documentation/write_markdown_to_disk.rb, line 28
def write_file(path, text)
  FileUtils.mkdir_p(path.split("/").tap { |p| p.pop }.join("/"))
  File.open(path, "w").write(text)
end