Module: Wpxf::Cli::Modules

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

Overview

Methods for handling module loading and searching.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#context_stackObject

Returns the value of attribute context_stack.



91
92
93
# File 'lib/wpxf/cli/modules.rb', line 91

def context_stack
  @context_stack
end

Instance Method Details

#backObject



16
17
18
19
20
21
# File 'lib/wpxf/cli/modules.rb', line 16

def back
  context_stack.pop
  context.module.active_workspace = active_workspace if context

  refresh_autocomplete_options
end

#contextObject



12
13
14
# File 'lib/wpxf/cli/modules.rb', line 12

def context
  context_stack.last
end

#initializeObject



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

def initialize
  super
  self.context_stack = []
end

#module_name_from_class(klass) ⇒ Object



57
58
59
# File 'lib/wpxf/cli/modules.rb', line 57

def module_name_from_class(klass)
  klass.new.module_name
end


66
67
68
69
70
71
72
73
# File 'lib/wpxf/cli/modules.rb', line 66

def print_module_table(modules)
  modules = modules.sort_by { |k| k[:path] }
  modules.unshift(path: 'Module', title: 'Title')
  puts
  indent_cursor 2 do
    print_table(modules)
  end
end

#reloadObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/wpxf/cli/modules.rb', line 23

def reload
  return unless module_loaded?(false)

  begin
    mod = context.reload
    mod.event_emitter.subscribe(self)
    mod.active_workspace = active_workspace
    print_good "Reloaded module: #{mod}"
  rescue StandardError => e
    print_bad "Failed to reload module: #{e}"
    return
  end

  apply_global_options(mod)
end

#reset_context_stackObject



86
87
88
89
# File 'lib/wpxf/cli/modules.rb', line 86

def reset_context_stack
  self.context_stack = []
  setup_auto_complete
end

#search(*args) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/wpxf/cli/modules.rb', line 75

def search(*args)
  results = search_modules(args)

  if !results.empty?
    print_good "#{results.length} Results for \"#{args.join(' ')}\""
    print_module_table results
  else
    print_bad "No results for \"#{args.join(' ')}\""
  end
end

#search_modules(args) ⇒ Object



61
62
63
64
# File 'lib/wpxf/cli/modules.rb', line 61

def search_modules(args)
  modules = Wpxf::Models::Module.where(Sequel.ilike(:name, "%#{args.join(' ')}%"))
  modules.map { |m| { path: m.path, title: m.name } }
end

#use(module_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wpxf/cli/modules.rb', line 39

def use(module_path)
  context = Context.new
  begin
    mod = context.load_module(module_path)
    mod.event_emitter.subscribe(self)
    mod.active_workspace = active_workspace
    print_good "Loaded module: #{mod}"
    mod.emit_usage_info
    context_stack.push(context)
  rescue StandardError => e
    print_bad "Failed to load module: #{e}"
    return
  end

  apply_global_options(mod)
  refresh_autocomplete_options
end