Class: Cinatra

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cinatra.rb,
lib/cinatra/command.rb

Defined Under Namespace

Classes: Command

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#exitingObject

Returns the value of attribute exiting.



8
9
10
# File 'lib/cinatra.rb', line 8

def exiting
  @exiting
end

Instance Method Details

#add_command(name, desc = '', &block) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/cinatra.rb', line 10

def add_command(name, desc = '', &block)
  name = normalize_as_command_name(name)
  if commands.key?(name)
    puts "Warning: The command '#{name}' will be overridden."
  end
  commands[name] = Command.new(name, desc, &block)
end

#call(line) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cinatra.rb', line 34

def call(line)
  line = line.strip
  return if line.empty?
  command_name, command_arg = resolve_command_name_and_arg(line)
  unless command_name
    puts "Error: Command not found!"
  else
    begin
      get_command(command_name).call(command_arg)
    rescue Exception => e
      puts e.message
    end
  end
end

#command_namesObject



30
31
32
# File 'lib/cinatra.rb', line 30

def command_names
  commands.keys.map {|i| i.to_s }
end

#commandsObject



26
27
28
# File 'lib/cinatra.rb', line 26

def commands
  @commands ||= {}
end

#delete_command(name) ⇒ Object



18
19
20
# File 'lib/cinatra.rb', line 18

def delete_command(name)
  commands.delete(normalize_as_command_name(name))
end

#exitObject



69
70
71
# File 'lib/cinatra.rb', line 69

def exit
  self.exiting = true
end

#get_command(name) ⇒ Object



22
23
24
# File 'lib/cinatra.rb', line 22

def get_command(name)
  commands[name.to_sym]
end

#is_command_match_to_line?(command, line) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/cinatra.rb', line 90

def is_command_match_to_line?(command, line)
  line.split(' ')[0...command.size] == command
end

#normalize_as_command_name(name) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
# File 'lib/cinatra.rb', line 84

def normalize_as_command_name(name)
  name = name.to_s
  raise ArgumentError, "`#{name}` is invalid for command name." if /^\s*$/ =~ name
  name.strip.gsub(/\s+/, ' ').to_sym
end

#resolve_command_name_and_arg(line) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/cinatra.rb', line 73

def resolve_command_name_and_arg(line)
  command_names.map {|i| i.split(' ')}.sort_by{|i| i.size}.reverse_each do |command|
    if is_command_match_to_line?(command, line)
      name = command.join(' ').to_sym
      arg = line.split(' ', command.size + 1)[command.size]
      return name, arg
    end
  end
  return nil, nil
end

#startObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/cinatra.rb', line 49

def start
  stty_save = `stty -g`.chomp
  trap("INT") do
    begin
      system "stty", stty_save
    ensure
      exit
    end
  end

  Readline.basic_word_break_characters= "\t\n\"\\'`><=;|&{("
  Readline.completion_proc = lambda do |text|
    Cinatra.commands.keys.map {|i| i.to_s }.grep(/#{Regexp.quote(text)}/)
  end

  while !exiting && buf = Readline.readline('> ', true)
    call(buf)
  end
end