Module: Wpxf::Cli::Workspace

Included in:
Console
Defined in:
lib/wpxf/cli/workspace.rb

Overview

Provides functionality for interacting with workspaces.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#active_workspaceObject

Returns the value of attribute active_workspace.



77
78
79
# File 'lib/wpxf/cli/workspace.rb', line 77

def active_workspace
  @active_workspace
end

Instance Method Details

#add_workspace(name) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wpxf/cli/workspace.rb', line 30

def add_workspace(name)
  unless Wpxf::Models::Workspace.where(name: name).count.zero?
    return print_warning "#{name} already exists"
  end

  begin
    Wpxf::Models::Workspace.create(name: name)
    return print_good "Added workspace: #{name}"
  rescue Sequel::ValidationFailed
    print_warning 'Workspace names may only contain 1-50 alphanumeric characters and underscores'
  end
end

#delete_workspace(name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wpxf/cli/workspace.rb', line 65

def delete_workspace(name)
  if name == 'default'
    print_warning 'You cannot delete the default workspace'
    return
  end

  current_name = active_workspace.name
  Wpxf::Models::Workspace.where(name: name).destroy
  print_good "Deleted workspace: #{name}"
  switch_workspace 'default' if name == current_name
end

#initializeObject



7
8
9
10
11
# File 'lib/wpxf/cli/workspace.rb', line 7

def initialize
  super

  self.active_workspace = Wpxf::Models::Workspace.first(name: 'default')
end

#list_workspacesObject



43
44
45
46
47
48
49
50
51
# File 'lib/wpxf/cli/workspace.rb', line 43

def list_workspaces
  workspaces.each do |workspace|
    if workspace.id == active_workspace.id
      print_info "#{workspace.name} #{'(active)'.green}"
    else
      print_info workspace.name
    end
  end
end

#switch_workspace(name) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/wpxf/cli/workspace.rb', line 53

def switch_workspace(name)
  next_workspace = Wpxf::Models::Workspace.first(name: name)

  if next_workspace
    self.active_workspace = next_workspace
    context.module.active_workspace = active_workspace if context&.module
    print_good "Switched to workspace: #{name}"
  else
    print_bad "#{name} is not a valid workspace"
  end
end

#workspace(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/wpxf/cli/workspace.rb', line 13

def workspace(*args)
  return list_workspaces if args.length.zero?

  case args[0]
  when '-a'
    add_workspace(args[1])
  when '-d'
    delete_workspace(args[1])
  else
    switch_workspace(args[0])
  end
end

#workspacesObject



26
27
28
# File 'lib/wpxf/cli/workspace.rb', line 26

def workspaces
  Wpxf::Models::Workspace.all
end