persistent 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ismael Celis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,78 @@
1
+ ## Basic persistence functionality for your Ruby objects
2
+
3
+ This is a drop-in thin persistence layer for your Ruby objects. I wrote it for some toy Sinatra apps I'm writing because I can't be bothered with using DataMapper, CouchDB and the rest for something so small.
4
+
5
+ The store can be anything that looks like a Hash.
6
+
7
+ From the code comments:
8
+
9
+ class Person
10
+ include Persistent
11
+ attr_accessor :name, :last_name
12
+ end
13
+
14
+ Person.store = Persistent::Store.new('/path/to/store_file.store') # or anything that looks like a Hash.
15
+
16
+ o = Person.new
17
+
18
+ o.name = 'Ismael'
19
+ o.last_name = 'Celis'
20
+
21
+ # == Save and namespace unique ID by class name
22
+
23
+ o.persist! 'some_unique_id'
24
+
25
+ o2 = Person.find( 'some_unique_id' )
26
+
27
+ o2.name # => 'Ismael'
28
+
29
+ o2 == o # => true
30
+
31
+ o.destroy!
32
+
33
+ Person.find( 'some_unique_id' ) # => nil
34
+
35
+
36
+ ## Usage
37
+
38
+ You can persist individual objects by key
39
+
40
+ o = SomeObject.new
41
+ o.persist! 'some_key'
42
+
43
+ Keys are internally prefixed with the object's class name so you can store objects of different types using the same keys.
44
+
45
+ ... But after conversation with Martyn (http://github.com/mloughran) I'm planning to store one big object with the whole domain object space in it, so I can store the whole thing at once and forget about persistance and have fun with pure Ruby objects (look ma, no database!).
46
+
47
+ I know. That only works because my particular domain is tiny.
48
+
49
+
50
+ ## Object store
51
+
52
+ Persistent::Store wraps Standard library's PStore to save objects to the filesystem in a transaction.
53
+
54
+ Persistent::Store itself quacks like a Hash (:[], :[]=, :fetch, :delete and :each) so you can use a Hash, Memcached, another wrapper like Moneta (http://github.com/wycats/moneta/tree/master) or you own object store.
55
+
56
+ ## Installation
57
+
58
+ This is so tiny that you can just copy the lib/persistent.rb and lib/persistent/store.rb files in you project (or clone them of course).
59
+
60
+ You can also use it as a gem:
61
+
62
+ sudo gem install ismasan-persistent
63
+
64
+ And then in your code:
65
+
66
+ require 'rubygems'
67
+ require 'persistent'
68
+
69
+ class SomeClass
70
+ include Persistent
71
+ ...
72
+ end
73
+
74
+ ## Other alternatives
75
+
76
+ This library is meant for simple use cases. If you want serious object persistence please check solutions such as CouchDB (http://couchdb.apache.org/), DataMapper (http://github.com/datamapper/dm-core/tree/master) or the similarly named Persistable gem (http://github.com/andykent/persistable/tree/master).
77
+
78
+ Also, Stone (http://github.com/ndemonner/stone/tree/master) looks pretty neat!
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "persistent"
8
+ gem.summary = %Q{TODO: one-line summary of your gem}
9
+ gem.description = %Q{TODO: longer description of your gem}
10
+ gem.email = "ismaelct@gmail.com"
11
+ gem.homepage = "http://github.com/ismasan/persistent"
12
+ gem.authors = ["Ismael Celis"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ if File.exist?('VERSION.yml')
40
+ config = YAML.load(File.read('VERSION.yml'))
41
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
42
+ else
43
+ version = ""
44
+ end
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "persistent #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
51
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/persistent.rb ADDED
@@ -0,0 +1,88 @@
1
+ module Persistent
2
+
3
+ autoload :Store, 'lib/persistent/store'
4
+
5
+ # This module gives you objects:
6
+ #
7
+ # class Person
8
+ # include Persistent
9
+ #
10
+ # attr_accessor :name, :last_name
11
+ # end
12
+ #
13
+ # Person.store = Persistent::Store.new('/path/to/store_file.store') # or anything that looks like a Hash.
14
+ #
15
+ # o = Person.new
16
+ #
17
+ # o.name = 'Ismael'
18
+ # o.last_name = 'Celis'
19
+ #
20
+ # # == Save and namespace unique ID by class name
21
+ #
22
+ # o.persist! 'some_unique_id'
23
+ #
24
+ # o2 = Person.find( 'some_unique_id' )
25
+ #
26
+ # o2.name # => 'Ismael'
27
+ #
28
+ # o2 == o # => true
29
+ #
30
+ # o.destroy!
31
+ #
32
+ def self.included(base)
33
+ base.extend ClassMethods
34
+ class << base
35
+ attr_accessor :store
36
+ end
37
+
38
+ # The only reason an instance wants to know about their persistence id is so we can call destroy! on it.
39
+ # Why not use auto-generated ID's? Because I might want to use them as URL permalinks, for example
40
+ #
41
+ base.class_eval do
42
+ attr_accessor :_persisted_id
43
+ end
44
+ end
45
+
46
+ def persist!(id)
47
+ self._persisted_id = id
48
+ store[persistent_key(id)] = self
49
+ end
50
+
51
+ def destroy!
52
+ return false unless self._persisted_id
53
+ store.delete(persistent_key(_persisted_id))
54
+ end
55
+
56
+ protected
57
+
58
+ def store
59
+ self.class.store
60
+ end
61
+
62
+ def persistent_key(id)
63
+ self.class.persistent_key(id)
64
+ end
65
+
66
+ # Class methods ::::::::::::::::::::::::::::::::::::::::::::
67
+ #
68
+ module ClassMethods
69
+
70
+ def find(id)
71
+ o = store[persistent_key(id)]
72
+ o._persisted_id = id
73
+ o
74
+ end
75
+
76
+ def exists?(id)
77
+ !store[persistent_key(id)].nil?
78
+ end
79
+
80
+ # 'Person/abc'
81
+ #
82
+ def persistent_key(id)
83
+ File.join(self.name.to_s, id.to_s)
84
+ end
85
+
86
+ end
87
+
88
+ end
@@ -0,0 +1,31 @@
1
+ require 'pstore'
2
+ module Persistent
3
+ class Store
4
+ include Enumerable
5
+
6
+ def initialize(key)
7
+ @store = PStore.new(key)
8
+ end
9
+
10
+ def [](key)
11
+ @store.transaction{@store[key]}
12
+ end
13
+
14
+ def []=(key, value)
15
+ @store.transaction{@store[key] = value}
16
+ end
17
+
18
+ def fetch(key, default)
19
+ @store.transaction{@store.fetch(key, default)}
20
+ end
21
+
22
+ def delete(key)
23
+ @store.transaction{@store.delete(key)}
24
+ end
25
+
26
+ def each(&block)
27
+ roots = @store.transaction{@store.roots}
28
+ roots.each(&block)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{persistent}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ismael Celis"]
12
+ s.date = %q{2009-10-05}
13
+ s.description = %q{Tiny plug & play persistence layer for your Ruby objects}
14
+ s.email = %q{ismaelct@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/persistent.rb",
27
+ "lib/persistent/store.rb",
28
+ "persistent.gemspec",
29
+ "spec/persistent_spec.rb",
30
+ "spec/spec_helper.rb",
31
+ "spec/store_spec.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/ismasan/persistent}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Tiny plug & play persistence layer for your Ruby objects}
38
+ s.test_files = [
39
+ "spec/persistent_spec.rb",
40
+ "spec/spec_helper.rb",
41
+ "spec/store_spec.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ else
50
+ end
51
+ else
52
+ end
53
+ end
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ class PersistentPerson
4
+ include Persistent
5
+
6
+ include Comparable
7
+
8
+ attr_accessor :name, :last_name, :email
9
+
10
+ def initialize(name, last_name, email)
11
+ @name, @last_name, @meail = name, last_name, email
12
+ end
13
+
14
+
15
+ def <=>(other)
16
+ self.name <=> other.name
17
+ end
18
+ end
19
+
20
+ PersistentPerson.store = Hash.new # Persistent::Store.new('test.store')
21
+
22
+ describe Persistent do
23
+
24
+ before do
25
+ @person = PersistentPerson.new('ismael', 'celis', nil)
26
+ end
27
+
28
+ describe 'saving' do
29
+ it "should persists" do
30
+ @person.name = 'foo'
31
+ @person.last_name = 'bar'
32
+ @person.persist! 'abc'
33
+
34
+ p2 = PersistentPerson.find('abc')
35
+ p2.should == @person
36
+ p2.name.should == 'foo'
37
+ p2.last_name.should == 'bar'
38
+ p2.email.should be_nil
39
+ end
40
+
41
+ it "should check it exists" do
42
+ @person.persist! 'abc'
43
+ PersistentPerson.exists?('abc').should be_true
44
+ PersistentPerson.exists?('zzz').should_not be_true
45
+ end
46
+
47
+ it "should destroy" do
48
+ @person.persist! 'abc'
49
+ PersistentPerson.exists?('abc').should be_true
50
+ @person.destroy!
51
+ PersistentPerson.exists?('abc').should be_false
52
+ end
53
+ end
54
+
55
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'persistent'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Persistent::Store do
4
+
5
+ before do
6
+ @file_path = File.dirname(__FILE__)+'/test.store'
7
+ @store = Persistent::Store.new(@file_path)
8
+ end
9
+
10
+ describe "hash interface" do
11
+ %w([] []= fetch delete each).each do |m|
12
+ it "should respond to :#{m}" do
13
+ @store.respond_to?(m.to_sym).should be_true
14
+ end
15
+ end
16
+ end
17
+
18
+ describe 'setting' do
19
+
20
+ after do
21
+ FileUtils.rm(@file_path) if File.exists?(@file_path)
22
+ end
23
+
24
+ it "should persist data" do
25
+ @store[:foo] = 'bar'
26
+ second_store = Persistent::Store.new(@file_path)
27
+ second_store[:foo].should == 'bar'
28
+ end
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: persistent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ismael Celis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-05 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Tiny plug & play persistence layer for your Ruby objects
17
+ email: ismaelct@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.markdown
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.markdown
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/persistent.rb
33
+ - lib/persistent/store.rb
34
+ - persistent.gemspec
35
+ - spec/persistent_spec.rb
36
+ - spec/spec_helper.rb
37
+ - spec/store_spec.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/ismasan/persistent
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Tiny plug & play persistence layer for your Ruby objects
66
+ test_files:
67
+ - spec/persistent_spec.rb
68
+ - spec/spec_helper.rb
69
+ - spec/store_spec.rb