cinatra 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
data/examples/todo.rb ADDED
@@ -0,0 +1,30 @@
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', <<DESC do |arg|
12
+ Add a todo to list
13
+ usage: todo add buy the milk
14
+ DESC
15
+ TODOS << arg
16
+ show_todos
17
+ end
18
+
19
+ command 'todo delete', 'Delete a todo from list' do |arg|
20
+ TODOS.delete_at(arg.to_i)
21
+ show_todos
22
+ end
23
+
24
+ command 'todo list', 'Show todo list' do |arg|
25
+ show_todos
26
+ end
27
+
28
+ command 'todo clear', 'Clear todo list' do |arg|
29
+ TODOS.clear
30
+ end
data/lib/cinatra.rb CHANGED
@@ -1,22 +1,22 @@
1
1
  require 'singleton'
2
2
  require 'readline'
3
+ require 'cinatra/command'
3
4
 
4
5
  class Cinatra
5
6
  include Singleton
6
7
 
7
8
  attr_accessor :exiting
8
9
 
9
- def add_command(name, &block)
10
- name = self.class.normalize_as_command_name(name.to_s).to_sym
10
+ def add_command(name, desc = '', &block)
11
+ name = normalize_as_command_name(name)
11
12
  if commands.key?(name)
12
- puts "Warning: command '#{name}' is already exists."
13
- return
13
+ puts "Warning: The command '#{name}' will be overridden."
14
14
  end
15
- commands[name] = block
15
+ commands[name] = Command.new(name, desc, &block)
16
16
  end
17
17
 
18
18
  def delete_command(name)
19
- commands.delete(name.to_sym)
19
+ commands.delete(normalize_as_command_name(name))
20
20
  end
21
21
 
22
22
  def get_command(name)
@@ -72,7 +72,7 @@ class Cinatra
72
72
 
73
73
  def resolve_command_name_and_arg(line)
74
74
  command_names.map {|i| i.split(' ')}.sort_by{|i| i.size}.reverse_each do |command|
75
- if self.class.is_command_match_to_line?(command, line)
75
+ if is_command_match_to_line?(command, line)
76
76
  name = command.join(' ').to_sym
77
77
  arg = line.split(' ', command.size + 1)[command.size]
78
78
  return name, arg
@@ -81,11 +81,20 @@ class Cinatra
81
81
  return nil, nil
82
82
  end
83
83
 
84
+ def normalize_as_command_name(name)
85
+ name = name.to_s
86
+ raise ArgumentError, "`#{name}` is invalid for command name." if /^\s*$/ =~ name
87
+ name.strip.gsub(/\s+/, ' ').to_sym
88
+ end
89
+
90
+ def is_command_match_to_line?(command, line)
91
+ line.split(' ')[0...command.size] == command
92
+ end
93
+
84
94
  class << self
85
95
  [
86
96
  :add_command, :get_command, :delete_command,
87
- :commands, :start, :call, :command_names,
88
- :resolve_command_name_and_arg, :exit
97
+ :commands, :start, :call, :command_names, :exit
89
98
  ].each do |method|
90
99
  class_eval <<-DELIM
91
100
  def #{method}(*args, &block)
@@ -93,27 +102,34 @@ class Cinatra
93
102
  end
94
103
  DELIM
95
104
  end
96
-
97
- def normalize_as_command_name(name)
98
- name.strip.gsub(/\s+/, ' ')
99
- end
100
-
101
- def is_command_match_to_line?(command, line)
102
- line.split(' ')[0...command.size] == command
103
- end
104
105
  end
105
106
  end
106
107
 
107
108
  module Kernel
108
- def command(name, &block)
109
- Cinatra.add_command(name, &block)
109
+ def command(name, desc = '', &block)
110
+ Cinatra.add_command(name, desc, &block)
110
111
  end
111
112
  end
112
113
 
113
- command 'exit' do
114
+ command 'exit', 'Exit' do
114
115
  Cinatra.exit
115
116
  end
116
117
 
118
+ command 'help', 'Show help'do |arg|
119
+ if arg
120
+ command = Cinatra.get_command(arg)
121
+ if command
122
+ puts command.desc
123
+ end
124
+ else
125
+ commands = Cinatra.commands.values
126
+ max_length = commands.inject(0) {|max, command| [command.name.to_s.size, max].max }
127
+ commands.each do |command|
128
+ puts "#{command.name.to_s.ljust(max_length)} #{command.desc.split("\n")[0]}"
129
+ end
130
+ end
131
+ end
132
+
117
133
  at_exit do
118
134
  Cinatra.start
119
135
  end
