proxy_for_template 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/proxy_for_template.rb +7 -7
- data/lib/proxy_for_template/version.rb +1 -1
- data/spec/activerecord_spec.rb +8 -1
- data/spec/proxy_spec.rb +1 -1
- metadata +5 -7
data/lib/proxy_for_template.rb
CHANGED
@@ -84,14 +84,12 @@ module ProxyForTemplate
|
|
84
84
|
self.class.treat_as_nil
|
85
85
|
end
|
86
86
|
|
87
|
-
def treat_as_nil?(method, template_value)
|
87
|
+
def treat_as_nil?(method, template_value, self_value)
|
88
88
|
|
89
89
|
proc = treat_as_nil[method.to_sym]
|
90
90
|
|
91
91
|
is_nil = false
|
92
92
|
|
93
|
-
self_value = self.send(method.to_s + '_orig')
|
94
|
-
|
95
93
|
if(proc)
|
96
94
|
call_info = TemplateProxyCall.new(method, template_value, self_value)
|
97
95
|
|
@@ -107,6 +105,7 @@ module ProxyForTemplate
|
|
107
105
|
|
108
106
|
def proxy_attribute_read(method)
|
109
107
|
self_value = nil
|
108
|
+
template_value = nil
|
110
109
|
|
111
110
|
if(template_responds_to?(method))
|
112
111
|
template_value = template.attributes[method]
|
@@ -114,7 +113,7 @@ module ProxyForTemplate
|
|
114
113
|
|
115
114
|
self_value = self.attributes[method]
|
116
115
|
|
117
|
-
if !(template_value.nil? || template_value == '')
|
116
|
+
if !(template_value.nil? || template_value == '' || treat_as_nil?(method, template_value, self_value))
|
118
117
|
self_value = template_value
|
119
118
|
end
|
120
119
|
|
@@ -122,7 +121,6 @@ module ProxyForTemplate
|
|
122
121
|
end
|
123
122
|
|
124
123
|
def proxy_read(method)
|
125
|
-
|
126
124
|
template_value = value_from_template(method)
|
127
125
|
self_value = nil
|
128
126
|
|
@@ -142,8 +140,10 @@ module ProxyForTemplate
|
|
142
140
|
template_value = template.send(method)
|
143
141
|
end
|
144
142
|
|
145
|
-
if(!template_value.nil?
|
146
|
-
|
143
|
+
if(!template_value.nil?)
|
144
|
+
self_value = self.send(method.to_s + '_orig')
|
145
|
+
|
146
|
+
template_value = nil if treat_as_nil?(method, template_value, self_value)
|
147
147
|
end
|
148
148
|
|
149
149
|
template_value
|
data/spec/activerecord_spec.rb
CHANGED
@@ -20,7 +20,7 @@ class Spork < ActiveRecord::Base
|
|
20
20
|
belongs_to :template, :class_name => Spork.name
|
21
21
|
include ProxyForTemplate
|
22
22
|
|
23
|
-
template_method :some_value
|
23
|
+
template_method :some_value, :treat_as_nil => lambda { |instance, call_info| call_info.template_value == 987 }
|
24
24
|
end
|
25
25
|
|
26
26
|
describe ProxyForTemplate, '#activerecord' do
|
@@ -96,4 +96,11 @@ describe ProxyForTemplate, '#activerecord' do
|
|
96
96
|
|
97
97
|
base.some_value_before_type_cast.should eq(10)
|
98
98
|
end
|
99
|
+
|
100
|
+
it 'value_before_type_cast should respect treat_as_nil' do
|
101
|
+
template = Spork.create(:some_value => 987)
|
102
|
+
base = Spork.create(:some_value => 1024, :template => template)
|
103
|
+
|
104
|
+
base.some_value_before_type_cast.should eq(1024)
|
105
|
+
end
|
99
106
|
end
|
data/spec/proxy_spec.rb
CHANGED
@@ -92,7 +92,7 @@ describe ProxyForTemplate do
|
|
92
92
|
it 'should be able to enumerate treat_as_nil methods' do
|
93
93
|
@f.treat_as_nil.keys.to_a.should eq([:a])
|
94
94
|
|
95
|
-
@f.treat_as_nil?(:a, false).should eq(true)
|
95
|
+
@f.treat_as_nil?(:a, false, false).should eq(true)
|
96
96
|
end
|
97
97
|
|
98
98
|
it 'should obey treat false as nil' do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proxy_for_template
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mike Emery
|
@@ -15,8 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-01-
|
19
|
-
default_executable:
|
18
|
+
date: 2012-01-25 00:00:00 Z
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
22
21
|
name: rspec
|
@@ -112,7 +111,6 @@ files:
|
|
112
111
|
- spec/activerecord_spec.rb
|
113
112
|
- spec/proxy_spec.rb
|
114
113
|
- spec/spec_helper.rb
|
115
|
-
has_rdoc: true
|
116
114
|
homepage: ""
|
117
115
|
licenses: []
|
118
116
|
|
@@ -142,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
140
|
requirements: []
|
143
141
|
|
144
142
|
rubyforge_project: proxy_for_template
|
145
|
-
rubygems_version: 1.
|
143
|
+
rubygems_version: 1.8.10
|
146
144
|
signing_key:
|
147
145
|
specification_version: 3
|
148
146
|
summary: Allows method calls to be delegated to a template object.
|