simple_console 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/README.md +13 -1
- data/lib/console/version.rb +1 -1
- data/lib/console.rb +3 -12
- data/spec/lib/console_spec.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba3b663a2e3b1e7ee541d4b6badb3679d2bb346a
|
4
|
+
data.tar.gz: 84608810ba2937791ee1f7f4615b3fccd97995ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a49bad893806d2165fccacba1478cd0ebbb83fc4cb53ddea8d571cdc327db2b766c0cd3f9ccaf3e5655f44ba80dd89bad16a0412ae81d15d552b7c1ed876c2cf
|
7
|
+
data.tar.gz: afad157479e35d21113f6829ab12083de8e101aa7dd64dccaa3d7893ec62fdde005eae5137e3092ec0659a639de4d75cfe46042e0730fad6749623da1783eea4
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Simple console
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'console'
|
10
|
+
gem 'console'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -27,6 +27,11 @@ class MyConsole
|
|
27
27
|
define_cmd(:rand, "puts random number") do |max = 100|
|
28
28
|
puts rand(max.to_i)
|
29
29
|
end
|
30
|
+
|
31
|
+
define_cmd(:incr, "increment number") do |incrby = 1|
|
32
|
+
@number ||= 0
|
33
|
+
puts @number += incrby.to_i
|
34
|
+
end
|
30
35
|
end
|
31
36
|
|
32
37
|
MyConsole.new.start("my-console > ", "Use 'help' show all commands")
|
@@ -39,12 +44,19 @@ my-console > help
|
|
39
44
|
help: show all commands
|
40
45
|
exit: exit console
|
41
46
|
rand: puts random number
|
47
|
+
incr: increment number
|
42
48
|
my-console > rand
|
43
49
|
23
|
44
50
|
my-console > rand 10
|
45
51
|
4
|
46
52
|
my-console > rand 10100
|
47
53
|
2492
|
54
|
+
my-console > incr
|
55
|
+
1
|
56
|
+
my-console > incr 3
|
57
|
+
4
|
58
|
+
my-console > incr
|
59
|
+
5
|
48
60
|
my-console > exit
|
49
61
|
$
|
50
62
|
|
data/lib/console/version.rb
CHANGED
data/lib/console.rb
CHANGED
@@ -15,15 +15,14 @@ module Console
|
|
15
15
|
module ClassMethods
|
16
16
|
def define_cmd(name, desc, &block)
|
17
17
|
commands[name.to_s] = {desc: desc, block: block}
|
18
|
-
|
19
|
-
define_method(command_method_name(name), &block)
|
20
18
|
end
|
21
19
|
|
22
20
|
def run_cmd(instance, cmd_name, args = [])
|
23
21
|
cmd_name = cmd_name.to_s
|
22
|
+
command = commands[cmd_name]
|
24
23
|
|
25
|
-
if
|
26
|
-
instance.
|
24
|
+
if command
|
25
|
+
instance.instance_exec(*args, &command[:block])
|
27
26
|
else
|
28
27
|
puts "Invalid command '#{cmd_name}'"
|
29
28
|
end
|
@@ -34,14 +33,6 @@ module Console
|
|
34
33
|
def commands
|
35
34
|
@commands ||= {}
|
36
35
|
end
|
37
|
-
|
38
|
-
|
39
|
-
private
|
40
|
-
|
41
|
-
def command_method_name(cmd_name)
|
42
|
-
"_cmd_#{cmd_name}"
|
43
|
-
end
|
44
|
-
|
45
36
|
end
|
46
37
|
|
47
38
|
|
data/spec/lib/console_spec.rb
CHANGED
@@ -48,10 +48,10 @@ RSpec.describe Console do
|
|
48
48
|
|
49
49
|
|
50
50
|
describe '.define_cmd' do
|
51
|
-
it 'should
|
51
|
+
it 'should save block' do
|
52
52
|
expect {
|
53
53
|
cls.define_cmd(:add, "desc") { |a, b| puts a + b }
|
54
|
-
}.to change { cls.
|
54
|
+
}.to change { cls.commands.length }.by(1)
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_console
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jiangzhi.xie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|