Class: Document

Inherits:
Object
  • Object
show all
Defined in:
lib/document.rb

Instance Method Summary collapse

Constructor Details

#initialize(main_path, edit_path: nil, sync_path: nil) ⇒ Document

Public



70
71
72
73
74
# 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

Instance Method Details

#editObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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

#syncObject



104
105
106
107
108
109
110
111
112
113
114
115
# 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) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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