passive_record 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/passive_record +1 -1
- data/lib/passive_record/hooks.rb +12 -0
- data/lib/passive_record/instance_methods.rb +45 -38
- data/lib/passive_record/version.rb +1 -1
- data/spec/passive_record_spec.rb +11 -0
- data/spec/spec_helper.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44c674c3b4dc0121e16a6368fce9d4109ccf6b51
|
4
|
+
data.tar.gz: 7621a7ffbba7ee1cf08cd36bcd0cb053d36da13f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea2328b524559ed872dbf095380ecd2d3f4b5fba455c798c600ba35e540965c42929e07c98d8c49e1ce19b456690871cb69bf0e4e3e975f72e43c0cd528af3c3
|
7
|
+
data.tar.gz: 5c12ba3c4eb166561a9c93703dde48e79e0cea00712c3eab9d919591073b16615e8c0f6e905132d56c525e81a46fd6df022e5ee1aa06e1fefa2f8834c714102c
|
data/bin/passive_record
CHANGED
data/lib/passive_record/hooks.rb
CHANGED
@@ -31,5 +31,17 @@ module PassiveRecord
|
|
31
31
|
@hooks += [ hook ]
|
32
32
|
self
|
33
33
|
end
|
34
|
+
|
35
|
+
def after_update_hooks
|
36
|
+
@hooks ||= []
|
37
|
+
@hooks.select { |hook| hook.kind == :after_update }
|
38
|
+
end
|
39
|
+
|
40
|
+
def after_update(*meth_syms, &blk)
|
41
|
+
hook = Hook.new(:after_update,*meth_syms,&blk)
|
42
|
+
@hooks ||= []
|
43
|
+
@hooks += [ hook ]
|
44
|
+
self
|
45
|
+
end
|
34
46
|
end
|
35
47
|
end
|
@@ -2,16 +2,14 @@ module PassiveRecord
|
|
2
2
|
module InstanceMethods
|
3
3
|
include PrettyPrinting
|
4
4
|
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
attr_names.reject! { |name| name.to_s.start_with?("@_") }
|
10
|
-
attr_names - blacklisted_attribute_names
|
11
|
-
end
|
5
|
+
def update(attrs={})
|
6
|
+
attrs.each do |k,v|
|
7
|
+
send("#{k}=", v)
|
8
|
+
end
|
12
9
|
|
13
|
-
|
14
|
-
|
10
|
+
self.class.after_update_hooks.each do |hook|
|
11
|
+
hook.run(self)
|
12
|
+
end
|
15
13
|
end
|
16
14
|
|
17
15
|
# from http://stackoverflow.com/a/8417341/90042
|
@@ -25,8 +23,6 @@ module PassiveRecord
|
|
25
23
|
]
|
26
24
|
end
|
27
25
|
|
28
|
-
###
|
29
|
-
|
30
26
|
def respond_to?(meth,*args,&blk)
|
31
27
|
if find_relation_by_target_name_symbol(meth)
|
32
28
|
true
|
@@ -44,42 +40,26 @@ module PassiveRecord
|
|
44
40
|
end
|
45
41
|
|
46
42
|
protected
|
43
|
+
def attribute_names
|
44
|
+
attr_names = instance_variables
|
45
|
+
attr_names += self.class.associations_id_syms
|
46
|
+
attr_names += members rescue []
|
47
|
+
attr_names.reject! { |name| name.to_s.start_with?("@_") }
|
48
|
+
attr_names - blacklisted_attribute_names
|
49
|
+
end
|
47
50
|
|
48
|
-
def
|
49
|
-
|
50
|
-
|
51
|
-
case meth.to_s
|
52
|
-
when target_name
|
53
|
-
matching_relation.lookup
|
54
|
-
when "#{target_name}="
|
55
|
-
if args.first.is_a?(Array)
|
56
|
-
# need to loop through each arg and set id
|
57
|
-
args.first.each do |child|
|
58
|
-
child.send(matching_relation.parent_model_id_field + "=", id)
|
59
|
-
end
|
60
|
-
else
|
61
|
-
# assume simple assignment
|
62
|
-
matching_relation.parent_model_id = args.first.id
|
63
|
-
end
|
64
|
-
when "create_#{target_name}", "create_#{target_name.singularize}"
|
65
|
-
matching_relation.create(*args)
|
66
|
-
when "#{target_name}_id"
|
67
|
-
matching_relation.parent_model_id
|
68
|
-
when "#{target_name}_id="
|
69
|
-
matching_relation.parent_model_id = args.first
|
70
|
-
when "#{target_name}_ids", "#{target_name.singularize}_ids"
|
71
|
-
matching_relation.parent_model.send(target_name).map(&:id)
|
72
|
-
end
|
51
|
+
def blacklisted_attribute_names
|
52
|
+
[]
|
73
53
|
end
|
74
54
|
|
55
|
+
private
|
56
|
+
|
75
57
|
def relata
|
76
58
|
@_relata ||= self.class.associations&.map do |assn|
|
77
59
|
assn.to_relation(self)
|
78
60
|
end || []
|
79
61
|
end
|
80
62
|
|
81
|
-
private
|
82
|
-
|
83
63
|
def find_relation_by_target_name_symbol(meth)
|
84
64
|
relata.detect do |relation| # matching relation...
|
85
65
|
possible_target_names(relation).include?(meth.to_s)
|
@@ -99,5 +79,32 @@ module PassiveRecord
|
|
99
79
|
"create_#{target_name.singularize}"
|
100
80
|
]
|
101
81
|
end
|
82
|
+
|
83
|
+
def send_relation(matching_relation, meth, *args)
|
84
|
+
target_name = matching_relation.association.target_name_symbol.to_s
|
85
|
+
|
86
|
+
case meth.to_s
|
87
|
+
when target_name
|
88
|
+
matching_relation.lookup
|
89
|
+
when "#{target_name}="
|
90
|
+
if args.first.is_a?(Array)
|
91
|
+
# need to loop through each arg and set id
|
92
|
+
args.first.each do |child|
|
93
|
+
child.send(matching_relation.parent_model_id_field + "=", id)
|
94
|
+
end
|
95
|
+
else
|
96
|
+
# assume simple assignment
|
97
|
+
matching_relation.parent_model_id = args.first.id
|
98
|
+
end
|
99
|
+
when "create_#{target_name}", "create_#{target_name.singularize}"
|
100
|
+
matching_relation.create(*args)
|
101
|
+
when "#{target_name}_id"
|
102
|
+
matching_relation.parent_model_id
|
103
|
+
when "#{target_name}_id="
|
104
|
+
matching_relation.parent_model_id = args.first
|
105
|
+
when "#{target_name}_ids", "#{target_name.singularize}_ids"
|
106
|
+
matching_relation.parent_model.send(target_name).map(&:id)
|
107
|
+
end
|
108
|
+
end
|
102
109
|
end
|
103
110
|
end
|
data/spec/passive_record_spec.rb
CHANGED
@@ -22,6 +22,17 @@ describe "passive record models" do
|
|
22
22
|
let(:value) { 'foo_value' }
|
23
23
|
|
24
24
|
describe "instance methods" do
|
25
|
+
describe "#update" do
|
26
|
+
it 'should update attrs' do
|
27
|
+
expect {model.update(foo: '123')}.to change {model.foo}.from(value).to('123')
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should invoke callbacks' do
|
31
|
+
model.update(foo: 'barbazquux')
|
32
|
+
expect(model.updated_at).to be_a(Time)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
25
36
|
describe "#inspect" do
|
26
37
|
it 'should report attribute details' do
|
27
38
|
expect(model.inspect).to eq("SimpleModel (id: #{model.id.inspect}, foo: \"foo_value\")")
|
data/spec/spec_helper.rb
CHANGED