iron-extensions 1.1.3 → 1.1.4

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/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ == 1.1.4 / 2014-01-14
2
+
3
+ * Add Enumerable#delete_unless
4
+ * Ensure File#safe_replace removes file on thrown exceptions in block
5
+
1
6
  == 1.1.3 / 2013-09-04
2
7
 
3
8
  * Flesh out DslProxy#exec to allow specifying one or more yielded values
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.3
1
+ 1.1.4
@@ -10,4 +10,10 @@ module Enumerable
10
10
  hash
11
11
  end
12
12
 
13
+ # Inverse of delete_if, to be more Ruby-ish in cases where you want negated
14
+ # tests.
15
+ def delete_unless(&block)
16
+ delete_if {|*args| !block.call(*args)}
17
+ end
18
+
13
19
  end
@@ -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(99999).to_s
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,5 +1,5 @@
1
-
2
1
  class Range
2
+
3
3
  def bound(arg)
4
4
  if arg < min
5
5
  return min
@@ -9,5 +9,6 @@ class Range
9
9
  return arg
10
10
  end
11
11
  end
12
+
12
13
  end
13
14
 
@@ -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 lists' do
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 = Object.new
26
+ receiver = ControlBuilder.new
27
27
  DslProxy.exec(receiver) do
28
28
  respond_to?(:garbaz).should == false
29
- respond_to?(:dup).should == true
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.3
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: 2013-09-04 00:00:00.000000000 Z
12
+ date: 2014-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec