marshal_file 0.0.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/lib/marshal_file.rb +106 -0
- metadata +46 -0
data/lib/marshal_file.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# == Marshal File Writing and Reading
|
2
|
+
# ---
|
3
|
+
# ==== Write Example:
|
4
|
+
# a = {1=>2, 3=>4}
|
5
|
+
# c = {5=>6, 7=>8}
|
6
|
+
# mf = SeqFile.new("test.marshal","write")
|
7
|
+
# mf.write(a)
|
8
|
+
# mf.write(c)
|
9
|
+
# mf.close
|
10
|
+
#
|
11
|
+
# ==== Read Example:
|
12
|
+
# mf = SeqFile.new("test.marshal")
|
13
|
+
# while !mf.eof
|
14
|
+
# puts mf.read
|
15
|
+
# end
|
16
|
+
# mf.close
|
17
|
+
#
|
18
|
+
|
19
|
+
class MarshalFile
|
20
|
+
|
21
|
+
# ==== initialize parameters:
|
22
|
+
# filename = name of file
|
23
|
+
# mode = "read" (default), "write", "append"
|
24
|
+
|
25
|
+
def initialize(filename = nil,mode="read")
|
26
|
+
@filename = filename
|
27
|
+
if @filename == nil
|
28
|
+
@filename = File.join(Rails.root, 'log', 'NoName-'+Time.now.localtime.strftime("%Y-%m-%d")+".marshal")
|
29
|
+
end
|
30
|
+
@open = true
|
31
|
+
if mode == "read"
|
32
|
+
if FileTest::exist?(@filename)
|
33
|
+
@file = File.open(@filename, 'rb')
|
34
|
+
else
|
35
|
+
@open = false
|
36
|
+
puts @filename + " not found"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
if mode == "write"
|
40
|
+
@file = File.open(@filename, 'wb')
|
41
|
+
end
|
42
|
+
if mode == "append"
|
43
|
+
@file = File.open(@filename, 'ab')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# check for eof
|
48
|
+
# while !mf.eof
|
49
|
+
# puts mf.read
|
50
|
+
# end
|
51
|
+
|
52
|
+
def eof
|
53
|
+
if @open
|
54
|
+
return @file.eof?
|
55
|
+
else
|
56
|
+
puts "file not open for reading"
|
57
|
+
return false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# read from marshal file
|
62
|
+
# must be opened in <b>read</b> mode
|
63
|
+
|
64
|
+
def read
|
65
|
+
if @open
|
66
|
+
return Marshal.load(@file)
|
67
|
+
else
|
68
|
+
puts "file not open for reading"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# write to marshal file
|
73
|
+
# must be opened in <b>append</b> or <b>write</b> mode
|
74
|
+
|
75
|
+
def write(val)
|
76
|
+
if @open
|
77
|
+
Marshal.dump(val,@file)
|
78
|
+
else
|
79
|
+
puts "file not open for writing"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
# close the marshal file
|
84
|
+
|
85
|
+
|
86
|
+
def close
|
87
|
+
if @open
|
88
|
+
@file.close
|
89
|
+
@open = false
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# delete the marshal file.
|
94
|
+
|
95
|
+
def delete
|
96
|
+
if @open
|
97
|
+
close
|
98
|
+
end
|
99
|
+
if FileTest::exist?(@filename)
|
100
|
+
File.delete(@filename)
|
101
|
+
else
|
102
|
+
puts @filename + " does not exist"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marshal_file
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stephen Foley
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-08-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Simple Reading and Writing of Marshal Files
|
15
|
+
email: stephen@talaw.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/marshal_file.rb
|
21
|
+
homepage: http://rubygems.org/gems/marshal_file
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.25
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Read/Write Marshal Files
|
46
|
+
test_files: []
|