cubbyhole 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +4 -0
- data/lib/cubbyhole/version.rb +1 -1
- data/lib/cubbyhole.rb +63 -2
- data/spec/cubbyhole_spec.rb +53 -0
- metadata +6 -5
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/lib/cubbyhole/version.rb
CHANGED
data/lib/cubbyhole.rb
CHANGED
@@ -6,8 +6,69 @@ end
|
|
6
6
|
|
7
7
|
module Cubbyhole
|
8
8
|
class Base
|
9
|
-
def
|
10
|
-
|
9
|
+
def self.create(params={})
|
10
|
+
new(params).save
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.get(id)
|
14
|
+
objs[id]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.next_id
|
18
|
+
@next_id ||= 0
|
19
|
+
id = @next_id
|
20
|
+
@next_id += 1
|
21
|
+
id
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.objs
|
25
|
+
@objs ||= {}
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize(params={})
|
29
|
+
@id = self.class.next_id
|
30
|
+
@params = params
|
31
|
+
@persisted = false
|
32
|
+
stringify_keys!
|
33
|
+
end
|
34
|
+
|
35
|
+
attr_reader :id
|
36
|
+
|
37
|
+
def persisted?
|
38
|
+
@persisted
|
39
|
+
end
|
40
|
+
|
41
|
+
def save
|
42
|
+
@persisted = true
|
43
|
+
stringify_keys!
|
44
|
+
self.class.objs[@id] = self
|
45
|
+
end
|
46
|
+
|
47
|
+
def method_missing(meth, *args, &blk)
|
48
|
+
key = meth.to_s
|
49
|
+
|
50
|
+
if key =~ /=$/
|
51
|
+
raise ArgumentError unless args.size == 1
|
52
|
+
@params[key.gsub(/=$/, "")] = args.first
|
53
|
+
else
|
54
|
+
raise ArgumentError unless args.size == 0
|
55
|
+
@params[key]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def respond_to?(*args); true; end
|
60
|
+
|
61
|
+
def update_attributes(params)
|
62
|
+
@params.merge!(params)
|
63
|
+
stringify_keys!
|
64
|
+
save
|
65
|
+
end
|
66
|
+
|
67
|
+
def stringify_keys!
|
68
|
+
@params.keys.each do |key|
|
69
|
+
@params[key.to_s] = @params.delete(key)
|
70
|
+
end
|
71
|
+
@params
|
11
72
|
end
|
12
73
|
end
|
13
74
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
2
|
+
|
3
|
+
require 'cubbyhole'
|
4
|
+
|
5
|
+
describe Cubbyhole do
|
6
|
+
it "makes constants" do
|
7
|
+
NewConstant.new.foo.should == nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "allows you to set attrs on new" do
|
11
|
+
model = NewModel.new(:foo => "bar", :baz => "bang")
|
12
|
+
model.foo.should == "bar"
|
13
|
+
model.baz.should == "bang"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "allows you to save and fetch objects" do
|
17
|
+
model = NewModel.new(:foo => "bar")
|
18
|
+
model.save
|
19
|
+
|
20
|
+
fetched = NewModel.get(model.id)
|
21
|
+
fetched.foo.should == "bar"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should not persist unsaved objects" do
|
25
|
+
model = NewModel.new(:foo => "bar")
|
26
|
+
|
27
|
+
NewModel.get(model.id).should be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "allows you to create saved objects" do
|
31
|
+
model = NewModel.create(:foo => "bar")
|
32
|
+
|
33
|
+
fetched = NewModel.get(model.id)
|
34
|
+
fetched.foo.should == "bar"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "allows you to set and retrieve attributes" do
|
38
|
+
model = NewModel.new
|
39
|
+
model.foo.should == nil
|
40
|
+
model.foo = "bar"
|
41
|
+
model.foo.should == "bar"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "allows you to update_attributes" do
|
45
|
+
model = NewModel.new(:foo => "bar")
|
46
|
+
model.save
|
47
|
+
model.update_attributes(:foo => "baz", :bar => "zot")
|
48
|
+
|
49
|
+
fetched = NewModel.get(model.id)
|
50
|
+
fetched.foo.should == "baz"
|
51
|
+
fetched.bar.should == "zot"
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cubbyhole
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 0
|
9
8
|
- 1
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andy Delcambre
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- cubbyhole.gemspec
|
37
37
|
- lib/cubbyhole.rb
|
38
38
|
- lib/cubbyhole/version.rb
|
39
|
+
- spec/cubbyhole_spec.rb
|
39
40
|
has_rdoc: true
|
40
41
|
homepage: ""
|
41
42
|
licenses: []
|
@@ -70,5 +71,5 @@ rubygems_version: 1.6.2
|
|
70
71
|
signing_key:
|
71
72
|
specification_version: 3
|
72
73
|
summary: A zero config data store
|
73
|
-
test_files:
|
74
|
-
|
74
|
+
test_files:
|
75
|
+
- spec/cubbyhole_spec.rb
|