passive_record 0.2.2 → 0.2.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cef72fdda85e2a5d1dabfd009842c092780ce77d
4
- data.tar.gz: a6f81fbd8c7a0b88d318e46a8aa232ae69b0a549
3
+ metadata.gz: 44c674c3b4dc0121e16a6368fce9d4109ccf6b51
4
+ data.tar.gz: 7621a7ffbba7ee1cf08cd36bcd0cb053d36da13f
5
5
  SHA512:
6
- metadata.gz: bb22e938cd0081234dd6f02b02ae060d3fc37e794b78b479a313e6793ab0fe486f367dfa38c91abb98b1f2594297a33ae4b529968d8b3906c1addcaacea6c8a1
7
- data.tar.gz: d0e3ee535809bb7817be14dece26cb727bb0db7621fb1a090f9baa2ad69b9f54366c29a73f4651e15f938e2edf802e424390398212510aa6cd5fcc7d50af4cf1
6
+ metadata.gz: ea2328b524559ed872dbf095380ecd2d3f4b5fba455c798c600ba35e540965c42929e07c98d8c49e1ce19b456690871cb69bf0e4e3e975f72e43c0cd528af3c3
7
+ data.tar.gz: 5c12ba3c4eb166561a9c93703dde48e79e0cea00712c3eab9d919591073b16615e8c0f6e905132d56c525e81a46fd6df022e5ee1aa06e1fefa2f8834c714102c
data/bin/passive_record CHANGED
@@ -8,7 +8,7 @@ if File.directory?(File.join(root,'.git'))
8
8
  rescue LoadError => e
9
9
  warn e.message
10
10
  warn "Run `gem install bundler` to install Bundler"
11
- exit -1
11
+ exit(-1)
12
12
  end
13
13
  end
14
14
  end
@@ -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 attribute_names
6
- attr_names = instance_variables
7
- attr_names += self.class.associations_id_syms
8
- attr_names += members rescue []
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
- def blacklisted_attribute_names
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 send_relation(matching_relation, meth, *args)
49
- target_name = matching_relation.association.target_name_symbol.to_s
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
@@ -1,4 +1,4 @@
1
1
  module PassiveRecord
2
2
  # passive_record version
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.3"
4
4
  end
@@ -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
@@ -13,6 +13,8 @@ end
13
13
 
14
14
  class SimpleModel < Struct.new(:foo)
15
15
  include PassiveRecord
16
+ attr_reader :updated_at
17
+ after_update { @updated_at = Time.now }
16
18
  end
17
19
 
18
20
  module Family
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: passive_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman