supermodel 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +3 -1
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/supermodel.rb +13 -13
- data/lib/supermodel/base.rb +10 -3
- data/lib/supermodel/redis.rb +7 -36
- data/lib/supermodel/validations/uniqueness.rb +3 -3
- data/supermodel.gemspec +4 -4
- metadata +30 -53
- data/.gitignore +0 -6
data/README
CHANGED
@@ -54,7 +54,9 @@ You can marshal objects to disk on startup/shutdown
|
|
54
54
|
You can use Redis, you need the Redis gem installed:
|
55
55
|
|
56
56
|
require "redis"
|
57
|
-
class Test < SuperModel::
|
57
|
+
class Test < SuperModel::Base
|
58
|
+
include SuperModel::Redis::Model
|
59
|
+
|
58
60
|
attributes :name
|
59
61
|
indexes :name
|
60
62
|
end
|
data/Rakefile
CHANGED
@@ -7,7 +7,7 @@ begin
|
|
7
7
|
gemspec.homepage = "http://github.com/maccman/supermodel"
|
8
8
|
gemspec.description = "In memory DB using ActiveModel"
|
9
9
|
gemspec.authors = ["Alex MacCaw"]
|
10
|
-
gemspec.add_dependency("activemodel", "
|
10
|
+
gemspec.add_dependency("activemodel", "~> 3.0.0")
|
11
11
|
end
|
12
12
|
rescue LoadError
|
13
13
|
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/lib/supermodel.rb
CHANGED
@@ -2,15 +2,14 @@ gem "activesupport"
|
|
2
2
|
gem "activemodel"
|
3
3
|
|
4
4
|
require "active_support/core_ext/class/attribute_accessors"
|
5
|
-
require "active_support/core_ext/class/inheritable_attributes"
|
6
5
|
require "active_support/core_ext/hash/indifferent_access"
|
7
6
|
require "active_support/core_ext/kernel/reporting"
|
8
|
-
require "active_support/core_ext/module/attr_accessor_with_default"
|
9
7
|
require "active_support/core_ext/module/delegation"
|
10
8
|
require "active_support/core_ext/module/aliasing"
|
11
9
|
require "active_support/core_ext/object/blank"
|
12
10
|
require "active_support/core_ext/object/try"
|
13
11
|
require "active_support/core_ext/object/to_query"
|
12
|
+
require "active_support/core_ext/class/attribute"
|
14
13
|
require "active_support/json"
|
15
14
|
|
16
15
|
require "active_model"
|
@@ -22,16 +21,17 @@ module SuperModel
|
|
22
21
|
end
|
23
22
|
|
24
23
|
$:.unshift(File.dirname(__FILE__))
|
25
|
-
|
26
24
|
require "supermodel/ext/array"
|
27
25
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
26
|
+
module SuperModel
|
27
|
+
autoload :Association, "supermodel/association"
|
28
|
+
autoload :Callbacks, "supermodel/callbacks"
|
29
|
+
autoload :Observing, "supermodel/observing"
|
30
|
+
autoload :Marshal, "supermodel/marshal"
|
31
|
+
autoload :RandomID, "supermodel/random_id"
|
32
|
+
autoload :Timestamp, "supermodel/timestamp"
|
33
|
+
autoload :Validations, "supermodel/validations"
|
34
|
+
autoload :Dirty, "supermodel/dirty"
|
35
|
+
autoload :Redis, "supermodel/redis"
|
36
|
+
autoload :Base, "supermodel/base"
|
37
|
+
end
|
data/lib/supermodel/base.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
module SuperModel
|
2
2
|
class Base
|
3
|
-
|
3
|
+
class_attribute :known_attributes
|
4
4
|
self.known_attributes = []
|
5
5
|
|
6
6
|
class << self
|
7
|
-
|
7
|
+
attr_accessor(:primary_key) #:nodoc:
|
8
|
+
|
9
|
+
def primary_key
|
10
|
+
@primary_key ||= 'id'
|
11
|
+
end
|
8
12
|
|
9
13
|
def collection(&block)
|
10
14
|
@collection ||= Class.new(Array)
|
@@ -120,12 +124,13 @@ module SuperModel
|
|
120
124
|
attr_writer :new_record
|
121
125
|
|
122
126
|
def known_attributes
|
123
|
-
self.class.known_attributes
|
127
|
+
self.class.known_attributes | self.attributes.keys.map(&:to_s)
|
124
128
|
end
|
125
129
|
|
126
130
|
def initialize(attributes = {})
|
127
131
|
@new_record = true
|
128
132
|
@attributes = {}.with_indifferent_access
|
133
|
+
@attributes.merge!(known_attributes.inject({}) {|h, n| h[n] = nil; h })
|
129
134
|
@changed_attributes = {}
|
130
135
|
load(attributes)
|
131
136
|
end
|
@@ -185,8 +190,10 @@ module SuperModel
|
|
185
190
|
def exists?
|
186
191
|
!new?
|
187
192
|
end
|
193
|
+
alias_method :persisted?, :exists?
|
188
194
|
|
189
195
|
def load(attributes) #:nodoc:
|
196
|
+
return unless attributes
|
190
197
|
attributes.each do |(name, value)|
|
191
198
|
self.send("#{name}=".to_sym, value)
|
192
199
|
end
|
data/lib/supermodel/redis.rb
CHANGED
@@ -5,10 +5,7 @@ module SuperModel
|
|
5
5
|
base.class_eval do
|
6
6
|
class_inheritable_array :indexed_attributes
|
7
7
|
self.indexed_attributes = []
|
8
|
-
|
9
|
-
class_inheritable_array :serialized_attributes
|
10
|
-
self.serialized_attributes = []
|
11
|
-
|
8
|
+
|
12
9
|
class_inheritable_hash :redis_options
|
13
10
|
self.redis_options = {}
|
14
11
|
end
|
@@ -29,11 +26,7 @@ module SuperModel
|
|
29
26
|
def indexes(*indexes)
|
30
27
|
self.indexed_attributes += indexes.map(&:to_s)
|
31
28
|
end
|
32
|
-
|
33
|
-
def serialize(*attributes)
|
34
|
-
self.serialized_attributes += attributes.map(&:to_s)
|
35
|
-
end
|
36
|
-
|
29
|
+
|
37
30
|
def redis_key(*args)
|
38
31
|
args.unshift(self.namespace)
|
39
32
|
args.join(":")
|
@@ -109,10 +102,7 @@ module SuperModel
|
|
109
102
|
|
110
103
|
destroy_indexes
|
111
104
|
redis.srem(self.class.redis_key, self.id)
|
112
|
-
|
113
|
-
attributes.keys.each do |key|
|
114
|
-
redis.del(redis_key(key))
|
115
|
-
end
|
105
|
+
redis.del(redis_key)
|
116
106
|
end
|
117
107
|
|
118
108
|
def destroy_indexes
|
@@ -130,7 +120,7 @@ module SuperModel
|
|
130
120
|
end
|
131
121
|
|
132
122
|
def generate_id
|
133
|
-
redis.incr(self.class.redis_key(:uid))
|
123
|
+
redis.incr(self.class.redis_key(:uid))
|
134
124
|
end
|
135
125
|
|
136
126
|
def indexed_attributes
|
@@ -140,36 +130,17 @@ module SuperModel
|
|
140
130
|
def redis
|
141
131
|
self.class.redis
|
142
132
|
end
|
143
|
-
|
133
|
+
|
144
134
|
def redis_key(*args)
|
145
135
|
self.class.redis_key(id, *args)
|
146
136
|
end
|
147
137
|
|
148
|
-
def serialized_attributes
|
149
|
-
self.class.serialized_attributes
|
150
|
-
end
|
151
|
-
|
152
|
-
def serialize_attribute(key, value)
|
153
|
-
return value unless serialized_attributes.include?(key)
|
154
|
-
value.to_json
|
155
|
-
end
|
156
|
-
|
157
|
-
def deserialize_attribute(key, value)
|
158
|
-
return value unless serialized_attributes.include?(key)
|
159
|
-
value && ActiveSupport::JSON.decode(value)
|
160
|
-
end
|
161
|
-
|
162
138
|
def redis_set
|
163
|
-
|
164
|
-
redis.set(redis_key(key), serialize_attribute(key, value))
|
165
|
-
end
|
139
|
+
redis.set(redis_key, serializable_hash.to_json)
|
166
140
|
end
|
167
141
|
|
168
142
|
def redis_get
|
169
|
-
|
170
|
-
result = deserialize_attribute(key, redis.get(redis_key(key)))
|
171
|
-
send("#{key}=", result)
|
172
|
-
end
|
143
|
+
load(ActiveSupport::JSON.decode(redis.get(redis_key)))
|
173
144
|
end
|
174
145
|
public :redis_get
|
175
146
|
|
@@ -1,11 +1,11 @@
|
|
1
|
-
module
|
1
|
+
module SuperModel
|
2
2
|
module Validations
|
3
|
-
class UniquenessValidator < EachValidator
|
3
|
+
class UniquenessValidator < ActiveModel::EachValidator
|
4
4
|
attr_reader :klass
|
5
5
|
|
6
6
|
def validate_each(record, attribute, value)
|
7
7
|
alternate = klass.find_by_attribute(attribute, value)
|
8
|
-
return unless alternate
|
8
|
+
return unless alternate
|
9
9
|
record.errors.add(attribute, "must be unique", :default => options[:message])
|
10
10
|
end
|
11
11
|
|
data/supermodel.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
|
|
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-08-
|
12
|
+
s.date = %q{2010-08-30}
|
13
13
|
s.description = %q{In memory DB using ActiveModel}
|
14
14
|
s.email = %q{info@eribium.org}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -48,12 +48,12 @@ Gem::Specification.new do |s|
|
|
48
48
|
s.specification_version = 3
|
49
49
|
|
50
50
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
-
s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0
|
51
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 3.0.0"])
|
52
52
|
else
|
53
|
-
s.add_dependency(%q<activemodel>, ["
|
53
|
+
s.add_dependency(%q<activemodel>, ["~> 3.0.0"])
|
54
54
|
end
|
55
55
|
else
|
56
|
-
s.add_dependency(%q<activemodel>, ["
|
56
|
+
s.add_dependency(%q<activemodel>, ["~> 3.0.0"])
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
metadata
CHANGED
@@ -1,48 +1,34 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: supermodel
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 1
|
8
|
-
- 4
|
9
|
-
version: 0.1.4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Alex MacCaw
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-08-26 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: activemodel
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70137254259940 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
- 3
|
30
|
-
- 0
|
31
|
-
- 0
|
32
|
-
- beta
|
33
|
-
version: 3.0.0.beta
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
34
22
|
type: :runtime
|
35
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70137254259940
|
36
25
|
description: In memory DB using ActiveModel
|
37
26
|
email: info@eribium.org
|
38
27
|
executables: []
|
39
|
-
|
40
28
|
extensions: []
|
41
|
-
|
42
|
-
extra_rdoc_files:
|
29
|
+
extra_rdoc_files:
|
43
30
|
- README
|
44
|
-
files:
|
45
|
-
- .gitignore
|
31
|
+
files:
|
46
32
|
- MIT-LICENSE
|
47
33
|
- README
|
48
34
|
- Rakefile
|
@@ -62,37 +48,28 @@ files:
|
|
62
48
|
- lib/supermodel/validations.rb
|
63
49
|
- lib/supermodel/validations/uniqueness.rb
|
64
50
|
- supermodel.gemspec
|
65
|
-
has_rdoc: true
|
66
51
|
homepage: http://github.com/maccman/supermodel
|
67
52
|
licenses: []
|
68
|
-
|
69
53
|
post_install_message:
|
70
|
-
rdoc_options:
|
71
|
-
|
72
|
-
require_paths:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
73
56
|
- lib
|
74
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
58
|
none: false
|
76
|
-
requirements:
|
77
|
-
- -
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
|
80
|
-
|
81
|
-
version: "0"
|
82
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
64
|
none: false
|
84
|
-
requirements:
|
85
|
-
- -
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
|
88
|
-
- 0
|
89
|
-
version: "0"
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
90
69
|
requirements: []
|
91
|
-
|
92
70
|
rubyforge_project:
|
93
|
-
rubygems_version: 1.
|
71
|
+
rubygems_version: 1.8.6
|
94
72
|
signing_key:
|
95
73
|
specification_version: 3
|
96
74
|
summary: In memory DB using ActiveModel
|
97
75
|
test_files: []
|
98
|
-
|