object-stash 1.0.1

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 ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.1 / 2008-03-15
2
+
3
+ * First release
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/object-stash
6
+ lib/object-stash.rb
7
+ test/test_object-stash.rb
data/README.txt ADDED
@@ -0,0 +1,61 @@
1
+ = object-stash
2
+
3
+ * http://www.marcuswestin.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ "Permanently stash your Ruby objects"
8
+
9
+ Serializes your ruby objects and writes them to disk (gziped).
10
+
11
+ Easily load stashed object.
12
+
13
+ == FEATURES/PROBLEMS:
14
+
15
+ * Serializes a Ruby object
16
+ * Writes the serialization to disk
17
+ * Gzips written files automatically (optional)
18
+ * Reloads Ruby objects from files
19
+
20
+ == SYNOPSIS:
21
+
22
+ # Store
23
+ my_hash = { :data => 'My big hash' }
24
+ ObjectStash.store a_hash, './my_hash.stash'
25
+
26
+ # Reload and test
27
+ my_stashed_hash = ObjectStash.load './hash.stash
28
+ throw :Error unless my_hash == my_stashed_hash
29
+
30
+ == REQUIREMENTS:
31
+
32
+ * requires 'zlib', which is part of the standard library
33
+
34
+ == INSTALL:
35
+
36
+ * sudo gem install object-stash
37
+
38
+ == LICENSE:
39
+
40
+ (The MIT License)
41
+
42
+ Copyright (c) 2008 Marcus Westin, Marcus@MarcusWestin.com
43
+
44
+ Permission is hereby granted, free of charge, to any person obtaining
45
+ a copy of this software and associated documentation files (the
46
+ 'Software'), to deal in the Software without restriction, including
47
+ without limitation the rights to use, copy, modify, merge, publish,
48
+ distribute, sublicense, and/or sell copies of the Software, and to
49
+ permit persons to whom the Software is furnished to do so, subject to
50
+ the following conditions:
51
+
52
+ The above copyright notice and this permission notice shall be
53
+ included in all copies or substantial portions of the Software.
54
+
55
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
56
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
58
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
59
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
60
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
61
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/object-stash.rb'
6
+
7
+ Hoe.new('object-stash', ObjectStash::VERSION) do |p|
8
+ #p.rubyforge_name = 'object-stash' # if different than lowercase project name
9
+ p.developer('Marcus Westin', 'Marcus@MarcusWestin.com')
10
+ #p.remote_rdoc_dir = '' # Release to root
11
+ end
12
+
13
+ # vim: syntax=Ruby
data/bin/object-stash ADDED
File without changes
@@ -0,0 +1,79 @@
1
+ require 'zlib'
2
+
3
+ # Save any ruby object to disk!
4
+ # Objects are stored as gzipped marshal dumps.
5
+ #
6
+ # Example
7
+ #
8
+ # # First ruby process
9
+ # hash = {1 => "Entry1", 2 => "Entry2"}
10
+ # ObjectStash.sotre hash, 'hash.stash'
11
+ #
12
+ # ...
13
+ #
14
+ # # Another ruby process - same_hash will be identical to the original hash
15
+ # same_hash = ObjectStash.load 'hash.stash'
16
+ class ObjectStash
17
+ VERSION = '1.0.1'
18
+
19
+ # Store an object as a gzipped file to disk
20
+ #
21
+ # Example
22
+ #
23
+ # hash = {1 => "Entry1", 2 => "Entry2"}
24
+ # ObjectStore.store hash, 'hash.stash.gz'
25
+ # ObjectStore.store hash, 'hash.stash', :gzip => false
26
+ def self.store obj, file_name, options={}
27
+ marshal_dump = Marshal.dump(obj)
28
+ file = File.new(file_name,'w')
29
+ file = Zlib::GzipWriter.new(file) unless options[:gzip] == false
30
+ file.write marshal_dump
31
+ file.close
32
+ return obj
33
+ end
34
+
35
+ # Read a marshal dump from file and load it as an object
36
+ #
37
+ # Example
38
+ #
39
+ # hash = ObjectStore.get 'hash.dump.gz'
40
+ # hash_no_gzip = ObjectStore.get 'hash.dump.gz'
41
+ def self.load file_name
42
+ begin
43
+ file = Zlib::GzipReader.open(file_name)
44
+ rescue Zlib::GzipFile::Error
45
+ file = File.open(file_name, 'r')
46
+ ensure
47
+ obj = Marshal.load file.read
48
+ file.close
49
+ return obj
50
+ end
51
+ end
52
+ end
53
+
54
+ if $0 == __FILE__
55
+ require 'test/unit'
56
+ class TestObjectStash < Test::Unit::TestCase
57
+ @@tmp = '/tmp/TestObjectStash.stash'
58
+ def test_hash_store_load
59
+ hash1 = {:test=>'test'}
60
+ ObjectStash.store hash1, @@tmp
61
+ hash2 = ObjectStash.load @@tmp
62
+ assert hash1 == hash2
63
+ end
64
+ def test_hash_store_load_no_gzip
65
+ hash1 = {:test=>'test'}
66
+ ObjectStash.store hash1, @@tmp, :gzip => false
67
+ hash2 = ObjectStash.load @@tmp
68
+ assert hash1 == hash2
69
+ end
70
+ def test_self_stash
71
+ ObjectStash.store ObjectStash, @@tmp
72
+ assert ObjectStash == ObjectStash.load(@@tmp)
73
+ end
74
+ def test_self_stash_no_gzip
75
+ ObjectStash.store ObjectStash, @@tmp, :gzip => false
76
+ assert ObjectStash == ObjectStash.load(@@tmp)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,25 @@
1
+ require 'test/unit'
2
+ require 'object-stash'
3
+ class TestObjectStash < Test::Unit::TestCase
4
+ @@tmp = '/tmp/TestObjectStash.stash'
5
+ def test_hash_store_load
6
+ hash1 = {:test=>'test'}
7
+ ObjectStash.store hash1, @@tmp
8
+ hash2 = ObjectStash.load @@tmp
9
+ assert hash1 == hash2
10
+ end
11
+ def test_hash_store_load_no_gzip
12
+ hash1 = {:test=>'test'}
13
+ ObjectStash.store hash1, @@tmp, :gzip => false
14
+ hash2 = ObjectStash.load @@tmp
15
+ assert hash1 == hash2
16
+ end
17
+ def test_self_stash
18
+ ObjectStash.store ObjectStash, @@tmp
19
+ assert ObjectStash == ObjectStash.load(@@tmp)
20
+ end
21
+ def test_self_stash_no_gzip
22
+ ObjectStash.store ObjectStash, @@tmp, :gzip => false
23
+ assert ObjectStash == ObjectStash.load(@@tmp)
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: object-stash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcus Westin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-03-15 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.1
23
+ version:
24
+ description: "\"Permanently stash your Ruby objects\" Serializes your ruby objects and writes them to disk (gziped). Easily load stashed object."
25
+ email:
26
+ - Marcus@MarcusWestin.com
27
+ executables:
28
+ - object-stash
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - Manifest.txt
34
+ - README.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.txt
39
+ - Rakefile
40
+ - bin/object-stash
41
+ - lib/object-stash.rb
42
+ - test/test_object-stash.rb
43
+ has_rdoc: true
44
+ homepage: http://www.marcuswestin.com
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --main
48
+ - README.txt
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: object-stash
66
+ rubygems_version: 1.0.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: "\"Permanently stash your Ruby objects\" Serializes your ruby objects and writes them to disk (gziped)"
70
+ test_files:
71
+ - test/test_object-stash.rb