@@ -0,0 +1,15 @@
1
+ class Cinatra
2
+ class Command
3
+ attr_reader :name, :desc, :proc
4
+
5
+ def initialize(name, desc = '', &block)
6
+ @name = name
7
+ @desc = desc
8
+ @proc = block
9
+ end
10
+
11
+ def call(*args)
12
+ proc.call(*args)
13
+ end
14
+ end
15
+ end
data/test/test_cinatra.rb CHANGED
@@ -8,6 +8,7 @@ class TestCinatra < Test::Unit::TestCase
8
8
  context 'default' do
9
9
  setup do
10
10
  Cinatra.commands.clear
11
+ @instance = Cinatra.instance
11
12
  end
12
13
 
13
14
  should 'add command' do
@@ -26,8 +27,14 @@ class TestCinatra < Test::Unit::TestCase
26
27
  Cinatra.add_command('test', &@block)
27
28
  end
28
29
 
30
+ should "override a command" do
31
+ block = lambda {}
32
+ Cinatra.add_command('test', &block)
33
+ assert_equal block, Cinatra.get_command('test').proc
34
+ end
35
+
29
36
  should "get a command" do
30
- assert_equal @block, Cinatra.get_command('test')
37
+ assert_equal @block, Cinatra.get_command('test').proc
31
38
  end
32
39
 
33
40
  should "delete a command" do
@@ -49,10 +56,10 @@ class TestCinatra < Test::Unit::TestCase
49
56
  end
50
57
 
51
58
  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')
59
+ assert_equal [:test, nil], @instance.resolve_command_name_and_arg('test')
60
+ assert_equal [:test, 'foo'], @instance.resolve_command_name_and_arg('test foo')
61
+ assert_equal [:test, 'foo bar'], @instance.resolve_command_name_and_arg('test foo bar')
62
+ assert_equal [:test, 'foo bar'], @instance.resolve_command_name_and_arg('test foo bar')
56
63
  end
57
64
 
58
65
  context "add a command 'test foo'" do
@@ -93,28 +100,24 @@ class TestCinatra < Test::Unit::TestCase
93
100
  end
94
101
 
95
102
  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
103
+ assert @instance.is_command_match_to_line?(['test'], 'test') == true
104
+ assert @instance.is_command_match_to_line?(['test'], ' test ') == true
105
+ assert @instance.is_command_match_to_line?(['test'], 'te st') == false
106
+ assert @instance.is_command_match_to_line?(['test'], 'tes') == false
107
+ assert @instance.is_command_match_to_line?(['test', 'foo'], 'test foo') == true
108
+ assert @instance.is_command_match_to_line?(['test', 'foo'], ' test foo ') == true
109
+ assert @instance.is_command_match_to_line?(['test', 'bar'], ' test foo ') == false
110
+ assert @instance.is_command_match_to_line?(['test', 'foo'], 'foo test') == false
111
+ assert @instance.is_command_match_to_line?(['test', 'foo', 'bar'], 'test foo bar') == true
105
112
  end
106
113
 
107
114
  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('')
113
- end
114
-
115
- should "raise Error when add commans with same name" do
116
- assert_nothing_raised(RuntimeError) { Cinatra.add_command('test') {} }
117
- assert_raise(RuntimeError) { Cinatra.add_command('test') {} }
115
+ assert_equal :'test', @instance.normalize_as_command_name('test')
116
+ assert_equal :'test', @instance.normalize_as_command_name(:test)
117
+ assert_equal :'test', @instance.normalize_as_command_name(' test ')
118
+ assert_equal :'test foo', @instance.normalize_as_command_name('test foo')
119
+ assert_equal :'test foo', @instance.normalize_as_command_name(' test foo ')
120
+ assert_raise(ArgumentError) { @instance.normalize_as_command_name('') }
118
121
  end
119
122
  end
120
123
  end
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.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-31 00:00:00 +09:00
12
+ date: 2010-01-01 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -48,8 +48,9 @@ files:
48
48
  - Rakefile
49
49
  - VERSION
50
50
  - examples/sleep.rb
51
- - examples/sub_command.rb
51
+ - examples/todo.rb
52
52
  - lib/cinatra.rb
53
+ - lib/cinatra/command.rb
53
54
  - test/helper.rb
54
55
  - test/test_cinatra.rb
55
56
  has_rdoc: true
@@ -84,4 +85,4 @@ test_files:
84
85
  - test/helper.rb
85
86
  - test/test_cinatra.rb
86
87
  - examples/sleep.rb
87
- - examples/sub_command.rb
88
+ - examples/todo.rb
@@ -1,27 +0,0 @@
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