shadydb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.markdown +60 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/lib/shadydb/attributes.rb +34 -0
- data/lib/shadydb/configuration.rb +33 -0
- data/lib/shadydb/document.rb +40 -0
- data/lib/shadydb/extensions/string/helpers.rb +21 -0
- data/lib/shadydb/extensions.rb +5 -0
- data/lib/shadydb/finders.rb +27 -0
- data/lib/shadydb/persistence.rb +117 -0
- data/lib/shadydb.rb +9 -0
- data/shadydb.gemspec +75 -0
- data/test/attributes_test.rb +35 -0
- data/test/configuration_test.rb +31 -0
- data/test/document_test.rb +9 -0
- data/test/finders_test.rb +16 -0
- data/test/helper.rb +23 -0
- data/test/persistence_test.rb +64 -0
- metadata +129 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 jdpace
|
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,60 @@
|
|
1
|
+
# ShadyDB
|
2
|
+
|
3
|
+
ShadyDB is a document persistence layer that speaks ActiveModel.
|
4
|
+
Like it's name says it is _shady_ and is **merely impersonating a database**.
|
5
|
+
Its saves records/documents to the file system as XML or JSON files.
|
6
|
+
|
7
|
+
In general you should use a traditional database for 99.999% of cases. Real databases
|
8
|
+
handle this type of thing way better. If you think ShadyDB is really the way to
|
9
|
+
go, think again because you're probably mistaken. ShadyDB is just a ActiveRecord like
|
10
|
+
wrapper for the file system and lacks traditional data integrity schemes.
|
11
|
+
|
12
|
+
ShadyDB is built on top of ActiveModel and as a result is a great learning tool for
|
13
|
+
understanding the modularity and power that ActiveModel brings. I highly encourage
|
14
|
+
anyone to spend the time to write their own ORM.
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
class User < ShadyDB::Document
|
19
|
+
end
|
20
|
+
|
21
|
+
u = User.new(:name => 'Marshall Mathers')
|
22
|
+
u.new_record? # true
|
23
|
+
u.save # true
|
24
|
+
u.new_record? # false
|
25
|
+
|
26
|
+
u.name # 'Marshall Mathers'
|
27
|
+
u[:name] # 'Marshall Mathers'
|
28
|
+
|
29
|
+
u = User.create(:name => 'Eminem')
|
30
|
+
u.new_record? # false
|
31
|
+
|
32
|
+
|
33
|
+
## Configuration
|
34
|
+
|
35
|
+
ShadyDB.configure do |config|
|
36
|
+
config.data_directory = Rails.root.join('db','shadydb')
|
37
|
+
config.format = :xml
|
38
|
+
end
|
39
|
+
|
40
|
+
## TODO
|
41
|
+
|
42
|
+
* Add an optional encryption layer
|
43
|
+
* Add support for collection finders
|
44
|
+
* Add support for conditional finders
|
45
|
+
* Add simple indexing
|
46
|
+
* Add defined fields a la Mongoid
|
47
|
+
|
48
|
+
## Note on Patches/Pull Requests
|
49
|
+
|
50
|
+
* Fork the project.
|
51
|
+
* Make your feature addition or bug fix.
|
52
|
+
* Add tests for it. This is important so I don't break it in a
|
53
|
+
future version unintentionally.
|
54
|
+
* Commit, do not mess with rakefile, version, or history.
|
55
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
56
|
+
* Send me a pull request. Bonus points for topic branches.
|
57
|
+
|
58
|
+
## Copyright
|
59
|
+
|
60
|
+
Copyright (c) 2010 Jared Pace. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "shadydb"
|
8
|
+
gem.summary = %Q{XML/JSON persistence layer that speaks ActiveModel}
|
9
|
+
gem.description = %Q{ORM that persists to file system using either XML or JSON with optional encryption layer.}
|
10
|
+
gem.email = "jared@codewordstudios.com"
|
11
|
+
gem.homepage = "http://github.com/jdpace/shadydb"
|
12
|
+
gem.authors = ["jdpace"]
|
13
|
+
gem.add_dependency "activemodel", '~> 3.0.0.beta3'
|
14
|
+
gem.add_dependency "activesupport", '~> 3.0.0.beta3'
|
15
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
16
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rake/testtask'
|
24
|
+
Rake::TestTask.new(:test) do |test|
|
25
|
+
test.libs << 'lib' << 'test'
|
26
|
+
test.pattern = 'test/**/*_test.rb'
|
27
|
+
test.verbose = true
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
require 'rcov/rcovtask'
|
32
|
+
Rcov::RcovTask.new do |test|
|
33
|
+
test.libs << 'test'
|
34
|
+
test.pattern = 'test/**/test_*.rb'
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
rescue LoadError
|
38
|
+
task :rcov do
|
39
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
task :test => :check_dependencies
|
44
|
+
|
45
|
+
task :default => :test
|
46
|
+
|
47
|
+
require 'rake/rdoctask'
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "shadydb #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ShadyDB
|
2
|
+
|
3
|
+
module Attributes
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
attr_accessor :id, :attributes
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def [](key)
|
12
|
+
attributes[key.to_s]
|
13
|
+
end
|
14
|
+
|
15
|
+
def []=(key, value)
|
16
|
+
attributes[key.to_s] = value
|
17
|
+
end
|
18
|
+
|
19
|
+
# Used for allowing accessor methods for dynamic attributes.
|
20
|
+
# Pulled from Mongoid
|
21
|
+
def method_missing(name, *args)
|
22
|
+
attr = name.to_s
|
23
|
+
return super unless @attributes.has_key?(attr.reader)
|
24
|
+
if attr.writer?
|
25
|
+
# "args.size > 1" allows to simulate 1.8 behavior of "*args"
|
26
|
+
@attributes[attr.reader] = (args.size > 1) ? args : args.first
|
27
|
+
else
|
28
|
+
@attributes[attr.reader]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ShadyDB
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :data_directory, :format
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@data_directory = '/data/db'
|
7
|
+
@format = :json
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :configuration
|
13
|
+
end
|
14
|
+
|
15
|
+
# Configure ShadyDB someplace sensible,
|
16
|
+
# like config/initializers/shadydb.rb
|
17
|
+
#
|
18
|
+
# @example
|
19
|
+
# ShadyDB.configure do |config|
|
20
|
+
# config.data_directory = Rails.root.join('db','shadydb')
|
21
|
+
# config.format = :json
|
22
|
+
# end
|
23
|
+
|
24
|
+
def self.configuration
|
25
|
+
@configuration ||= Configuration.new
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
def self.configure
|
30
|
+
self.configuration
|
31
|
+
yield(configuration)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ShadyDB
|
2
|
+
|
3
|
+
class Document
|
4
|
+
extend ActiveModel::Callbacks
|
5
|
+
include ActiveModel::Validations
|
6
|
+
include ShadyDB::Attributes
|
7
|
+
include ShadyDB::Persistence
|
8
|
+
include ShadyDB::Finders
|
9
|
+
|
10
|
+
before_save :validate
|
11
|
+
|
12
|
+
def initialize(attribs = {})
|
13
|
+
@new_record = true
|
14
|
+
@destroyed = false
|
15
|
+
|
16
|
+
self.attributes = {}
|
17
|
+
self.attributes = attribs.stringify_keys if attribs.kind_of?(Hash)
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_model
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_param
|
25
|
+
id
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_key
|
29
|
+
persisted? ? [id] : nil
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def validate
|
35
|
+
valid?
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ShadyDB
|
2
|
+
|
3
|
+
module Finders
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.class_eval do
|
7
|
+
extend ClassMethods
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def find(id)
|
13
|
+
document = self.new
|
14
|
+
document.instance_variable_set('@id', id)
|
15
|
+
|
16
|
+
return false unless File.exist?(document.path)
|
17
|
+
storage = File.read(document.path)
|
18
|
+
|
19
|
+
document.send(:restore!, storage)
|
20
|
+
document.instance_variable_set('@new_record', false)
|
21
|
+
document
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module ShadyDB
|
2
|
+
|
3
|
+
module Persistence
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
include ActiveModel::Serializers::JSON
|
6
|
+
include ActiveModel::Serializers::Xml
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.class_eval do
|
10
|
+
define_model_callbacks :save, :create, :update, :destroy
|
11
|
+
|
12
|
+
before_create :ensure_data_directory_exists
|
13
|
+
|
14
|
+
extend ClassMethods
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def create(attribs = {})
|
20
|
+
document = new(attribs)
|
21
|
+
document.save && document
|
22
|
+
end
|
23
|
+
|
24
|
+
def data_directory
|
25
|
+
File.join(ShadyDB.configuration.data_directory, self.model_name.plural)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def save
|
30
|
+
_run_save_callbacks do
|
31
|
+
create_or_update
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def update_attributes(attribs)
|
36
|
+
@attributes.merge!(attribs.stringify_keys)
|
37
|
+
save
|
38
|
+
end
|
39
|
+
|
40
|
+
def destroy
|
41
|
+
return false unless persisted?
|
42
|
+
begin
|
43
|
+
File.delete(path)
|
44
|
+
@destroyed = true
|
45
|
+
rescue
|
46
|
+
false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def persisted?
|
51
|
+
!(new_record? || destroyed?)
|
52
|
+
end
|
53
|
+
|
54
|
+
def new_record?
|
55
|
+
@new_record
|
56
|
+
end
|
57
|
+
|
58
|
+
def destroyed?
|
59
|
+
@destroyed
|
60
|
+
end
|
61
|
+
|
62
|
+
def path
|
63
|
+
File.join(self.class.data_directory, @id)
|
64
|
+
end
|
65
|
+
|
66
|
+
protected
|
67
|
+
|
68
|
+
def ensure_data_directory_exists
|
69
|
+
self.class.data_directory.split('/').inject do |base, dir_name|
|
70
|
+
dir = File.join(base, dir_name)
|
71
|
+
Dir.mkdir(dir) unless File.exist?(dir)
|
72
|
+
dir
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def create_or_update
|
77
|
+
persisted? ? update : create
|
78
|
+
end
|
79
|
+
|
80
|
+
def create
|
81
|
+
_run_create_callbacks do
|
82
|
+
@id = generate_id
|
83
|
+
persist!
|
84
|
+
@new_record = false
|
85
|
+
|
86
|
+
true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def update
|
91
|
+
_run_update_callbacks do
|
92
|
+
File.open(path,'w') do |storage|
|
93
|
+
storage << self.to_xml
|
94
|
+
end
|
95
|
+
|
96
|
+
true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def persist!
|
101
|
+
File.open(path,'w') do |storage|
|
102
|
+
storage << self.send(:"to_#{ShadyDB.configuration.format}")
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def restore!(xml_or_json)
|
107
|
+
send(:"from_#{ShadyDB.configuration.format}" ,xml_or_json)
|
108
|
+
end
|
109
|
+
|
110
|
+
# TODO: should check if ID is already taken
|
111
|
+
def generate_id
|
112
|
+
ActiveSupport::SecureRandom.hex()
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
data/lib/shadydb.rb
ADDED
data/shadydb.gemspec
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{shadydb}
|
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 = ["jdpace"]
|
12
|
+
s.date = %q{2010-05-17}
|
13
|
+
s.description = %q{ORM that persists to file system using either XML or JSON with optional encryption layer.}
|
14
|
+
s.email = %q{jared@codewordstudios.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/shadydb.rb",
|
27
|
+
"lib/shadydb/attributes.rb",
|
28
|
+
"lib/shadydb/configuration.rb",
|
29
|
+
"lib/shadydb/document.rb",
|
30
|
+
"lib/shadydb/extensions.rb",
|
31
|
+
"lib/shadydb/extensions/string/helpers.rb",
|
32
|
+
"lib/shadydb/finders.rb",
|
33
|
+
"lib/shadydb/persistence.rb",
|
34
|
+
"shadydb.gemspec",
|
35
|
+
"test/attributes_test.rb",
|
36
|
+
"test/configuration_test.rb",
|
37
|
+
"test/document_test.rb",
|
38
|
+
"test/finders_test.rb",
|
39
|
+
"test/helper.rb",
|
40
|
+
"test/persistence_test.rb"
|
41
|
+
]
|
42
|
+
s.homepage = %q{http://github.com/jdpace/shadydb}
|
43
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = %q{1.3.6}
|
46
|
+
s.summary = %q{XML/JSON persistence layer that speaks ActiveModel}
|
47
|
+
s.test_files = [
|
48
|
+
"test/attributes_test.rb",
|
49
|
+
"test/configuration_test.rb",
|
50
|
+
"test/document_test.rb",
|
51
|
+
"test/finders_test.rb",
|
52
|
+
"test/helper.rb",
|
53
|
+
"test/persistence_test.rb"
|
54
|
+
]
|
55
|
+
|
56
|
+
if s.respond_to? :specification_version then
|
57
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
58
|
+
s.specification_version = 3
|
59
|
+
|
60
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
61
|
+
s.add_runtime_dependency(%q<activemodel>, ["~> 3.0.0.beta3"])
|
62
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
|
63
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
64
|
+
else
|
65
|
+
s.add_dependency(%q<activemodel>, ["~> 3.0.0.beta3"])
|
66
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
|
67
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
68
|
+
end
|
69
|
+
else
|
70
|
+
s.add_dependency(%q<activemodel>, ["~> 3.0.0.beta3"])
|
71
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
|
72
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class AttributesTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
subject do
|
6
|
+
@subject = ShadyDB::Document.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context "Supporting hash-like interface for attributes" do
|
10
|
+
should "allow reading attributes via []" do
|
11
|
+
document = ShadyDB::Document.new :first_name => 'John', :last_name => 'Locke'
|
12
|
+
assert document[:first_name] == 'John'
|
13
|
+
end
|
14
|
+
|
15
|
+
should "allow setting attribures via []=" do
|
16
|
+
document = ShadyDB::Document.new
|
17
|
+
document[:name] = 'Jack Shepard'
|
18
|
+
assert document.attributes['name'] == 'Jack Shepard'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "Treating attribute keys like top level methods" do
|
23
|
+
should "support top level methods for reading attributes" do
|
24
|
+
document = ShadyDB::Document.new :first_name => 'John', :last_name => 'Locke'
|
25
|
+
assert document.first_name == 'John'
|
26
|
+
end
|
27
|
+
|
28
|
+
should 'support top level methods for setting attributes' do
|
29
|
+
document = ShadyDB::Document.new :name => 'Hugo'
|
30
|
+
document.name = 'Jack Shepard'
|
31
|
+
assert document.attributes['name'] == 'Jack Shepard'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class ConfigurationTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "Configuration" do
|
6
|
+
should "have defaults" do
|
7
|
+
config = ShadyDB::Configuration.new
|
8
|
+
assert config.data_directory
|
9
|
+
assert config.format
|
10
|
+
end
|
11
|
+
|
12
|
+
should "be accessible from ShadyDB.configuration" do
|
13
|
+
assert ShadyDB.configuration.kind_of?(ShadyDB::Configuration)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "be configurable" do
|
17
|
+
original_config = ShadyDB.configuration
|
18
|
+
ShadyDB.configuration = ShadyDB::Configuration.new
|
19
|
+
ShadyDB.configure do |config|
|
20
|
+
config.data_directory = '/shadydb'
|
21
|
+
config.format = :xml
|
22
|
+
end
|
23
|
+
|
24
|
+
assert ShadyDB.configuration.data_directory == '/shadydb'
|
25
|
+
assert ShadyDB.configuration.format == :xml
|
26
|
+
|
27
|
+
ShadyDB.configuration = original_config
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class FindersTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "Finding a document by its ID" do
|
6
|
+
should "load the document exactly as it was saved" do
|
7
|
+
original = ShadyDB::Document.create :name => 'Sawyer'
|
8
|
+
found = ShadyDB::Document.find original.id
|
9
|
+
|
10
|
+
assert found.id == original.id
|
11
|
+
assert found.name == original.name
|
12
|
+
assert found.attributes == original.attributes
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
require 'shadydb'
|
8
|
+
|
9
|
+
class Test::Unit::TestCase
|
10
|
+
|
11
|
+
def teardown
|
12
|
+
purge_documents
|
13
|
+
end
|
14
|
+
|
15
|
+
def purge_documents
|
16
|
+
FileUtils.rm_rf(File.join(File.expand_path(File.dirname(__FILE__)), 'db'))
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
ShadyDB.configure do |config|
|
22
|
+
config.data_directory = File.join(File.expand_path(File.dirname(__FILE__)), 'db')
|
23
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class PersistenceTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "Creating a new document" do
|
6
|
+
should "Save and return a document" do
|
7
|
+
@document = ShadyDB::Document.create :name => 'Dharma Initiative'
|
8
|
+
assert @document.persisted?
|
9
|
+
assert File.exist?(@document.path)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "Saving a new document" do
|
14
|
+
setup do
|
15
|
+
@document = ShadyDB::Document.new :name => 'Dharma Initiative'
|
16
|
+
end
|
17
|
+
|
18
|
+
should "save the document to the file system" do
|
19
|
+
assert @document.save
|
20
|
+
assert File.exist?(@document.path)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "Saving an existing document" do
|
25
|
+
setup do
|
26
|
+
@document = ShadyDB::Document.create :name => 'Dharma Initiative'
|
27
|
+
@before = File.new(@document.path).atime
|
28
|
+
end
|
29
|
+
|
30
|
+
should "update the document on the file system" do
|
31
|
+
assert @document.save
|
32
|
+
assert File.new(@document.path).atime >= @before
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "Updating an existing document" do
|
37
|
+
setup do
|
38
|
+
@document = ShadyDB::Document.create :name => 'Dharma Initiative'
|
39
|
+
@before = File.new(@document.path).atime
|
40
|
+
end
|
41
|
+
|
42
|
+
should "update the attributes" do
|
43
|
+
assert @document.update_attributes :location => 'The Island'
|
44
|
+
assert @document.location == 'The Island'
|
45
|
+
end
|
46
|
+
|
47
|
+
should "update the document on the file system" do
|
48
|
+
assert @document.update_attributes :location => 'The Island'
|
49
|
+
assert File.new(@document.path).atime >= @before
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context "Destroying a document" do
|
54
|
+
setup do
|
55
|
+
@document = ShadyDB::Document.create :name => 'Dharma Initiative'
|
56
|
+
end
|
57
|
+
|
58
|
+
should "remove the document from the file system" do
|
59
|
+
assert @document.destroy
|
60
|
+
assert !File.exist?(@document.path)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shadydb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- jdpace
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-17 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activemodel
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
- beta3
|
32
|
+
version: 3.0.0.beta3
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: activesupport
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ~>
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
segments:
|
43
|
+
- 3
|
44
|
+
- 0
|
45
|
+
- 0
|
46
|
+
- beta3
|
47
|
+
version: 3.0.0.beta3
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: shoulda
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
description: ORM that persists to file system using either XML or JSON with optional encryption layer.
|
63
|
+
email: jared@codewordstudios.com
|
64
|
+
executables: []
|
65
|
+
|
66
|
+
extensions: []
|
67
|
+
|
68
|
+
extra_rdoc_files:
|
69
|
+
- LICENSE
|
70
|
+
- README.markdown
|
71
|
+
files:
|
72
|
+
- .document
|
73
|
+
- .gitignore
|
74
|
+
- LICENSE
|
75
|
+
- README.markdown
|
76
|
+
- Rakefile
|
77
|
+
- VERSION
|
78
|
+
- lib/shadydb.rb
|
79
|
+
- lib/shadydb/attributes.rb
|
80
|
+
- lib/shadydb/configuration.rb
|
81
|
+
- lib/shadydb/document.rb
|
82
|
+
- lib/shadydb/extensions.rb
|
83
|
+
- lib/shadydb/extensions/string/helpers.rb
|
84
|
+
- lib/shadydb/finders.rb
|
85
|
+
- lib/shadydb/persistence.rb
|
86
|
+
- shadydb.gemspec
|
87
|
+
- test/attributes_test.rb
|
88
|
+
- test/configuration_test.rb
|
89
|
+
- test/document_test.rb
|
90
|
+
- test/finders_test.rb
|
91
|
+
- test/helper.rb
|
92
|
+
- test/persistence_test.rb
|
93
|
+
has_rdoc: true
|
94
|
+
homepage: http://github.com/jdpace/shadydb
|
95
|
+
licenses: []
|
96
|
+
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options:
|
99
|
+
- --charset=UTF-8
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
segments:
|
114
|
+
- 0
|
115
|
+
version: "0"
|
116
|
+
requirements: []
|
117
|
+
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 1.3.6
|
120
|
+
signing_key:
|
121
|
+
specification_version: 3
|
122
|
+
summary: XML/JSON persistence layer that speaks ActiveModel
|
123
|
+
test_files:
|
124
|
+
- test/attributes_test.rb
|
125
|
+
- test/configuration_test.rb
|
126
|
+
- test/document_test.rb
|
127
|
+
- test/finders_test.rb
|
128
|
+
- test/helper.rb
|
129
|
+
- test/persistence_test.rb
|