data-writer 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -0
- data/data-writer.gemspec +2 -2
- data/lib/data-writer.rb +57 -10
- metadata +2 -2
data/README.md
CHANGED
data/data-writer.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'data-writer'
|
3
|
-
s.version = '0.9.
|
4
|
-
s.date = '2012-07-
|
3
|
+
s.version = '0.9.1'
|
4
|
+
s.date = '2012-07-18'
|
5
5
|
s.summary = "Allows you to write to DATA"
|
6
6
|
s.description = "Normally you can only read from DATA but with data-writer you can also write to it. This allows you to easily persist data in a source file."
|
7
7
|
s.authors = ["Kalle Lindstrom"]
|
data/lib/data-writer.rb
CHANGED
@@ -12,20 +12,21 @@ class DATAWriter
|
|
12
12
|
|
13
13
|
#
|
14
14
|
# Factory method for DATA writers. Works simliar to File.new.
|
15
|
+
# mode can be both a string like "w" or a number like File::WRONLY
|
15
16
|
#
|
16
17
|
def self.file(mode, opt={})
|
17
18
|
check_DATA_defined # raises an exception if DATA is not defined.
|
18
19
|
data_path = File.expand_path(DATA.path)
|
19
20
|
|
20
|
-
if mode
|
21
|
-
|
22
|
-
mode.include?("b") ? m = "rb+" : m = "r+" # the actual mode will be rb+ or r+.
|
23
|
-
file = create_file(data_path, m, opt)
|
21
|
+
if mode.is_a?(String)
|
22
|
+
valid_mode = get_valid_string_mode(mode)
|
24
23
|
else
|
25
|
-
|
24
|
+
valid_mode = get_valid_int_mode(mode)
|
26
25
|
end
|
27
26
|
|
28
|
-
|
27
|
+
file = create_file(data_path, valid_mode, opt)
|
28
|
+
|
29
|
+
@data_start_pos = scan_data_pos
|
29
30
|
file.pos = @data_start_pos # sets the file pos to the line after __END__.
|
30
31
|
enhanced = enhance_file(file) # adds specialized methods for this object.
|
31
32
|
|
@@ -38,13 +39,59 @@ class DATAWriter
|
|
38
39
|
end
|
39
40
|
|
40
41
|
#
|
41
|
-
#
|
42
|
+
# If "w" was specifed as a mode we need to remove that (because it will truncate the whole file)
|
43
|
+
#
|
44
|
+
def self.get_valid_string_mode(mode)
|
45
|
+
if mode =~ /w/
|
46
|
+
clear_end # truncate after __END__ only.
|
47
|
+
valid_mode = 0
|
48
|
+
valid_mode |= mode.include?("+") ? File::RDWR : File::RDONLY
|
49
|
+
valid_mode |= File::BINARY if mode.include?("b")
|
50
|
+
valid_mode
|
51
|
+
else
|
52
|
+
mode
|
53
|
+
end
|
54
|
+
end
|
55
|
+
private_class_method :get_valid_string_mode
|
56
|
+
|
57
|
+
#
|
58
|
+
# Same as get_valid_string_mode but for the integer modes specifed in File::Constants.
|
59
|
+
#
|
60
|
+
def self.get_valid_int_mode(mode)
|
61
|
+
if (mode & File::TRUNC) == File::TRUNC # mode includes TRUNC
|
62
|
+
clear_end # truncate after __END__ only.
|
63
|
+
valid_mode = 0
|
64
|
+
File::Constants.constants.each do |const| # build new mode excluding TRUNC
|
65
|
+
value = File::const_get(const)
|
66
|
+
next if value.is_a?(String) # for example File::NULL
|
67
|
+
if (mode & value) == value && value != File::TRUNC
|
68
|
+
valid_mode |= value
|
69
|
+
end
|
70
|
+
end
|
71
|
+
valid_mode
|
72
|
+
else
|
73
|
+
mode
|
74
|
+
end
|
75
|
+
end
|
76
|
+
private_class_method :get_valid_int_mode
|
77
|
+
|
78
|
+
#
|
79
|
+
# Helper method to create a file that works in both 1.8 and 1.9 and different implementations.
|
42
80
|
#
|
43
81
|
def self.create_file(path, mode_string, opt = {})
|
44
|
-
if
|
45
|
-
|
82
|
+
if RUBY_PLATFORM =~ /java/i
|
83
|
+
if RUBY_VERSION =~ /1\.9\.3/
|
84
|
+
File.new(path, mode_string, opt) # Only JRuby 1.7 seem to implement this method the 1.9 way.
|
85
|
+
else
|
86
|
+
File.new(path, mode_string)
|
87
|
+
end
|
88
|
+
|
46
89
|
else
|
47
|
-
|
90
|
+
if RUBY_VERSION =~ /1\.9/
|
91
|
+
File.new(path, mode_string, opt)
|
92
|
+
else
|
93
|
+
File.new(path, mode_string)
|
94
|
+
end
|
48
95
|
end
|
49
96
|
end
|
50
97
|
private_class_method :create_file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data-writer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
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-07-
|
12
|
+
date: 2012-07-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|