campchair 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +40 -1
- data/lib/campchair/model.rb +9 -5
- data/lib/campchair/version.rb +1 -1
- data/spec/campchair/model_spec.rb +5 -0
- data/spec/integration/readme_examples_spec.rb +46 -20
- metadata +2 -2
data/README.md
CHANGED
@@ -20,7 +20,7 @@ This is not append-only and replication is not a feature.
|
|
20
20
|
|
21
21
|
This project was born at [Railscamp 11](http://railscamps.com/).
|
22
22
|
|
23
|
-
###
|
23
|
+
### As a basic data structure store
|
24
24
|
|
25
25
|
For these examples we'll work with two documents.
|
26
26
|
|
@@ -67,6 +67,45 @@ fred[:last_seen] = Time.new(2012, 3, 6, 12, 40, 25)
|
|
67
67
|
doc[key] = fred
|
68
68
|
```
|
69
69
|
|
70
|
+
### As an persisted object store
|
71
|
+
|
72
|
+
Sometimes you want persistence baked-in à la ActiveRecord.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class People
|
76
|
+
include Campchair::Model
|
77
|
+
# note: this includes Campchair::Store automagically
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
Then you get some extra methods:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
# Add a person to the db
|
85
|
+
fred = People.new
|
86
|
+
fred.save
|
87
|
+
key = fred.key # think 'id'
|
88
|
+
# this key is generated
|
89
|
+
# but you can set one before saving if you like
|
90
|
+
|
91
|
+
# Rerieve a person from the db
|
92
|
+
fred = People.load(key) # think 'find'
|
93
|
+
|
94
|
+
# Delete the person!
|
95
|
+
fred.delete
|
96
|
+
|
97
|
+
# Reincarnate & persist
|
98
|
+
fred = Person.new
|
99
|
+
fred.save
|
100
|
+
|
101
|
+
# Attribtues get persisted
|
102
|
+
fred.attributes[:favourite_colour] = 'LSD'
|
103
|
+
fred.save
|
104
|
+
|
105
|
+
# as you can see with a Store-style query
|
106
|
+
People[fred.key] # => { :favourite_colour => 'LSD' }
|
107
|
+
```
|
108
|
+
|
70
109
|
### Basic views _not yet implemented_
|
71
110
|
|
72
111
|
For views to be available on classes we need to include `Campchair::Views`.
|
data/lib/campchair/model.rb
CHANGED
@@ -16,6 +16,8 @@ module Campchair
|
|
16
16
|
|
17
17
|
module ClassMethods
|
18
18
|
def load(key)
|
19
|
+
return unless exists?(key)
|
20
|
+
|
19
21
|
entity = new
|
20
22
|
entity.key = key
|
21
23
|
entity.reload
|
@@ -62,11 +64,13 @@ module Campchair
|
|
62
64
|
alias :destroy :delete
|
63
65
|
|
64
66
|
def reload(options = nil)
|
65
|
-
fresh_attributes = self.class[key]
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
67
|
+
if fresh_attributes = self.class[key]
|
68
|
+
attributes.update(fresh_attributes)
|
69
|
+
@new_entity = false
|
70
|
+
self
|
71
|
+
else
|
72
|
+
nil
|
73
|
+
end
|
70
74
|
end
|
71
75
|
|
72
76
|
end
|
data/lib/campchair/version.rb
CHANGED
@@ -26,6 +26,11 @@ describe Campchair::Model do
|
|
26
26
|
test_entity.should_not be_new_entity
|
27
27
|
test_entity.should_not be_destroyed
|
28
28
|
end
|
29
|
+
|
30
|
+
it "returns nil if the key is not present in the store" do
|
31
|
+
test_class['BADA55'].should be_nil
|
32
|
+
test_class.load('BADA55').should be_nil
|
33
|
+
end
|
29
34
|
end
|
30
35
|
|
31
36
|
describe "#save" do
|
@@ -1,6 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "As a basic data structure store" do
|
4
|
+
let(:test_class) do
|
5
|
+
Class.new do
|
6
|
+
include Campchair::Store
|
7
|
+
self.db_path = 'tmp/ccdb'
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
4
11
|
before do
|
5
12
|
@fred = {
|
6
13
|
:name => 'Fred', :height => 1.748,
|
@@ -14,42 +21,61 @@ describe "DB behavior" do
|
|
14
21
|
:first_seen => Time.local(2012, 3, 6, 12, 20),
|
15
22
|
:last_seen => Time.local(2012, 3, 6, 12, 50)
|
16
23
|
}
|
17
|
-
|
18
|
-
class TestPeople
|
19
|
-
include Campchair::Store
|
20
|
-
self.db_path = 'tmp/ccdb'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
after do
|
25
|
-
`rm -fr tmp/ccdb`
|
26
24
|
end
|
27
25
|
|
28
26
|
it "persists documents" do
|
29
|
-
key =
|
30
|
-
|
27
|
+
key = test_class << @fred
|
28
|
+
test_class[key].should == @fred
|
31
29
|
end
|
32
30
|
|
33
31
|
it "deletes documents" do
|
34
|
-
key =
|
35
|
-
|
36
|
-
|
32
|
+
key = test_class << @fred
|
33
|
+
test_class.delete(key)
|
34
|
+
test_class[key].should be_nil
|
37
35
|
end
|
38
36
|
|
39
37
|
it "updates docuemnts with a key" do
|
40
|
-
key =
|
38
|
+
key = test_class << @fred
|
41
39
|
new_last_seen = Time.local(2012, 3, 6, 12, 50)
|
42
40
|
@fred[:last_seen] = new_last_seen
|
43
|
-
|
41
|
+
test_class[key] = @fred
|
44
42
|
|
45
|
-
|
43
|
+
test_class[key][:last_seen].should == new_last_seen
|
46
44
|
end
|
47
45
|
|
48
46
|
it "creates documents with a key" do
|
49
47
|
key = 'something'
|
50
|
-
|
48
|
+
test_class[key] = @fred
|
51
49
|
|
52
|
-
|
50
|
+
test_class[key].should == @fred
|
53
51
|
end
|
54
52
|
|
55
53
|
end
|
54
|
+
|
55
|
+
describe "As an persisted object store" do
|
56
|
+
let(:test_class) do
|
57
|
+
Class.new do
|
58
|
+
include Campchair::Model
|
59
|
+
self.db_path = 'tmp/ccdb'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "persists object attributes" do
|
64
|
+
fred = test_class.new
|
65
|
+
fred.save
|
66
|
+
|
67
|
+
key = fred.key
|
68
|
+
loaded_fred = test_class.load(key)
|
69
|
+
fred.attributes.should == loaded_fred.attributes
|
70
|
+
|
71
|
+
fred.delete
|
72
|
+
test_class.should_not be_exists(fred.key)
|
73
|
+
|
74
|
+
fred = test_class.new
|
75
|
+
fred.save
|
76
|
+
fred.attributes[:favourite_colour] = 'LSD'
|
77
|
+
fred.save
|
78
|
+
|
79
|
+
test_class[fred.key].should == { :favourite_colour => 'LSD' }
|
80
|
+
end
|
81
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: campchair
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: leveldb-ruby
|