class Main

Constants

ActionWithArgs

Attributes

chid_config[R]

Public Class Methods

new(chid_config) click to toggle source
# File lib/chid/main.rb, line 9
def initialize(chid_config)
  @chid_config = chid_config
end

Public Instance Methods

init(&execute_action_block) click to toggle source
# File lib/chid/main.rb, line 13
def init(&execute_action_block)
  puts "Hello #{chid_config.username}".blue
  puts "How can I help you?"

  run(&execute_action_block)
end

Private Instance Methods

choose_multiple_action(actions_with_args) click to toggle source
# File lib/chid/main.rb, line 86
def choose_multiple_action(actions_with_args)
  puts "You are trying to execute #{actions_with_args.count} actions at once."
  puts "Please choose:"
  puts "0 - none"
  actions_with_args.each_with_index { |a, i| puts "#{i + 1} - #{a.action}" }
  choose = fn_get_input.().to_i
  if choose == 0
    puts "Ok, canceled"
    return nil
  end
  choose = choose - 1
  actions_with_args[choose]
end
fn_get_input() click to toggle source
# File lib/chid/main.rb, line 21
def fn_get_input
  -> () {
    print "> "
    STDIN.gets.strip
  }
end
get_action(input, &execute_action_block) click to toggle source
# File lib/chid/main.rb, line 42
def get_action(input, &execute_action_block)
  actions_with_args = get_actions_with_args(input)
  return not_found_msgs if actions_with_args.empty?

  get_action_with_args(actions_with_args) do | action_with_args|
    execute_action_block.(action_with_args.action, action_with_args.args)
  end

end
get_action_with_args(actions_with_args) { |action_with_args| ... } click to toggle source
# File lib/chid/main.rb, line 74
def get_action_with_args(actions_with_args)
  return if actions_with_args.nil?

  if actions_with_args.count > 1
    action_with_args = choose_multiple_action(actions_with_args)
  else
    action_with_args = actions_with_args.first
  end

  yield action_with_args if action_with_args
end
get_actions_with_args(input) click to toggle source
# File lib/chid/main.rb, line 52
def get_actions_with_args(input)
  actions_with_args = Chid::REGEX_ACTIONS.collect do |action, regexs|
    action_with_args = nil

    regex_match(input, regexs) do |captured_args|
      action_with_args  = ActionWithArgs.new(action, captured_args)
    end
    action_with_args
  end.compact!
end
not_found_msgs() click to toggle source
# File lib/chid/main.rb, line 100
def not_found_msgs()
  msgs = [
    "Sorry, I did not found any action.",
    "You should try another action. That does not exist. Sorry.",
    "Maybe you typed wrongly. Please try again."
  ]

  msg_number = rand(3) - 1

  puts msgs[msg_number]
end
quit_command?(input) click to toggle source
# File lib/chid/main.rb, line 38
def quit_command?(input)
  input =~ /^:q/ || input =~ /^bye/ || input =~ /^quit/ || input =~ /^exit/
end
regex_match(input, regexs, &block) click to toggle source
# File lib/chid/main.rb, line 63
def regex_match(input, regexs, &block)
  regexs.each do |regex|
    matched = regex.match input
    if matched
      captured_args = matched.captures
      block.(captured_args)
      return
    end
  end
end
run(&execute_action_block) click to toggle source
# File lib/chid/main.rb, line 28
def run(&execute_action_block)
  input = fn_get_input.()
  if quit_command?(input)
    puts 'Bye Bye'
    return
  end
  get_action(input, &execute_action_block)
  run(&execute_action_block)
end