ztore 1.0.0

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.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Christian MICHON
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ # Ztore
2
+
3
+ This gem allows easy creation of in-memory hashes and persists them into a pstore file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ztore'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ztore
18
+
19
+ ## Usage
20
+
21
+ Use case 1: initial data creation
22
+
23
+ ~~~ ruby
24
+
25
+ require 'ztore'
26
+ db = Ztore::DB.new(:db) # => will create file 'db.pstore'
27
+ db.tree :People # this will derive a class 'People' from Ztore::Tree, usable as a Hash
28
+
29
+ People[0] = 'CM'
30
+ p People[0]
31
+
32
+ People[1] = { :name => 'CM', :features => { :developer => true } }
33
+ p People[1]
34
+
35
+ db.save # this is needed at the end of your script to store
36
+
37
+ ~~~
38
+
39
+ Use case 2: use a previously created Ztore::DB
40
+
41
+ ~~~ ruby
42
+
43
+ require 'ztore'
44
+ db = Ztore::DB.new(:db) # => will load file 'db.pstore' and recreate all Ztore::Tree
45
+
46
+ p People[0] # 'CM'
47
+ p People[1] # { :name => 'CM', :features => { :developer => true } }
48
+
49
+ ~~~
50
+
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create new Pull Request
@@ -0,0 +1,10 @@
1
+ %w[rubygems rake rake/testtask].map &method(:require)
2
+
3
+ desc 'Run tests'
4
+ Rake::TestTask.new 'test' do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => 'test'
@@ -0,0 +1,48 @@
1
+ %w[forwardable ztore/version pstore].map &method(:require)
2
+
3
+ module Ztore
4
+ module ClassMethods
5
+ extend Forwardable
6
+ def_delegators :@tree, *%w[
7
+ [] []= clear count each entries first key? keys last map select size value? values
8
+ ].map(&:to_sym)
9
+ def regexp(pattern)
10
+ re = Regexp.new "#{pattern}", Regexp::EXTENDED | Regexp::IGNORECASE
11
+ keys.select{ |k,v| "#{k}" =~ re }
12
+ end
13
+ end
14
+ class DB
15
+ def initialize(dbname=nil)
16
+ @pstore = PStore.new("#{dbname}.pstore")
17
+ @pstore.transaction do
18
+ @trees = @pstore.roots
19
+ @trees.map &method(:tree)
20
+ end
21
+ end
22
+ def tree(treename)
23
+ unless Object.const_defined?(treename)
24
+ Object.const_set treename, Class.new(Tree)
25
+ if @trees.include?(treename)
26
+ Object.const_get(treename).instance_variable_set :@tree, @pstore[treename]
27
+ else
28
+ Object.const_get(treename).instance_variable_set :@tree, Hash.new
29
+ @trees << treename
30
+ end
31
+ end
32
+ Object.const_get treename
33
+ end
34
+ def trees
35
+ @trees
36
+ end
37
+ def save
38
+ @pstore.transaction do
39
+ @trees.each do |treename|
40
+ @pstore[treename] = Object.const_get(treename).instance_variable_get :@tree
41
+ end
42
+ end
43
+ end
44
+ end
45
+ class Tree
46
+ extend ClassMethods
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Ztore
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ $: << File.expand_path('../lib', __FILE__)
2
+ %w[rubygems tempfile test/unit ztore].map &method(:require)
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ module MyTest
4
+ def runner(type)
5
+ db = @db
6
+ assert_instance_of Ztore::DB, db
7
+
8
+ db.tree :People
9
+ assert_equal Ztore::Tree, People.superclass
10
+ assert_equal [:People], db.trees
11
+ assert_equal [], People.entries
12
+
13
+ People[0] = 'CM'
14
+ People[1] = {
15
+ :name => 'CM',
16
+ :features => {
17
+ :developer => true
18
+ }
19
+ }
20
+
21
+ assert People.key?(0)
22
+ assert People.key?(1)
23
+ assert_equal [1], People.regexp('^1$')
24
+ assert_equal 2, People.size
25
+ assert_equal 2, People.count
26
+ assert_equal [0,1], People.keys
27
+
28
+ assert_instance_of String, People[0]
29
+ assert_equal 'CM', People[0]
30
+
31
+ assert_instance_of Hash, People[1]
32
+ assert_equal 'CM', People[1][:name]
33
+ assert_instance_of Hash, People[1][:features]
34
+ assert People[1][:features][:developer]
35
+
36
+ People.clear
37
+ assert_equal 0, People.count
38
+
39
+ Object.send(:remove_const, :People)
40
+ end
41
+ end
42
+
43
+ class ZtoreDBTest < Test::Unit::TestCase
44
+ include MyTest
45
+ def setup
46
+ @file = Tempfile.new('testdb')
47
+ end
48
+ def teardown
49
+ end
50
+ def test_ztore_db
51
+ @db = Ztore::DB.new(@file.path)
52
+ runner(:FileDB)
53
+ File.delete "#{@file.path}.pstore"
54
+ end
55
+ end
@@ -0,0 +1,17 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ztore/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'ztore'
7
+ spec.version = Ztore::VERSION
8
+ spec.authors = ['Christian MICHON']
9
+ spec.email = ['christian.michon@gmail.com']
10
+ spec.description = %q{This gem allows easy creation of in-memory hashes and persists them into a pstore file.}
11
+ spec.summary = %q{Easy persistence of hashes into pstore}
12
+ spec.homepage = 'http://github.com/cmichon/ztore'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = Dir['{**/*}']
16
+ spec.require_paths = ['lib']
17
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ztore
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Christian MICHON
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-12-04 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: This gem allows easy creation of in-memory hashes and persists them into a pstore file.
22
+ email:
23
+ - christian.michon@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/ztore/version.rb
32
+ - lib/ztore.rb
33
+ - LICENSE.txt
34
+ - Rakefile
35
+ - README.md
36
+ - test/test_helper.rb
37
+ - test/zstore_test.rb
38
+ - ztore.gemspec
39
+ homepage: http://github.com/cmichon/ztore
40
+ licenses:
41
+ - MIT
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ hash: 3
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ requirements: []
66
+
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Easy persistence of hashes into pstore
72
+ test_files: []
73
+