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

Constructor Details

#initializeCinatra

Returns a new instance of Cinatra.



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

def initialize
  Readline.basic_word_break_characters= "\t\n\"\\'`><=;|&{("
  Readline.completion_proc = lambda {|text| completion(text) }
  trap("INT") { exit }
end

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



16
17
18
19
20
21
22
23
24
# File 'lib/cinatra.rb', line 16

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)
rescue Exception => e
  handle_error(e)
end

#call(line) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cinatra.rb', line 42

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: #{line}"
  else
    get_command(command_name).call(command_arg)
  end
rescue Exception => e
  handle_error(e)
end

#command_namesObject



38
39
40
# File 'lib/cinatra.rb', line 38

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

#commandsObject



34
35
36
# File 'lib/cinatra.rb', line 34

def commands
  @commands ||= {}
end

#completion(text) ⇒ Object



55
56
57
# File 'lib/cinatra.rb', line 55

def completion(text)
  commands.keys.map {|i| i.to_s }.grep(/^#{Regexp.quote(text)}/)
end

#delete_command(name) ⇒ Object



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

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

#exitObject



65
66
67
# File 'lib/cinatra.rb', line 65

def exit
  self.exiting = true
end

#get_command(name) ⇒ Object



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

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

#handle_error(e) ⇒ Object



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

def handle_error(e)
  puts "Error: #{e.message}\n  #{e.backtrace.join("\n  ")}"
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



59
60
61
62
63
# File 'lib/cinatra.rb', line 59

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