spruz 0.1.1 → 0.1.2

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -19,4 +19,5 @@ module Spruz
19
19
  require 'spruz/time_dummy'
20
20
  require 'spruz/to_proc'
21
21
  require 'spruz/uniq_by'
22
+ require 'spruz/write'
22
23
  end
@@ -1,6 +1,6 @@
1
1
  module Spruz
2
2
  # Spruz version
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -0,0 +1,20 @@
1
+ module Spruz
2
+ module Write
3
+ # Write to a file atomically
4
+ def write(filename, content = nil, mode = 'w')
5
+ temp = File.new(filename + ".tmp.#$$.#{Time.now.to_f}", mode)
6
+ if content.nil? and block_given?
7
+ yield temp
8
+ elsif !content.nil?
9
+ temp.write content
10
+ else
11
+ raise ArgumentError, "either content or block argument required"
12
+ end
13
+ temp.close
14
+ File.rename temp.path, filename
15
+ ensure
16
+ temp and !temp.closed? and temp.close
17
+ File.file?(temp.path) and File.unlink temp.path
18
+ end
19
+ end
20
+ end
@@ -12,4 +12,5 @@ module Spruz
12
12
  require 'spruz/xt/uniq_by'
13
13
  require 'spruz/xt/p'
14
14
  require 'spruz/xt/symbol_to_proc'
15
+ require 'spruz/xt/write'
15
16
  end
@@ -0,0 +1,11 @@
1
+ require 'spruz/write'
2
+
3
+ module Spruz
4
+ class ::Object
5
+ include Spruz::Write
6
+ end
7
+
8
+ class ::IO
9
+ extend Spruz::Write
10
+ end
11
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Frank
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-18 00:00:00 +02:00
17
+ date: 2010-06-14 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -46,6 +46,7 @@ files:
46
46
  - lib/spruz/bijection.rb
47
47
  - lib/spruz/module_group.rb
48
48
  - lib/spruz/memoize.rb
49
+ - lib/spruz/write.rb
49
50
  - lib/spruz/generator.rb
50
51
  - lib/spruz/hash_union.rb
51
52
  - lib/spruz/version.rb
@@ -61,6 +62,7 @@ files:
61
62
  - lib/spruz/xt/p.rb
62
63
  - lib/spruz/xt/blank.rb
63
64
  - lib/spruz/xt/symbol_to_proc.rb
65
+ - lib/spruz/xt/write.rb
64
66
  - lib/spruz/xt/hash_union.rb
65
67
  - lib/spruz/xt/full.rb
66
68
  - lib/spruz/xt/irb.rb