cinatra 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -0,0 +1,31 @@
1
+ require 'cinatra'
2
+
3
+ TODOS = []
4
+
5
+ def show_todos
6
+ TODOS.each_with_index do |todo, index|
7
+ puts "#{index}: #{todo}"
8
+ end
9
+ end
10
+
11
+ command 'todo add' do |arg|
12
+ TODOS << arg
13
+ show_todos
14
+ end
15
+
16
+ command 'todo delete' do |arg|
17
+ TODOS.delete_at(arg.to_i)
18
+ show_todos
19
+ end
20
+
21
+ command 'todo list' do |arg|
22
+ show_todos
23
+ end
24
+
25
+ command 'todo clear' do |arg|
26
+ TODOS.clear
27
+ end
28
+
29
+ command 'exit' do
30
+ Cinatra.exit
31
+ end
data/lib/cinatra.rb CHANGED
@@ -4,11 +4,12 @@ require 'readline'
4
4
  class Cinatra
5
5
  include Singleton
6
6
 
7
- def add_command(name, &block)
8
- raise ArgumentError, "invalid command name: #{name}." unless /^\w+$/ =~ name.to_s
9
- raise "command '#{name}' is already exists." if commands.key?(name.to_sym)
7
+ attr_accessor :exiting
10
8
 
11
- commands[name.to_sym] = block
9
+ def add_command(name, &block)
10
+ name = self.class.normalize_as_command_name(name.to_s).to_sym
11
+ raise "command '#{name}' is already exists." if commands.key?(name)
12
+ commands[name] = block
12
13
  end
13
14
 
14
15
  def delete_command(name)
@@ -23,13 +24,19 @@ class Cinatra
23
24
  @commands ||= {}
24
25
  end
25
26
 
27
+ def command_names
28
+ commands.keys.map {|i| i.to_s }
29
+ end
30
+
26
31
  def call(line)
27
- return unless /^\s*(\w+)\s+(.*?)\s*$/ =~ line
28
- unless command = commands[$1.to_sym]
29
- puts "Command `#{command}` not found!"
32
+ line = line.strip
33
+ return if line.empty?
34
+ command_name, command_arg = resolve_command_name_and_arg(line)
35
+ unless command_name
36
+ puts "Command not found!"
30
37
  else
31
38
  begin
32
- command.call($2)
39
+ get_command(command_name).call(command_arg)
33
40
  rescue Exception => e
34
41
  puts e.message
35
42
  end
