iron-extensions 1.1.3 → 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/README.rdoc +4 -0
- data/Version.txt +1 -1
- data/lib/iron/extensions/enumerable.rb +6 -0
- data/lib/iron/extensions/file.rb +11 -3
- data/lib/iron/extensions/range.rb +2 -1
- data/spec/extensions/array_spec.rb +2 -2
- data/spec/extensions/dsl_proxy_spec.rb +2 -2
- data/spec/extensions/enumerable_spec.rb +13 -1
- metadata +2 -2
data/History.txt
CHANGED
data/README.rdoc
CHANGED
@@ -40,6 +40,10 @@ Helpful extensions to core Ruby classes, plus a little sugar for common patterns
|
|
40
40
|
|
41
41
|
[:frog, :pig].to_hash {|n| n.to_s.capitalize} # => {:frog => 'Frog', :pig => 'Pig'}
|
42
42
|
[:frog, :pig].to_hash(nil) # => {:frog => nil, :pig => nil}
|
43
|
+
|
44
|
+
* Enumerable#delete_unless - equivalent to delete_if but with inverted test
|
45
|
+
|
46
|
+
[1,2,3,4,5].delete_unless(&:odd?) # => [1,3,5]
|
43
47
|
|
44
48
|
* File.replace - atomic replacement of a file given a block to generate it
|
45
49
|
|
data/Version.txt
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.4
|
data/lib/iron/extensions/file.rb
CHANGED
@@ -2,15 +2,17 @@ require 'fileutils'
|
|
2
2
|
|
3
3
|
class File
|
4
4
|
|
5
|
-
# Atomic replace code - write to temp file, rename to source file path
|
5
|
+
# Atomic replace code - write to temp file, rename to source file path only after
|
6
|
+
# successful completion of block. Prevents partial writes from overwriting files.
|
7
|
+
#
|
6
8
|
# Usage:
|
7
9
|
#
|
8
10
|
# File.safe_replace('/etc/foo.conf') do |file|
|
9
11
|
# file.write('bob=1234')
|
10
12
|
# end
|
11
|
-
def self.safe_replace(path, perm=nil) # :yields: file
|
13
|
+
def self.safe_replace(path, perm = nil) # :yields: file
|
12
14
|
begin
|
13
|
-
tmp_path = path + '.tmp' + Kernel.rand(
|
15
|
+
tmp_path = path + '.tmp' + Kernel.rand(999999).to_s
|
14
16
|
end while File.exist?(tmp_path)
|
15
17
|
|
16
18
|
file = File.open(tmp_path, 'w', perm)
|
@@ -18,6 +20,12 @@ class File
|
|
18
20
|
file.close
|
19
21
|
|
20
22
|
FileUtils.mv(tmp_path, path)
|
23
|
+
ensure
|
24
|
+
# Close file if needed
|
25
|
+
file.close unless file.closed?
|
26
|
+
|
27
|
+
# Clean up temp file
|
28
|
+
File.unlink(tmp_path) if File.exists?(tmp_path)
|
21
29
|
end
|
22
30
|
|
23
31
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
describe Array do
|
2
2
|
|
3
3
|
it 'should join lists ignoring blank entries' do
|
4
|
-
['word', nil, '', 'end'].list_join('-').should == 'word-end'
|
4
|
+
['word', nil, '', 'end', ''].list_join('-').should == 'word-end'
|
5
5
|
end
|
6
6
|
|
7
|
-
it 'should handle empty arrays while joining
|
7
|
+
it 'should handle empty arrays while joining as a list' do
|
8
8
|
[].list_join.should == ''
|
9
9
|
end
|
10
10
|
|
@@ -23,10 +23,10 @@ describe DslProxy do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it 'should proxy respond_to? to the receiver' do
|
26
|
-
receiver =
|
26
|
+
receiver = ControlBuilder.new
|
27
27
|
DslProxy.exec(receiver) do
|
28
28
|
respond_to?(:garbaz).should == false
|
29
|
-
respond_to?(:
|
29
|
+
respond_to?(:button).should == true
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -5,12 +5,24 @@ describe Enumerable do
|
|
5
5
|
[1,2,3].convert_to_hash.should == {1 => nil, 2 => nil, 3 => nil}
|
6
6
|
end
|
7
7
|
|
8
|
-
it 'should accept a default value' do
|
8
|
+
it 'should accept a default hash value' do
|
9
9
|
[:a, :b].convert_to_hash(false).should == {:a => false, :b => false}
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should accept a block to set hash values' do
|
13
13
|
[:a, :b, :c].convert_to_hash {|k| k.to_s.upcase}.should == {:a => 'A', :b => 'B', :c => 'C'}
|
14
14
|
end
|
15
|
+
|
16
|
+
it 'should delete_unless' do
|
17
|
+
a = [1,2,3,9,22]
|
18
|
+
a.delete_unless(&:odd?)
|
19
|
+
a.should == [1,3,9]
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should delete_unless when a hash' do
|
23
|
+
h = {:a => 2, :b => 5, :c => 12}
|
24
|
+
h.delete_unless {|k, v| v == 12}
|
25
|
+
h.should == {:c => 12}
|
26
|
+
end
|
15
27
|
|
16
28
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iron-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.4
|
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:
|
12
|
+
date: 2014-01-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|