shadydb 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -43,7 +43,6 @@ anyone to spend the time to write their own ORM.
43
43
  * Add support for collection finders
44
44
  * Add support for conditional finders
45
45
  * Add simple indexing
46
- * Add defined fields a la Mongoid
47
46
 
48
47
  ## Note on Patches/Pull Requests
49
48
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -4,10 +4,21 @@ module ShadyDB
4
4
 
5
5
  def self.included(base)
6
6
  base.class_eval do
7
- attr_accessor :id, :attributes
7
+ extend ClassMethods
8
+
9
+ cattr_accessor :fields
10
+ self.fields = {}
11
+
12
+ field :id
13
+
14
+ attr_accessor :attributes
8
15
  end
9
16
  end
10
17
 
18
+ def id
19
+ self[:id]
20
+ end
21
+
11
22
  def [](key)
12
23
  attributes[key.to_s]
13
24
  end
@@ -29,6 +40,14 @@ module ShadyDB
29
40
  end
30
41
  end
31
42
 
43
+ module ClassMethods
44
+
45
+ def field(name, options = {})
46
+ self.fields[name.to_s] = options[:default]
47
+ end
48
+
49
+ end
50
+
32
51
  end
33
52
 
34
53
  end
@@ -4,8 +4,8 @@ module ShadyDB
4
4
  extend ActiveModel::Callbacks
5
5
  include ActiveModel::Validations
6
6
  include ShadyDB::Attributes
7
- include ShadyDB::Persistence
8
7
  include ShadyDB::Finders
8
+ include ShadyDB::Persistence
9
9
 
10
10
  before_save :validate
11
11
 
@@ -13,8 +13,8 @@ module ShadyDB
13
13
  @new_record = true
14
14
  @destroyed = false
15
15
 
16
- self.attributes = {}
17
- self.attributes = attribs.stringify_keys if attribs.kind_of?(Hash)
16
+ self.attributes = attribs.stringify_keys
17
+ self.attributes.reverse_merge!(self.class.fields)
18
18
  end
19
19
 
20
20
  def to_model
@@ -10,18 +10,21 @@ module ShadyDB
10
10
 
11
11
  module ClassMethods
12
12
  def find(id)
13
- document = self.new
14
- document.instance_variable_set('@id', id)
13
+ return false unless self.exists?(id)
15
14
 
16
- return false unless File.exist?(document.path)
15
+ document = self.new :id => id
17
16
  storage = File.read(document.path)
18
17
 
19
18
  document.send(:restore!, storage)
20
19
  document.instance_variable_set('@new_record', false)
21
20
  document
22
21
  end
22
+
23
+ def exists?(id)
24
+ document = self.new :id => id
25
+ File.exist?(document.path)
26
+ end
23
27
  end
24
-
25
28
  end
26
29
 
27
30
  end
@@ -60,7 +60,7 @@ module ShadyDB
60
60
  end
61
61
 
62
62
  def path
63
- File.join(self.class.data_directory, @id)
63
+ File.join(self.class.data_directory, id)
64
64
  end
65
65
 
66
66
  protected
@@ -79,7 +79,7 @@ module ShadyDB
79
79
 
80
80
  def create
81
81
  _run_create_callbacks do
82
- @id = generate_id
82
+ generate_id
83
83
  persist!
84
84
  @new_record = false
85
85
 
@@ -107,9 +107,10 @@ module ShadyDB
107
107
  send(:"from_#{ShadyDB.configuration.format}" ,xml_or_json)
108
108
  end
109
109
 
110
- # TODO: should check if ID is already taken
111
110
  def generate_id
112
- ActiveSupport::SecureRandom.hex()
111
+ while(id.nil? || self.class.exists?(id)) do
112
+ self.id = ActiveSupport::SecureRandom.hex()
113
+ end
113
114
  end
114
115
 
115
116
  end
data/shadydb.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{shadydb}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["jdpace"]
12
- s.date = %q{2010-05-17}
12
+ s.date = %q{2010-05-19}
13
13
  s.description = %q{ORM that persists to file system using either XML or JSON with optional encryption layer.}
14
14
  s.email = %q{jared@codewordstudios.com}
15
15
  s.extra_rdoc_files = [
@@ -1,5 +1,10 @@
1
1
  require 'helper'
2
2
 
3
+ class SampleDocument < ShadyDB::Document
4
+ field :name
5
+ field :role, :default => 'developer'
6
+ end
7
+
3
8
  class AttributesTest < Test::Unit::TestCase
4
9
 
5
10
  subject do
@@ -32,4 +37,22 @@ class AttributesTest < Test::Unit::TestCase
32
37
  end
33
38
  end
34
39
 
40
+ context 'Defined fields' do
41
+ should "allow defined fields which are always created" do
42
+ document = SampleDocument.new
43
+ assert document.attributes.key?('name')
44
+ assert document.name.nil?
45
+ end
46
+
47
+ should "allow default values for defined fields" do
48
+ document = SampleDocument.new
49
+ assert document[:role] == 'developer'
50
+ end
51
+
52
+ should "be able to override default values" do
53
+ document = SampleDocument.new :role => 'admin'
54
+ assert document[:role] == 'admin'
55
+ end
56
+ end
57
+
35
58
  end
data/test/finders_test.rb CHANGED
@@ -13,4 +13,18 @@ class FindersTest < Test::Unit::TestCase
13
13
  end
14
14
  end
15
15
 
16
+ context "Checking whether or not a Document exists" do
17
+ should "return true if a document is persited" do
18
+ document = ShadyDB::Document.create :name => 'Sawyer'
19
+ assert document.persisted?
20
+ assert ShadyDB::Document.exists?(document.id)
21
+ end
22
+
23
+ should "return false if a document is not persited" do
24
+ document = ShadyDB::Document.new :id => 'fake-id', :name => 'Sawyer'
25
+ assert !document.persisted?
26
+ assert !ShadyDB::Document.exists?(document.id)
27
+ end
28
+ end
29
+
16
30
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - jdpace
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-17 00:00:00 -04:00
17
+ date: 2010-05-19 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency