supermodel 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/supermodel/base.rb +23 -4
- data/lib/supermodel/scriber.rb +41 -19
- data/supermodel.gemspec +1 -1
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/lib/supermodel/base.rb
CHANGED
@@ -5,6 +5,14 @@ module SuperModel
|
|
5
5
|
class << self
|
6
6
|
attr_accessor_with_default(:primary_key, 'id') #:nodoc:
|
7
7
|
|
8
|
+
def attributes(*attributes)
|
9
|
+
@known_attributes = attributes.map(&:to_s)
|
10
|
+
end
|
11
|
+
|
12
|
+
def known_attributes
|
13
|
+
@known_attributes ||= []
|
14
|
+
end
|
15
|
+
|
8
16
|
def records
|
9
17
|
@records ||= []
|
10
18
|
end
|
@@ -26,7 +34,7 @@ module SuperModel
|
|
26
34
|
end
|
27
35
|
|
28
36
|
def last
|
29
|
-
item = records[1]
|
37
|
+
item = records[-1]
|
30
38
|
item && item.dup
|
31
39
|
end
|
32
40
|
|
@@ -66,13 +74,15 @@ module SuperModel
|
|
66
74
|
rec.save && rec
|
67
75
|
end
|
68
76
|
|
69
|
-
def method_missing(method_symbol, *
|
77
|
+
def method_missing(method_symbol, *args) #:nodoc:
|
70
78
|
method_name = method_symbol.to_s
|
71
79
|
|
72
80
|
if method_name =~ /^find_by_(\w+)!/
|
73
|
-
send("find_by_#{$1}", *
|
81
|
+
send("find_by_#{$1}", *args) || raise(UnknownRecord)
|
74
82
|
elsif method_name =~ /^find_by_(\w+)/
|
75
|
-
records.find {|r| r.send($1) ==
|
83
|
+
records.find {|r| r.send($1) == args.first }
|
84
|
+
elsif method_name =~ /^find_or_create_by_(\w+)/
|
85
|
+
send("find_by_#{$1}", *args) || create($1 => args.first)
|
76
86
|
else
|
77
87
|
super
|
78
88
|
end
|
@@ -81,6 +91,10 @@ module SuperModel
|
|
81
91
|
|
82
92
|
attr_accessor :attributes
|
83
93
|
|
94
|
+
def known_attributes
|
95
|
+
self.class.known_attributes + self.attributes.keys.map(&:to_s)
|
96
|
+
end
|
97
|
+
|
84
98
|
def initialize(attributes = {})
|
85
99
|
@attributes = {}.with_indifferent_access
|
86
100
|
load(attributes)
|
@@ -164,6 +178,8 @@ module SuperModel
|
|
164
178
|
method_name = method.to_s
|
165
179
|
if attributes.nil?
|
166
180
|
super
|
181
|
+
elsif known_attributes.include?(method_name)
|
182
|
+
true
|
167
183
|
elsif method_name =~ /(?:=|\?)$/ && attributes.include?($`)
|
168
184
|
true
|
169
185
|
else
|
@@ -221,6 +237,7 @@ module SuperModel
|
|
221
237
|
end
|
222
238
|
else
|
223
239
|
return attributes[method_name] if attributes.include?(method_name)
|
240
|
+
return nil if known_attributes.include?(method_name)
|
224
241
|
super
|
225
242
|
end
|
226
243
|
end
|
@@ -229,6 +246,8 @@ module SuperModel
|
|
229
246
|
class Base
|
230
247
|
extend ActiveModel::Naming
|
231
248
|
include ActiveModel::Conversion
|
249
|
+
include ActiveModel::Serializers::JSON
|
250
|
+
include ActiveModel::Serializers::Xml
|
232
251
|
include Observing, Validations, Callbacks
|
233
252
|
end
|
234
253
|
end
|
data/lib/supermodel/scriber.rb
CHANGED
@@ -1,15 +1,20 @@
|
|
1
1
|
module SuperModel
|
2
|
-
module Scriber
|
3
|
-
|
4
|
-
|
5
|
-
end
|
6
|
-
module_function :klasses
|
7
|
-
|
8
|
-
class Observer < ActiveModel::Observer
|
9
|
-
def self.observed_classes
|
10
|
-
Scriber.klasses
|
11
|
-
end
|
2
|
+
module Scriber
|
3
|
+
class Observer
|
4
|
+
include Singleton
|
12
5
|
|
6
|
+
class << self
|
7
|
+
def disabled?
|
8
|
+
@disabled
|
9
|
+
end
|
10
|
+
|
11
|
+
def disable(&block)
|
12
|
+
@disabled = true
|
13
|
+
yield
|
14
|
+
@disabled = false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
13
18
|
def after_create(rec)
|
14
19
|
rec.class.record(:create, rec.attributes)
|
15
20
|
end
|
@@ -24,28 +29,45 @@ module SuperModel
|
|
24
29
|
|
25
30
|
def after_destroy
|
26
31
|
rec.class.record(:destroy, rec.id)
|
27
|
-
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def update(observed_method, object) #:nodoc:
|
35
|
+
return if self.class.disabled?
|
36
|
+
send(observed_method, object) if respond_to?(observed_method)
|
37
|
+
end
|
38
|
+
|
39
|
+
def observed_class_inherited(subclass) #:nodoc:
|
40
|
+
subclass.add_observer(self)
|
41
|
+
end
|
28
42
|
end
|
29
43
|
|
44
|
+
def klasses
|
45
|
+
@klasses ||= []
|
46
|
+
end
|
47
|
+
module_function :klasses
|
48
|
+
|
30
49
|
module Model
|
31
50
|
def self.included(base)
|
32
51
|
Scriber.klasses << base
|
33
52
|
base.extend ClassMethods
|
53
|
+
base.add_observer(Observer.instance)
|
34
54
|
end
|
35
55
|
|
36
56
|
module ClassMethods
|
37
57
|
def scribe_play(type, data) #:nodoc:
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
58
|
+
Observer.disable do
|
59
|
+
case type
|
60
|
+
when :create then create(data)
|
61
|
+
when :destroy then destroy(data)
|
62
|
+
when :update then update(data)
|
63
|
+
else
|
64
|
+
method = "scribe_play_#{type}"
|
65
|
+
send(method) if respond_to?(method)
|
66
|
+
end
|
45
67
|
end
|
46
68
|
end
|
47
69
|
|
48
|
-
def record(type, data)
|
70
|
+
def record(type, data = nil)
|
49
71
|
::Scriber.record(self, type, data)
|
50
72
|
end
|
51
73
|
end
|
data/supermodel.gemspec
CHANGED