supermodel 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/VERSION +1 -1
- data/lib/supermodel/base.rb +15 -14
- data/lib/supermodel/marshal.rb +26 -7
- data/lib/supermodel/redis.rb +10 -10
- data/supermodel.gemspec +3 -3
- metadata +21 -9
data/.gitignore
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/lib/supermodel/base.rb
CHANGED
@@ -11,15 +11,16 @@ module SuperModel
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def records
|
14
|
-
@records ||=
|
14
|
+
@records ||= {}
|
15
15
|
end
|
16
16
|
|
17
17
|
def find_by_attribute(name, value) #:nodoc:
|
18
|
-
records.find {|r| r.send(name) == value }
|
18
|
+
item = records.values.find {|r| r.send(name) == value }
|
19
|
+
item && item.dup
|
19
20
|
end
|
20
21
|
|
21
22
|
def raw_find(id) #:nodoc:
|
22
|
-
|
23
|
+
records[id] || raise(UnknownRecord)
|
23
24
|
end
|
24
25
|
|
25
26
|
# Find record by ID, or raise.
|
@@ -30,12 +31,12 @@ module SuperModel
|
|
30
31
|
alias :[] :find
|
31
32
|
|
32
33
|
def first
|
33
|
-
item = records[0]
|
34
|
+
item = records.values[0]
|
34
35
|
item && item.dup
|
35
36
|
end
|
36
37
|
|
37
38
|
def last
|
38
|
-
item = records[-1]
|
39
|
+
item = records.values[-1]
|
39
40
|
item && item.dup
|
40
41
|
end
|
41
42
|
|
@@ -44,7 +45,7 @@ module SuperModel
|
|
44
45
|
end
|
45
46
|
|
46
47
|
def all
|
47
|
-
records.dup
|
48
|
+
records.values.dup
|
48
49
|
end
|
49
50
|
|
50
51
|
def update(id, atts)
|
@@ -72,7 +73,7 @@ module SuperModel
|
|
72
73
|
# create(:name => "foo", :id => 1)
|
73
74
|
def create(atts = {})
|
74
75
|
rec = self.new(atts)
|
75
|
-
rec.save && rec
|
76
|
+
rec.save! && rec
|
76
77
|
end
|
77
78
|
|
78
79
|
def method_missing(method_symbol, *args) #:nodoc:
|
@@ -91,7 +92,7 @@ module SuperModel
|
|
91
92
|
end
|
92
93
|
|
93
94
|
attr_accessor :attributes
|
94
|
-
attr_writer
|
95
|
+
attr_writer :new_record
|
95
96
|
|
96
97
|
def known_attributes
|
97
98
|
self.class.known_attributes + self.attributes.keys.map(&:to_s)
|
@@ -192,10 +193,6 @@ module SuperModel
|
|
192
193
|
super
|
193
194
|
end
|
194
195
|
end
|
195
|
-
|
196
|
-
def raw_destroy
|
197
|
-
self.class.records.delete(self)
|
198
|
-
end
|
199
196
|
|
200
197
|
def destroy
|
201
198
|
raw_destroy
|
@@ -215,8 +212,12 @@ module SuperModel
|
|
215
212
|
object_id
|
216
213
|
end
|
217
214
|
|
215
|
+
def raw_destroy
|
216
|
+
self.class.records.delete(self.id)
|
217
|
+
end
|
218
|
+
|
218
219
|
def raw_create
|
219
|
-
self.class.records
|
220
|
+
self.class.records[self.id] = self.dup
|
220
221
|
end
|
221
222
|
|
222
223
|
def create
|
@@ -262,6 +263,6 @@ module SuperModel
|
|
262
263
|
include ActiveModel::Conversion
|
263
264
|
include ActiveModel::Serializers::JSON
|
264
265
|
include ActiveModel::Serializers::Xml
|
265
|
-
include Dirty, Observing,
|
266
|
+
include Dirty, Observing, Callbacks, Validations
|
266
267
|
end
|
267
268
|
end
|
data/lib/supermodel/marshal.rb
CHANGED
@@ -18,11 +18,19 @@ module SuperModel
|
|
18
18
|
def load
|
19
19
|
return unless path
|
20
20
|
return unless File.exist?(path)
|
21
|
-
|
22
|
-
File.open(path, "rb")
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
data = []
|
22
|
+
File.open(path, "rb") do |file|
|
23
|
+
begin
|
24
|
+
data = ::Marshal.load(file)
|
25
|
+
rescue
|
26
|
+
# Lots of errors can occur during
|
27
|
+
# marshaling - such as EOF etc
|
28
|
+
return false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
data.each do |klass, records|
|
32
|
+
klass.marshal_records(records)
|
33
|
+
end
|
26
34
|
true
|
27
35
|
end
|
28
36
|
|
@@ -30,8 +38,11 @@ module SuperModel
|
|
30
38
|
return unless path
|
31
39
|
tmp_file = Tempfile.new("rbdump")
|
32
40
|
tmp_file.binmode
|
33
|
-
|
34
|
-
|
41
|
+
data = klasses.inject({}) {|hash, klass|
|
42
|
+
hash[klass] = klass.marshal_records
|
43
|
+
hash
|
44
|
+
}
|
45
|
+
::Marshal.dump(data, tmp_file)
|
35
46
|
# Atomic serialization - so we never corrupt the db
|
36
47
|
FileUtils.mv(tmp_file.path, path)
|
37
48
|
true
|
@@ -41,6 +52,7 @@ module SuperModel
|
|
41
52
|
|
42
53
|
module Model
|
43
54
|
def self.included(base)
|
55
|
+
base.extend ClassMethods
|
44
56
|
Marshal.klasses << base
|
45
57
|
end
|
46
58
|
|
@@ -53,6 +65,13 @@ module SuperModel
|
|
53
65
|
# isn't setup properly
|
54
66
|
@attributes = atts
|
55
67
|
end
|
68
|
+
|
69
|
+
module ClassMethods
|
70
|
+
def marshal_records(records = nil)
|
71
|
+
@records = records if records
|
72
|
+
@records
|
73
|
+
end
|
74
|
+
end
|
56
75
|
end
|
57
76
|
end
|
58
77
|
end
|
data/lib/supermodel/redis.rb
CHANGED
@@ -87,19 +87,19 @@ module SuperModel
|
|
87
87
|
def id
|
88
88
|
super.try(:to_i)
|
89
89
|
end
|
90
|
-
|
91
|
-
|
92
|
-
|
90
|
+
|
91
|
+
protected
|
92
|
+
def raw_destroy
|
93
|
+
return if new?
|
93
94
|
|
94
|
-
|
95
|
-
|
95
|
+
destroy_indexes
|
96
|
+
redis.set_delete(self.class.redis_key, self.id)
|
96
97
|
|
97
|
-
|
98
|
-
|
98
|
+
attributes.keys.each do |key|
|
99
|
+
redis.delete(redis_key(key))
|
100
|
+
end
|
99
101
|
end
|
100
|
-
|
101
|
-
|
102
|
-
protected
|
102
|
+
|
103
103
|
def destroy_indexes
|
104
104
|
indexed_attributes.each do |index|
|
105
105
|
old_attribute = changes[index].try(:first) || send(index)
|
data/supermodel.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{supermodel}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alex MacCaw"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-03-11}
|
13
13
|
s.description = %q{In memory DB using ActiveModel}
|
14
14
|
s.email = %q{info@eribium.org}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -36,7 +36,7 @@ Gem::Specification.new do |s|
|
|
36
36
|
s.homepage = %q{http://github.com/maccman/supermodel}
|
37
37
|
s.rdoc_options = ["--charset=UTF-8"]
|
38
38
|
s.require_paths = ["lib"]
|
39
|
-
s.rubygems_version = %q{1.3.
|
39
|
+
s.rubygems_version = %q{1.3.6}
|
40
40
|
s.summary = %q{In memory DB using ActiveModel}
|
41
41
|
|
42
42
|
if s.respond_to? :specification_version then
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: supermodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Alex MacCaw
|
@@ -9,19 +14,24 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-03-11 00:00:00 +00:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: activemodel
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
- beta
|
23
32
|
version: 3.0.0.beta
|
24
|
-
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
25
35
|
description: In memory DB using ActiveModel
|
26
36
|
email: info@eribium.org
|
27
37
|
executables: []
|
@@ -60,18 +70,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
70
|
requirements:
|
61
71
|
- - ">="
|
62
72
|
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 0
|
63
75
|
version: "0"
|
64
|
-
version:
|
65
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
77
|
requirements:
|
67
78
|
- - ">="
|
68
79
|
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
69
82
|
version: "0"
|
70
|
-
version:
|
71
83
|
requirements: []
|
72
84
|
|
73
85
|
rubyforge_project:
|
74
|
-
rubygems_version: 1.3.
|
86
|
+
rubygems_version: 1.3.6
|
75
87
|
signing_key:
|
76
88
|
specification_version: 3
|
77
89
|
summary: In memory DB using ActiveModel
|