class Document

Public Class Methods

new(main_path, edit_path:nil, sync_path:nil) click to toggle source

Public

# File lib/document.rb, line 70
def initialize(main_path, edit_path:nil, sync_path:nil)
  @main_path = main_path
  @edit_path = edit_path
  @sync_path = sync_path
end

Public Instance Methods

edit() click to toggle source
# File lib/document.rb, line 76
def edit()

  # No edit path
  if !@edit_path
    return
  end

  # Check synced
  if @main_path != @edit_path
    main_contents = _load_document(@main_path)
    sync_contents = _load_document(@sync_path)
    if main_contents != sync_contents
      raise Exception.new("Document '#{@edit_path}' is out of sync")
    end
  end

  # Remote document
  if @edit_path.start_with?('http')
    Kernel.system('xdg-open', @edit_path)

  # Local document
  # TODO: supress commands output
  else
    Kernel.system('editor', @edit_path)
  end

end
sync() click to toggle source
# File lib/document.rb, line 104
def sync()

  # No sync path
  if !@sync_path
    return
  end

  # Save remote to local
  contents = Net::HTTP.get(URI(@sync_path))
  File.write(@main_path, contents)

end
test(sync:false, return_report:false, exit_first:false) click to toggle source
# File lib/document.rb, line 117
def test(sync:false, return_report:false, exit_first:false)

  # No test path
  path = sync ? @sync_path : @main_path
  if !path
    return true
  end

  # Test document
  contents = _load_document(path)
  elements = _parse_document(contents)
  report = _validate_document(elements, exit_first:exit_first)

  return return_report ? report : report['valid']
end