Shorty 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -9,14 +9,19 @@ Installation
9
9
 
10
10
  gem install Shorty
11
11
 
12
- Usage
12
+ Usage: Ruby
13
13
  ------
14
14
 
15
15
  require "Shorty"
16
16
 
17
17
  include Shorty::DSL
18
18
 
19
- add :ssh, My_SSH_Commands.new
19
+ class My_SSH
20
+ def self.restart
21
+ end
22
+ end
23
+
24
+ add :ssh, My_SSH
20
25
 
21
26
  before :ssh, :restart do
22
27
  puts "restarting SSH"
@@ -26,8 +31,50 @@ Usage
26
31
  puts "SSH has been restarted"
27
32
  end
28
33
 
29
- run :ssh, :restart
34
+ run :ssh, :restart
35
+
36
+ # --> My_SSH.restart is run with before/after hooks.
37
+
38
+ You can also use lambdas or a code block:
39
+
40
+ add :start, lambda { `service ssh restart` }
41
+ before :start, :run, lambda { puts 're-starting ssh' }
42
+ after :start, :run, lambda { puts 'finished re-starting ssh' }
43
+
44
+ add :stop do
45
+ `service ssh stop`
46
+ end
30
47
 
48
+ run :start
49
+
50
+ # equivalent to...
51
+ run :start, :run
52
+
53
+ # --> "lambda { `service ssh restart` }.call" is called.
54
+
55
+ Shorty is implemented [in just one file](https://github.com/da99/Shorty/blob/master/lib/Shorty.rb)
56
+ in case you have any more questions.
57
+
58
+ Usage: Shorty (executable)
59
+ ------
60
+
61
+ Write a Ruby file, `~/uptime.rb`, that uses the Shorty DSL:
62
+
63
+ add :uptime, lambda { puts 'uptime' }
64
+
65
+ before :uptime, :run do
66
+ puts "starting uptime"
67
+ end
68
+
69
+ after :uptime, :run do
70
+ puts "finished uptime"
71
+ end
72
+
73
+ run :uptime
74
+
75
+ In your shell:
76
+
77
+ Shorty ~/uptime.rb
31
78
 
32
79
  Run Tests
33
80
  ---------
@@ -37,9 +84,4 @@ Run Tests
37
84
  bundle update
38
85
  bundle exec bacon spec/main.rb
39
86
 
40
- "I hate writing."
41
- -----------------------------
42
-
43
- If you know of existing software that makes the above redundant,
44
- please tell me. The last thing I want to do is maintain code.
45
87
 
@@ -16,14 +16,21 @@ class Shorty
16
16
  @afters ||= Hash[]
17
17
  end
18
18
 
19
- def add name, val = :_NONE_
20
- if name.is_a?(String) && val == :_NONE_
19
+ def add name, val = :_NONE_, &blok
20
+ if name.is_a?(String) && val == :_NONE_ && !blok
21
21
  file = File.expand_path(name)
22
22
  (file = "#{file}.rb") unless File.exists?(file)
23
23
  eval File.read(file), nil, file, 1
24
24
  return true
25
25
  end
26
+
26
27
  raise ArgumentError, "#{name.inspect} already set" if shortys[name]
28
+ raise ArgumentError, "lambda and block both given" if val.is_a?(Proc) && block_given?
29
+
30
+ if val == :_NONE_ && blok
31
+ val = blok
32
+ end
33
+
27
34
  shortys[name] = val
28
35
  end
29
36
 
@@ -1,3 +1,3 @@
1
1
  class Shorty
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -13,7 +13,35 @@ module My_Box
13
13
  end # === class
14
14
  end # === My_Box
15
15
 
16
- describe "Shorty run" do
16
+ describe "Shorty :add" do
17
+
18
+ it "raises ArgumentError if both lambda and block are given" do
19
+ lambda {
20
+ s = Shorty.new
21
+ l = lambda {}
22
+ s.add :my_box, l do
23
+ end
24
+ }.should.raise(ArgumentError)
25
+ .message.should.match %r!lambda and block both given!
26
+ end
27
+
28
+ it "accepts a block as a value" do
29
+ p = Proc.new {}
30
+ s = Shorty.new
31
+ s.add :my_box, &p
32
+ s.shortys[:my_box].should == p
33
+ end
34
+
35
+ it "accepts a lambda as a value" do
36
+ l = lambda {}
37
+ s = Shorty.new
38
+ s.add :my_box, l
39
+ s.shortys[:my_box].should == l
40
+ end
41
+
42
+ end # === Shorty :add
43
+
44
+ describe "Shorty :run" do
17
45
 
18
46
  before {
19
47
  @s = Shorty.new
@@ -46,6 +74,18 @@ describe "Shorty before" do
46
74
  s.run :test
47
75
  t.should == [:b, :t]
48
76
  end
77
+
78
+ it "accepts a block" do
79
+ s = Shorty.new
80
+ t = []
81
+ s.add :test, lambda { t << :t }
82
+ s.before :test, :run do
83
+ t << :b
84
+ end
85
+
86
+ s.run :test
87
+ t.should == [:b, :t]
88
+ end
49
89
 
50
90
  it "accepts a lambda instead of a block" do
51
91
  s = Shorty.new
@@ -85,6 +125,18 @@ describe "Shorty after" do
85
125
  s.run :test
86
126
  t.should == [:t, :a]
87
127
  end
128
+
129
+ it "accepts a block" do
130
+ s = Shorty.new
131
+ t = []
132
+ s.add :test, lambda { t << :t }
133
+ s.after :test, :run do
134
+ t << :a
135
+ end
136
+
137
+ s.run :test
138
+ t.should == [:t, :a]
139
+ end
88
140
 
89
141
  it "accepts a lambda instead of a block" do
90
142
  s = Shorty.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Shorty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: