Shorty 0.1.0 → 0.2.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  Shorty
3
3
  ================
4
4
 
5
- A Ruby gem.
5
+ A Ruby gem that provides DSL to add before/after hooks to method calls.
6
6
 
7
7
  Installation
8
8
  ------------
data/bin/Shorty ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- ruby -*-
3
+ #
4
+
5
+
6
+ require "Shorty"
7
+
8
+ include Shorty::DSL
9
+
10
+ files = ARGV.map { |str| File.expand_path(str) }.uniq
11
+
12
+ files.each { |file|
13
+ require file
14
+ }
data/lib/Shorty.rb CHANGED
@@ -27,14 +27,19 @@ class Shorty
27
27
  shortys[name] = val
28
28
  end
29
29
 
30
- def before name, action, &blok
31
- befores[[name, action]] ||= []
32
- befores[[name, action]] << blok
30
+ def before *args, &blok
31
+ add_hook :before, *args, &blok
33
32
  end
34
33
 
35
- def after name, action, &blok
36
- afters[[name, action]] ||= []
37
- afters[[name, action]] << blok
34
+ def after *args, &blok
35
+ add_hook :after, *args, &blok
36
+ end
37
+
38
+ def add_hook type, name, action, l = nil, &blok
39
+ raise ArgumentError, "lambda and block both given" if l && blok
40
+ list = (type == :before ? befores : afters )
41
+ list[[name, action]] ||= []
42
+ list[[name, action]] << (l || blok)
38
43
  end
39
44
 
40
45
  def run_hooks group, name, action, package
@@ -67,10 +72,6 @@ class Shorty
67
72
  result
68
73
  end # === def package
69
74
 
70
- def executable? name
71
- `which #{name}`.strip.empty?
72
- end
73
-
74
75
  end # === DSL
75
76
 
76
77
  include DSL
@@ -1,3 +1,3 @@
1
1
  class Shorty
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,12 @@
1
+
2
+ add :uptime, lambda { puts 'uptime' }
3
+
4
+ before :uptime, :run do
5
+ puts "starting uptime"
6
+ end
7
+
8
+ after :uptime, :run do
9
+ puts "finished uptime"
10
+ end
11
+
12
+ run :uptime
data/spec/main.rb CHANGED
@@ -3,6 +3,7 @@ require File.expand_path('spec/helper')
3
3
  require 'Shorty'
4
4
  require 'Bacon_Colored'
5
5
  require 'pry'
6
+ require 'Exit_Zero'
6
7
 
7
8
 
8
9
  # ======== Include the tests.
data/spec/tests/Shorty.rb CHANGED
@@ -47,6 +47,28 @@ describe "Shorty before" do
47
47
  t.should == [:b, :t]
48
48
  end
49
49
 
50
+ it "accepts a lambda instead of a block" do
51
+ s = Shorty.new
52
+ t = []
53
+ s.add :test, lambda { t << :t }
54
+ s.before :test, :run, lambda {
55
+ t << :b
56
+ }
57
+
58
+ s.run :test
59
+ t.should == [:b, :t]
60
+ end
61
+
62
+ it "raises ArgumentError if both a lambda and a block are given" do
63
+ s = Shorty.new
64
+ s.add :test, lambda {}
65
+ lambda {
66
+ l = lambda {}
67
+ s.before(:test, :run, l) {}
68
+ }.should.raise(ArgumentError)
69
+ .message.should.match %r!lambda and block both given!
70
+ end
71
+
50
72
  end # === Shorty before
51
73
 
52
74
 
@@ -64,4 +86,26 @@ describe "Shorty after" do
64
86
  t.should == [:t, :a]
65
87
  end
66
88
 
89
+ it "accepts a lambda instead of a block" do
90
+ s = Shorty.new
91
+ t = []
92
+ s.add :test, lambda { t << :t }
93
+ s.after :test, :run, lambda {
94
+ t << :a
95
+ }
96
+
97
+ s.run :test
98
+ t.should == [:t, :a]
99
+ end
100
+
101
+ it "raises ArgumentError if both a lambda and a block are given" do
102
+ s = Shorty.new
103
+ s.add :test, lambda {}
104
+ lambda {
105
+ l = lambda {}
106
+ s.after(:test, :run, l) {}
107
+ }.should.raise(ArgumentError)
108
+ .message.should.match %r!lambda and block both given!
109
+ end
110
+
67
111
  end # === Shorty after
data/spec/tests/bin.rb CHANGED
@@ -1,13 +1,36 @@
1
1
 
2
- bins = Dir.glob("bin/*")
3
-
4
- unless bins.empty?
5
- describe "permissions of bin/" do
6
- bins.each { |file|
7
- it "should chmod 755 for: #{file}" do
8
- `stat -c %a #{file}`.strip
9
- .should.be == "755"
10
- end
11
- }
12
- end # === permissions of bin/
13
- end # === unless bins.empty?
2
+
3
+ describe "permissions of bin/" do
4
+
5
+ bins = Dir.glob("bin/*")
6
+
7
+ bins.each { |file|
8
+ it "should chmod 755 for: #{file}" do
9
+ `stat -c %a #{file}`.strip
10
+ .should.be == "755"
11
+ end
12
+ }
13
+
14
+ end # === permissions of bin/
15
+
16
+ describe "Shorty bin" do
17
+
18
+ it "runs file that includes Shorty DSL" do
19
+ Exit_0("Shorty spec/files/uptime.rb").out.split
20
+ .should == %@
21
+ starting uptime
22
+ uptime
23
+ finished uptime
24
+ @.split
25
+ end
26
+
27
+ it "runs file only once despite same file, different relative paths" do
28
+ Exit_0("Shorty spec/files/uptime.rb ./spec/files/uptime.rb").out.split
29
+ .should == %@
30
+ starting uptime
31
+ uptime
32
+ finished uptime
33
+ @.split
34
+ end
35
+
36
+ end # === Shorty bin
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.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-20 00:00:00.000000000 Z
12
+ date: 2012-04-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bacon
@@ -95,7 +95,8 @@ description: ! "\n Add before/after hooks to your method calls. More info. a
95
95
  \ the homepage.\n "
96
96
  email:
97
97
  - i-hate-spam-45671204@mailinator.com
98
- executables: []
98
+ executables:
99
+ - Shorty
99
100
  extensions: []
100
101
  extra_rdoc_files: []
101
102
  files:
@@ -104,8 +105,10 @@ files:
104
105
  - README.md
105
106
  - Rakefile
106
107
  - Shorty.gemspec
108
+ - bin/Shorty
107
109
  - lib/Shorty.rb
108
110
  - lib/Shorty/version.rb
111
+ - spec/files/uptime.rb
109
112
  - spec/helper.rb
110
113
  - spec/main.rb
111
114
  - spec/tests/DSL.rb