@@ -51,19 +58,46 @@ class Cinatra
51
58
  Cinatra.commands.keys.map {|i| i.to_s }.grep(/#{Regexp.quote(text)}/)
52
59
  end
53
60
 
54
- while buf = Readline.readline('> ', true)
61
+ while !exiting && buf = Readline.readline('> ', true)
55
62
  call(buf)
56
63
  end
57
64
  end
58
65
 
66
+ def exit
67
+ self.exiting = true
68
+ end
69
+
70
+ def resolve_command_name_and_arg(line)
71
+ command_names.map {|i| i.split(' ')}.sort_by{|i| i.size}.reverse_each do |command|
72
+ if self.class.is_command_match_to_line?(command, line)
73
+ name = command.join(' ').to_sym
74
+ arg = line.split(' ', command.size + 1)[command.size]
75
+ return name, arg
76
+ end
77
+ end
78
+ return nil, nil
79
+ end
80
+
59
81
  class << self
60
- [:add_command, :get_command, :delete_command, :commands, :start, :call].each do |method|
82
+ [
83
+ :add_command, :get_command, :delete_command,
84
+ :commands, :start, :call, :command_names,
85
+ :resolve_command_name_and_arg, :exit
86
+ ].each do |method|
61
87
  class_eval <<-DELIM
62
88
  def #{method}(*args, &block)
63
89
  instance.#{method}(*args, &block)
64
90
  end
65
91
  DELIM
66
92
  end
93
+
94
+ def normalize_as_command_name(name)
95
+ name.strip.gsub(/\s+/, ' ')
96
+ end
97
+
98
+ def is_command_match_to_line?(command, line)
99
+ line.split(' ')[0...command.size] == command
100
+ end
67
101
  end
68
102
  end
69
103
 
data/test/test_cinatra.rb CHANGED
@@ -47,10 +47,69 @@ class TestCinatra < Test::Unit::TestCase
47
47
  Cinatra.call(' test_ foo bar ')
48
48
  $stdout = STDOUT
49
49
  end
50
+
51
+ should 'resolve command name' do
52
+ assert_equal [:test, nil], Cinatra.resolve_command_name_and_arg('test')
53
+ assert_equal [:test, 'foo'], Cinatra.resolve_command_name_and_arg('test foo')
54
+ assert_equal [:test, 'foo bar'], Cinatra.resolve_command_name_and_arg('test foo bar')
55
+ assert_equal [:test, 'foo bar'], Cinatra.resolve_command_name_and_arg('test foo bar')
56
+ end
57
+
58
+ context "add a command 'test foo'" do
59
+ setup do
60
+ @block_foo = lambda {}
61
+ Cinatra.add_command('test foo', &@block_foo)
62
+ end
63
+
64
+ should "call command 'test'" do
65
+ mock(@block).call('bar')
66
+ Cinatra.call('test bar')
67
+ end
68
+
69
+ should "call command 'test foo'" do
70
+ mock(@block).call.with_any_args.times(0)
71
+ mock(@block_foo).call(nil)
72
+ Cinatra.call('test foo')
73
+ mock(@block_foo).call('a b')
74
+ Cinatra.call('test foo a b')
75
+ end
76
+
77
+ context "add a command 'test foo bar'" do
78
+ setup do
79
+ @block_foo_bar = lambda {}
80
+ Cinatra.add_command('test foo bar', &@block_foo_bar)
81
+ end
82
+
83
+ should "call command 'test foo bar'" do
84
+ mock(@block).call.with_any_args.times(0)
85
+ mock(@block_foo).call.with_any_args.times(0)
86
+ mock(@block_foo_bar).call(nil)
87
+ Cinatra.call('test foo bar')
88
+ mock(@block_foo_bar).call('a b')
89
+ Cinatra.call('test foo bar a b')
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ should "check to match command to line" do
96
+ assert Cinatra.is_command_match_to_line?(['test'], 'test') == true
97
+ assert Cinatra.is_command_match_to_line?(['test'], ' test ') == true
98
+ assert Cinatra.is_command_match_to_line?(['test'], 'te st') == false
99
+ assert Cinatra.is_command_match_to_line?(['test'], 'tes') == false
100
+ assert Cinatra.is_command_match_to_line?(['test', 'foo'], 'test foo') == true
101
+ assert Cinatra.is_command_match_to_line?(['test', 'foo'], ' test foo ') == true
102
+ assert Cinatra.is_command_match_to_line?(['test', 'bar'], ' test foo ') == false
103
+ assert Cinatra.is_command_match_to_line?(['test', 'foo'], 'foo test') == false
104
+ assert Cinatra.is_command_match_to_line?(['test', 'foo', 'bar'], 'test foo bar') == true
50
105
  end
51
106
 
52
- should "raise Error when spcify invalid name for command " do
53
- assert_raise(ArgumentError) { Cinatra.add_command(' test') {} }
107
+ should "normalize as command name" do
108
+ assert_equal 'test', Cinatra.normalize_as_command_name('test')
109
+ assert_equal 'test', Cinatra.normalize_as_command_name(' test ')
110
+ assert_equal 'test foo', Cinatra.normalize_as_command_name('test foo')
111
+ assert_equal 'test foo', Cinatra.normalize_as_command_name(' test foo ')
112
+ assert_equal '', Cinatra.normalize_as_command_name('')
54
113
  end
55
114
 
56
115
  should "raise Error when add commans with same name" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -48,6 +48,7 @@ files:
48
48
  - Rakefile
49
49
  - VERSION
50
50
  - examples/sleep.rb
51
+ - examples/sub_command.rb
51
52
  - lib/cinatra.rb
52
53
  - test/helper.rb
53
54
  - test/test_cinatra.rb
@@ -83,3 +84,4 @@ test_files:
83
84
  - test/helper.rb
84
85
  - test/test_cinatra.rb
85
86
  - examples/sleep.rb
87
+ - examples/sub_command.rb