Class: DevSystem::CommandPanel

Inherits:
Liza::Panel show all
Defined in:
lib/dev_system/sub/command/command_panel.rb

Defined Under Namespace

Classes: AlreadySet, Error, NotFoundError, ParseError

Constant Summary collapse

PARSE_REGEX =
/(?<command_given>[A-Za-z0-9_]+)(?::(?<command_class_method>[a-z0-9_]+))?(?:#(?<command_instance_method>[a-z0-9_]+))?(?:\.(?<command_method>[a-z0-9_]+))?/

Instance Attribute Summary

Attributes inherited from Liza::Panel

#key

Instance Method Summary collapse

Methods inherited from Liza::Panel

box, #box, color, #controller, controller, division, #division, #initialize, #push, #short, #started, subsystem, #subsystem, token

Methods inherited from Liza::Unit

const_missing, division, part, system, #system, test_class

Constructor Details

This class inherits a constructor from Liza::Panel

Instance Method Details

#_find(string) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/dev_system/sub/command/command_panel.rb', line 53

def _find string
  k = Liza.const "#{string}_command"
  log :lower, k
  k
rescue Liza::ConstNotFound
  raise NotFoundError, "command not found: #{string.inspect}"
end

#build_env(args) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/dev_system/sub/command/command_panel.rb', line 36

def build_env args
  env = parse args[0]
  env[:args] = Array(args[1..-1])
  env[:command_args] = env[:args].dup
  env[:command_name] = short env[:command_given]
  env
end

#call(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dev_system/sub/command/command_panel.rb', line 7

def call args
  log :lower, "args = #{args.inspect}"

  return call_not_found args if args.none?

  env = build_env args
  find env
  forward env
  inform env
# rescue Exception => e
rescue Error => e
  rescue_from_panel(e, with: env)
end

#call_not_found(args) ⇒ Object



121
122
123
# File 'lib/dev_system/sub/command/command_panel.rb', line 121

def call_not_found args
  Liza[:NotFoundCommand].call args
end

#find(env) ⇒ Object



46
47
48
49
50
51
# File 'lib/dev_system/sub/command/command_panel.rb', line 46

def find env
  raise "env[:command_name] is empty #{env}" if env[:command_name].empty?
  env[:command_class] = Liza.const "#{env[:command_name]}_command"
rescue Liza::ConstNotFound
  raise NotFoundError, "command not found: #{env[:command_name].inspect}"
end

#forward(env) ⇒ Object



63
64
65
66
67
68
# File 'lib/dev_system/sub/command/command_panel.rb', line 63

def forward env
  command_class = env[:command_class]
  
  return forward_base_command env if command_class < BaseCommand
  return forward_command env if command_class < Command
end

#forward_base_command(env) ⇒ Object



70
71
72
73
# File 'lib/dev_system/sub/command/command_panel.rb', line 70

def forward_base_command env
  log :lower,  "forwarding"
  env[:command_class].call env
end

#forward_command(env) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/dev_system/sub/command/command_panel.rb', line 75

def forward_command env
  case
  when env[:command_class_method]
    log :lower,  "#{env[:command_class]}.#{env[:command_class_method]}(#{env[:args]})"
    env[:command_class].public_send env[:command_class_method], env[:args]
  when env[:command_instance_method]
    log :lower,  "#{env[:command_class]}.new.#{env[:command_instance_method]}(#{env[:args]})"
    env[:command_class].new.public_send env[:command_instance_method], env[:args]
  when env[:command_method]
    if env[:command_class].respond_to?(env[:command_method])
      log :lower,  "#{env[:command_class]}.#{env[:command_method]}(#{env[:args]})"
      env[:command_class].public_send env[:command_method], env[:args]
    else
      log :lower,  "#{env[:command_class]}.new.#{env[:command_method]}(#{env[:args]})"
      env[:command_class].new.public_send env[:command_method], env[:args]
    end
  else
    log :lower,  "#{env[:command_class]}.call(#{env[:args]})"
    env[:command_class].call env[:args]
  end
end

#inform(env) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/dev_system/sub/command/command_panel.rb', line 99

def inform env
  return if env[:simple] == nil
  return if env[:simple] == [""]

  env[:simple].shift if env[:simple][0] == ""

  puts if log? :higher

  args = [*env[:command_args], *env[:simple]].join(" ")

  log sticks :black, system.color, :b,
    ["LIZA"],
    ["HELPS",    :u],
    ["YOU",      :u, :i],
    ["REMEMBER", :i] \
      if log? :higher
  
  log stick system.color, "#{ $0.split("/").last } #{ env[:command_arg] } #{ args }" if log? :higher
end

#input(name = nil) ⇒ Object

Raises:



127
128
129
130
131
# File 'lib/dev_system/sub/command/command_panel.rb', line 127

def input name = nil
  return (@input || InputCommand) if name.nil?
  raise AlreadySet, "input already set to #{@input.inspect}, but trying to set to #{name.inspect}", caller if @input
  @input = _find "#{name}_input"
end

#parse(string) ⇒ Object

Hash command_name class_method instance_method method

Raises:



26
27
28
29
30
31
32
33
34
# File 'lib/dev_system/sub/command/command_panel.rb', line 26

def parse string
  md = string.to_s.match PARSE_REGEX
  raise ParseError if md.nil?

  env = md.named_captures.map { [_1.to_sym, _2] }.to_h
  env[:command_arg] = string
  log :lower, "{#{env.map { ":#{_1}=>#{_2.to_s.inspect}" }.join(", ") }}"
  env
end

#pick_many(title, options) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/dev_system/sub/command/command_panel.rb', line 142

def pick_many title, options
  if log_level? :highest
    puts
    log "Pick Many"
  end
  # input.pick_many title, options
  TtyInputCommand.multi_select title, options
end

#pick_one(title, options = ["Yes", "No"]) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/dev_system/sub/command/command_panel.rb', line 133

def pick_one title, options = ["Yes", "No"]
  if log_level? :highest
    puts
    log "Pick One"
  end
  # input.pick_one title, options
  TtyInputCommand.pick_one title, options
end