patch_life 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -3
- data/lib/patch_life.rb +10 -7
- data/lib/version.rb +1 -1
- data/spec/print_patch_warning_spec.rb +12 -12
- data/spec/spec_helper.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -7,7 +7,7 @@ But, as often happens, large scale ecosystems tend to collect dust, and hide sec
|
|
7
7
|
The first, uses a Warning, printed every time your application starts, when the patch is loaded. If your defined Ruby Version and Patch level are equal to or older than the version of Ruby you're running the application with, you'll receive a Warning.
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
|
10
|
+
define_patch :version => '1.9.3', :patch=> 448, :message => "It's time to remove me, as you've upgraded to a version of ruby where this code is no longer needed"
|
11
11
|
|
12
12
|
#Dirty hack that is considered a bold move
|
13
13
|
```
|
@@ -15,7 +15,7 @@ define_patch_life :version => '1.9.3', :patch=> 448, :message => "It's time to r
|
|
15
15
|
The second, allows you to pass the patch into define_patch_life as a block, only to be executed if the defined version / patch level of Ruby is not equal to or higher than the Ruby Version the application is running with.
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
|
18
|
+
define_patch :version => '1.9.3', :patch=> 448 do
|
19
19
|
#World saving code
|
20
20
|
end
|
21
21
|
```
|
@@ -23,7 +23,7 @@ end
|
|
23
23
|
You can also combine the two, and have the patch execute only if ruby is beneath it's defined version, and have the message emitted as a Warning as well
|
24
24
|
|
25
25
|
```ruby
|
26
|
-
|
26
|
+
define_patch :version => '1.9.3', :patch=> 448, :message => "It's time to remove me, as you've upgraded to a version of ruby where this code is no longer needed" do
|
27
27
|
#There will be much scorn and derision when this bites someone in the ass
|
28
28
|
end
|
29
29
|
```
|
data/lib/patch_life.rb
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
module PatchLife
|
2
|
-
def define_patch_life(options={})
|
3
|
-
raise(ArgumentError, "
|
4
|
-
raise(ArgumentError, "
|
5
|
-
raise(ArgumentError, "
|
2
|
+
def define_patch_life(options={}, &blk)
|
3
|
+
raise(ArgumentError, "define_patch requires a :version argument to be set") unless options[:version]
|
4
|
+
raise(ArgumentError, "define_patch requires a :patch argument to be set") unless options[:patch]
|
5
|
+
raise(ArgumentError, "define_patch requires either a :message argument, a block to yield, or both") unless options[:message] || block_given?
|
6
6
|
|
7
|
-
|
7
|
+
past_due_version = Gem::Version.new(ruby_version) >= Gem::Version.new(options[:version])
|
8
|
+
past_due_patch = ruby_patch_level >= options[:patch].to_i
|
9
|
+
past_due = past_due_version && past_due_patch
|
10
|
+
|
8
11
|
Kernel.warn(options[:message]) if past_due && options[:message]
|
9
12
|
yield if !past_due && block_given?
|
10
13
|
#Don't want to return anything at all...
|
@@ -23,6 +26,6 @@ module PatchLife
|
|
23
26
|
module_function :ruby_patch_level
|
24
27
|
end
|
25
28
|
|
26
|
-
def
|
27
|
-
PatchLife.define_patch_life(options)
|
29
|
+
def define_patch(options={}, &blk)
|
30
|
+
PatchLife.define_patch_life(options, &blk)
|
28
31
|
end
|
data/lib/version.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe PatchLife do
|
4
4
|
before(:each) do
|
5
5
|
#Don't wanna actually fire it...
|
6
6
|
Kernel.stub(:warn)
|
@@ -11,23 +11,23 @@ describe PrintPatchWarning do
|
|
11
11
|
let (:patch) {1}
|
12
12
|
let (:message) {"You're outdated"}
|
13
13
|
it "requires a version to be delcared" do
|
14
|
-
expect {subject.
|
14
|
+
expect {subject.define_patch_life}.to raise_error(ArgumentError)
|
15
15
|
end
|
16
16
|
|
17
17
|
it "requires a patch level to be declared" do
|
18
|
-
expect {subject.
|
18
|
+
expect {subject.define_patch_life(:version=>version)}.to raise_error(ArgumentError)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "requires a message when no block has been yielded" do
|
22
|
-
expect {subject.
|
22
|
+
expect {subject.define_patch_life(:version=>version, :patch=>patch, :message=>message)}.not_to raise_error
|
23
23
|
end
|
24
24
|
|
25
25
|
it "requires a block when no message was declared" do
|
26
|
-
expect {subject.
|
26
|
+
expect {subject.define_patch_life(:version=>version, :patch=>patch) {nil} }.not_to raise_error
|
27
27
|
end
|
28
28
|
|
29
29
|
it "allows both a message and a block at the same time" do
|
30
|
-
expect {subject.
|
30
|
+
expect {subject.define_patch_life(:version=>version, :patch=>patch, :message=>message) {nil} }.not_to raise_error
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -39,12 +39,12 @@ describe PrintPatchWarning do
|
|
39
39
|
|
40
40
|
it "will not print a message if defined" do
|
41
41
|
Kernel.should_not_receive(:warn).with(message)
|
42
|
-
subject.
|
42
|
+
subject.define_patch_life(:version=>version, :patch=>patch, :message=>message) {nil}
|
43
43
|
end
|
44
44
|
|
45
45
|
it "will yield a block if given" do
|
46
46
|
block_called = false
|
47
|
-
subject.
|
47
|
+
subject.define_patch_life(:version=>version, :patch=>patch, :message=>message) {block_called=true}
|
48
48
|
block_called.should == true
|
49
49
|
end
|
50
50
|
end
|
@@ -56,12 +56,12 @@ describe PrintPatchWarning do
|
|
56
56
|
|
57
57
|
it "will print a message if defined" do
|
58
58
|
Kernel.should_receive(:warn).with(message)
|
59
|
-
subject.
|
59
|
+
subject.define_patch_life(:version=>version, :patch=>patch, :message=>message) {nil}
|
60
60
|
end
|
61
61
|
|
62
62
|
it "will not yield a block if given" do
|
63
63
|
block_called = false
|
64
|
-
subject.
|
64
|
+
subject.define_patch_life(:version=>version, :patch=>patch, :message=>message) {block_called=true}
|
65
65
|
block_called.should == false
|
66
66
|
end
|
67
67
|
end
|
@@ -73,12 +73,12 @@ describe PrintPatchWarning do
|
|
73
73
|
|
74
74
|
it "will print a message if defined" do
|
75
75
|
Kernel.should_receive(:warn).with(message)
|
76
|
-
subject.
|
76
|
+
subject.define_patch_life(:version=>version, :patch=>patch, :message=>message) {nil}
|
77
77
|
end
|
78
78
|
|
79
79
|
it "will not yield a block if given" do
|
80
80
|
block_called = false
|
81
|
-
subject.
|
81
|
+
subject.define_patch_life(:version=>version, :patch=>patch, :message=>message) {block_called=true}
|
82
82
|
block_called.should == false
|
83
83
|
end
|
84
84
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require 'rspec'
|
2
|
-
require '
|
2
|
+
require 'patch_life'
|