Class: Workspace

Inherits:
Object
  • Object
show all
Includes:
Modules::ResourceUtils
Defined in:
lib/notes_cli/models/workspace.rb

Instance Method Summary collapse

Methods included from Modules::ResourceUtils

#config, #create?, #current_workspace, #notebook_exists?, #notebook_path, #notes_folder, #workspace_exists?, #workspace_path

Constructor Details

#initializeWorkspace

Returns a new instance of Workspace.



6
7
8
# File 'lib/notes_cli/models/workspace.rb', line 6

def initialize
  FileUtils.touch(NotesCli::CONFIG_PATH) unless File.file?(NotesCli::CONFIG_PATH)
end

Instance Method Details

#create_note(notebook, title) ⇒ Object



10
11
12
# File 'lib/notes_cli/models/workspace.rb', line 10

def create_note(notebook, title)
  NoteCreator.new(notebook, title, notebook_path(notebook), workspace_path).call
end

#delete_note(notebook, title) ⇒ Object



14
15
16
# File 'lib/notes_cli/models/workspace.rb', line 14

def delete_note(notebook, title)
  NoteDeleter.new(notebook, title, notebook_path(notebook), workspace_path).call
end

#list_notes(notebook) ⇒ Object



18
19
20
# File 'lib/notes_cli/models/workspace.rb', line 18

def list_notes(notebook)
  NoteLister.new(notebook).call
end

#switch(workspace) ⇒ Object



22
23
24
25
26
# File 'lib/notes_cli/models/workspace.rb', line 22

def switch(workspace)
  return unless workspace_exists?(workspace) || create?('workspace')

  update_entry('workspace', workspace)
end

#update_entry(key, value) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/notes_cli/models/workspace.rb', line 28

def update_entry(key, value)
  current_config = config
  current_config ? current_config[key] = value.strip.chomp : current_config = { key => value }
  File.open(NotesCli::CONFIG_PATH, 'w') { |file| file.truncate(0) }
  File.open(NotesCli::CONFIG_PATH, 'r+') do |f|
    YAML.dump(current_config, f)
  end
end