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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c732548976f9a9abed280b2feb3e4794852a850
4
- data.tar.gz: 6ca5f7cb9937abc5f952f6ad1f3f756a5a076e63
3
+ metadata.gz: ba3b663a2e3b1e7ee541d4b6badb3679d2bb346a
4
+ data.tar.gz: 84608810ba2937791ee1f7f4615b3fccd97995ab
5
5
  SHA512:
6
- metadata.gz: 9ff0209a9c1218d621c87fa4be33972ba468a4a948a0c566cfd06e00fd36cea6c1a368ee535c41da5279a9c505c8d9f2704ea9f384f1fe28c53dcaf83e95dbeb
7
- data.tar.gz: c32129efd3f8e6a9429575e0f32cd96d4f0ef60a4c1728865a6ae8fe107a86cf015030efb3a3dca2a955f1c91fd8077624bb905643f47b3c84d64f329599ef1b
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', github: 'xjz19901211/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
 
@@ -1,3 +1,3 @@
1
1
  module Console
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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 commands[cmd_name]
26
- instance.send(command_method_name(cmd_name), *args)
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
 
@@ -48,10 +48,10 @@ RSpec.describe Console do
48
48
 
49
49
 
50
50
  describe '.define_cmd' do
51
- it 'should create instance method' do
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.instance_methods.include?(:_cmd_add) }.to(true)
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.2
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-06-03 00:00:00.000000000 Z
11
+ date: 2015-07-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler