oj-pstore 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ae69d1e88086797ba1ed8e5d343e3a32d67d305
4
+ data.tar.gz: 18ce4d169b36abdd033514b771b0753dde8ca2f9
5
+ SHA512:
6
+ metadata.gz: 9c8f5f0843c396f5c70fec9f6ecf9a48f96b3fb28408e11663eb2b95d43941c673a2591be5e9aff5e155b941eb8ee37aba4a490390b140ee59d3479ac0c59a59
7
+ data.tar.gz: 37d6a78e3d7e89c814c64c6b0c06bbe26aa9bc514ca68a57bd27778770b5dd1ef56c1fcf8394ae0a9a5d137c81e53c9408b3398ae75006efaf0b1e39eddd6281
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ *.old
3
+ *.lock
4
+ local/
5
+ tmp/
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ ## oj-pstore gem
2
+
3
+ This gem implements a [PStore](http://ruby-doc.org/stdlib-2.0/libdoc/pstore/rdoc/PStore.html) alternative using the fast [Oj](https://github.com/ohler55/oj#readme) gem.
4
+
5
+ ## Installation
6
+
7
+ gem install oj-pstore
8
+
9
+ ## Documentation
10
+
11
+ The gem supports the [PStore](http://ruby-doc.org/stdlib-2.0/libdoc/pstore/rdoc/PStore.html) API.
12
+
@@ -0,0 +1,5 @@
1
+ module Oj
2
+ class Store
3
+ VERSION= '0.0.2'
4
+ end
5
+ end
data/lib/oj/store.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'pstore'
2
+ require 'oj'
3
+
4
+ class Oj::Store < ::PStore
5
+
6
+ attr_reader :obtions
7
+
8
+ def initialize(file_name, options= {})
9
+ super(file_name, options.fetch(:thread_safe, false))
10
+ @options= options
11
+ end
12
+
13
+ def dump(obj)
14
+ Oj.dump(obj, @options)
15
+ end
16
+
17
+ def load(content)
18
+ content.size.zero? ? {} : Oj.object_load(content, @options)
19
+ end
20
+
21
+ def marshal_dump_supports_canonical_option?
22
+ false
23
+ end
24
+
25
+ def empty_marshal_data
26
+ EMPTY_MARSHAL_DATA
27
+ end
28
+
29
+ def empty_marshal_checksum
30
+ EMPTY_MARSHAL_CHECKSUM
31
+ end
32
+
33
+ private
34
+
35
+ EMPTY_MARSHAL_DATA = Oj.dump({}).freeze
36
+ EMPTY_MARSHAL_CHECKSUM = Digest::MD5.digest(EMPTY_MARSHAL_DATA)
37
+
38
+ autoload(:VERSION, File.expand_path('../store/version', __FILE__))
39
+
40
+ end
data/oj-pstore.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../lib/oj/store/version', __FILE__)
2
+
3
+ GemSpec= Gem::Specification.new do |spec|
4
+ spec.required_rubygems_version= Gem::Requirement.new('>= 1.3.6')
5
+ spec.name= 'oj-pstore'
6
+ spec.version= Oj::Store::VERSION
7
+ spec.date= Time.now.strftime('%Y-%m-%d')
8
+ spec.authors= ['Mike Blumtritt']
9
+ # spec.email= ''
10
+ spec.summary= 'PStore based on fast JSON parser/marshaller Oj.'
11
+ spec.description= 'This gem implements a PStore alternative using the very fast Oj gem.'
12
+ spec.homepage= 'https://github.com/mblumtritt/oj-pstore'
13
+ spec.platform= Gem::Platform::RUBY
14
+ spec.required_ruby_version= '>= 1.9.3'
15
+ spec.add_dependency 'oj'
16
+ spec.add_development_dependency 'minitest'
17
+ spec.has_rdoc= false # TODO: we need a RDOC!
18
+ spec.extra_rdoc_files= ['README.md']
19
+ spec.require_paths = ['lib']
20
+ spec.files = `git ls-files`.split("\n")
21
+ spec.test_files = `git ls-files -- spec/*`.split("\n")
22
+ spec.rubyforge_project= spec.name
23
+ end
@@ -0,0 +1,150 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/oj/store'
3
+
4
+ class OjStoreTest < Minitest::Test
5
+
6
+ class SampleData
7
+ attr_accessor :name, :number
8
+ end
9
+
10
+ def setup
11
+ @store_file = "oj-store.tmp.#{Process.pid}"
12
+ @store_file2 = "oj-store.2.tmp.#{Process.pid}"
13
+ @store = Oj::Store.new(@store_file)
14
+ end
15
+
16
+ def teardown
17
+ File.file?(@store_file) and File.unlink(@store_file) rescue nil
18
+ File.file?(@store_file2) and File.unlink(@store_file2) rescue nil
19
+ end
20
+
21
+ def test_opening_new_file_in_readonly_mode_should_result_in_empty_values
22
+ @store.transaction(true) do
23
+ assert_nil @store[:foo]
24
+ assert_nil @store[:bar]
25
+ end
26
+ end
27
+
28
+ def test_opening_new_file_in_readwrite_mode_should_result_in_empty_values
29
+ @store.transaction do
30
+ assert_nil @store[:foo]
31
+ assert_nil @store[:bar]
32
+ end
33
+ end
34
+
35
+ def test_data_should_be_loaded_correctly_when_in_readonly_mode
36
+ @store.transaction do
37
+ @store[:foo] = 'bar'
38
+ end
39
+ @store.transaction(true) do
40
+ assert_equal 'bar', @store[:foo]
41
+ end
42
+ end
43
+
44
+ def test_data_should_be_loaded_correctly_when_in_readwrite_mode
45
+ @store.transaction do
46
+ @store[:foo] = 'bar'
47
+ end
48
+ @store.transaction do
49
+ assert_equal 'bar', @store[:foo]
50
+ end
51
+ end
52
+
53
+ def test_changes_after_commit_are_discarded
54
+ @store.transaction do
55
+ @store[:foo] = 'bar'
56
+ @store.commit
57
+ @store[:foo] = 'baz'
58
+ end
59
+ @store.transaction(true) do
60
+ assert_equal 'bar', @store[:foo]
61
+ end
62
+ end
63
+
64
+ def test_changes_are_not_written_on_abort
65
+ @store.transaction do
66
+ @store[:not_written] = 'just a value'
67
+ @store.abort
68
+ end
69
+ @store.transaction(true) do
70
+ assert_nil @store[:not_written]
71
+ end
72
+ end
73
+
74
+ def test_writing_inside_readonly_transaction_raises_error
75
+ assert_raises(PStore::Error) do
76
+ @store.transaction(true) do
77
+ @store[:foo] = 'bar'
78
+ end
79
+ end
80
+ end
81
+
82
+ def test_thread_safe
83
+ assert_raises(PStore::Error) do
84
+ flag = false
85
+ Thread.new do
86
+ @store.transaction do
87
+ @store[:foo] = 'bar'
88
+ flag = true
89
+ sleep 1
90
+ end
91
+ end
92
+ until flag
93
+ # nop
94
+ end
95
+ @store.transaction {}
96
+ end
97
+ begin
98
+ pstore = PStore.new(@store_file2,true)
99
+ flag = false
100
+ Thread.new do
101
+ pstore.transaction do
102
+ pstore[:foo] = 'bar'
103
+ flag = true
104
+ sleep 1
105
+ end
106
+ end
107
+ until flag
108
+ # nop
109
+ end
110
+ pstore.transaction do
111
+ assert(pstore[:foo] == 'bar')
112
+ end
113
+ end
114
+ end
115
+
116
+ def test_nested_transaction_raises_error
117
+ assert_raises(PStore::Error) do
118
+ @store.transaction { @store.transaction { } }
119
+ end
120
+ pstore = PStore.new(@store_file2, true)
121
+ assert_raises(PStore::Error) do
122
+ pstore.transaction { pstore.transaction { } }
123
+ end
124
+ end
125
+
126
+ def test_extended_data
127
+ array= [1,2,3,4,'a','b','c']
128
+ hash= {a: 42, b: :c, d: 'e', 'f' => 4711}
129
+ s= SampleData.new
130
+ s.name = 'a name'
131
+ s.number = 12345678
132
+ @store.transaction do
133
+ @store[:string] = 'a simple string'
134
+ @store[:number] = 42.21
135
+ @store[:array] = array
136
+ @store[:hash] = hash
137
+ @store[:sample] = s
138
+ end
139
+ @store.transaction(true) do
140
+ assert_equal 'a simple string', @store[:string]
141
+ assert_equal 42.21, @store[:number]
142
+ assert_equal array, @store[:array]
143
+ assert_equal hash, @store[:hash]
144
+ assert_equal s.class, @store[:sample].class
145
+ assert_equal s.name, @store[:sample].name
146
+ assert_equal s.number, @store[:sample].number
147
+ end
148
+
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oj-pstore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Mike Blumtritt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: This gem implements a PStore alternative using the very fast Oj gem.
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files:
46
+ - README.md
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - README.md
51
+ - lib/oj/store.rb
52
+ - lib/oj/store/version.rb
53
+ - oj-pstore.gemspec
54
+ - test/test_oj_store.rb
55
+ homepage: https://github.com/mblumtritt/oj-pstore
56
+ licenses: []
57
+ metadata: {}
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: 1.9.3
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - '>='
70
+ - !ruby/object:Gem::Version
71
+ version: 1.3.6
72
+ requirements: []
73
+ rubyforge_project: oj-pstore
74
+ rubygems_version: 2.0.14
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: PStore based on fast JSON parser/marshaller Oj.
78
+ test_files: []