proxy_for_template 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -66,26 +66,36 @@ module ProxyForTemplate
66
66
  end
67
67
  end
68
68
 
69
+ class TemplateProxyCall
70
+ attr_reader :method, :template_value, :self_value, :instance
71
+
72
+ def initialize(method, template_value, self_value)
73
+ @method = method
74
+ @template_value = template_value
75
+ @self_value = self_value
76
+ end
77
+ end
78
+
69
79
  def self.included(base)
70
80
  base.extend(ClassMethods)
71
81
  end
72
82
 
73
- def template_takes_precedence(method)
74
- true
75
- end
76
-
77
83
  def treat_as_nil
78
84
  self.class.treat_as_nil
79
85
  end
80
86
 
81
- def treat_as_nil?(method, value)
82
-
87
+ def treat_as_nil?(method, template_value)
88
+
83
89
  proc = treat_as_nil[method.to_sym]
84
90
 
85
91
  is_nil = false
86
92
 
93
+ self_value = self.send(method.to_s + '_orig')
94
+
87
95
  if(proc)
88
- is_nil = proc.call(value)
96
+ call_info = TemplateProxyCall.new(method, template_value, self_value)
97
+
98
+ is_nil = proc.call(self, call_info)
89
99
  end
90
100
 
91
101
  is_nil
@@ -95,9 +105,7 @@ module ProxyForTemplate
95
105
  self.class.templated_methods
96
106
  end
97
107
 
98
- def proxy_attribute_read(method)
99
-
100
-
108
+ def proxy_attribute_read(method)
101
109
  self_value = nil
102
110
 
103
111
  if(template_responds_to?(method))
@@ -106,7 +114,7 @@ module ProxyForTemplate
106
114
 
107
115
  self_value = self.attributes[method]
108
116
 
109
- if template_takes_precedence(method) && !(template_value.nil? || template_value == '')
117
+ if !(template_value.nil? || template_value == '')
110
118
  self_value = template_value
111
119
  end
112
120
 
@@ -120,7 +128,7 @@ module ProxyForTemplate
120
128
 
121
129
  self_value = self.send(method + '_orig')
122
130
 
123
- if template_takes_precedence(method) && !(template_value.nil? || template_value == '')
131
+ if !(template_value.nil? || template_value == '')
124
132
  self_value = template_value
125
133
  end
126
134
 
@@ -145,6 +153,7 @@ module ProxyForTemplate
145
153
  value = value_from_template(method)
146
154
 
147
155
  blank = (value.nil? || value == '')
156
+
148
157
  !blank
149
158
  end
150
159
 
@@ -1,3 +1,3 @@
1
1
  module ProxyForTemplate
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -20,12 +20,6 @@ class Spork < ActiveRecord::Base
20
20
  belongs_to :template, :class_name => Spork.name
21
21
  include ProxyForTemplate
22
22
 
23
- attr_accessor :template_takes_precedence
24
-
25
- def template_takes_precedence(method)
26
- @template_takes_precedence.nil? ? true : @template_takes_precedence
27
- end
28
-
29
23
  template_method :some_value
30
24
  end
31
25
 
@@ -102,13 +96,4 @@ describe ProxyForTemplate, '#activerecord' do
102
96
 
103
97
  base.some_value_before_type_cast.should eq(10)
104
98
  end
105
-
106
- it 'the base should take precedence over the template when specified and using before type cast' do
107
- template = Spork.create(:some_value => 10)
108
- base = Spork.create(:some_value => 5, :template => template)
109
-
110
- base.template_takes_precedence = false
111
-
112
- base.some_value_before_type_cast.should eq(5)
113
- end
114
99
  end
data/spec/proxy_spec.rb CHANGED
@@ -6,21 +6,19 @@ class Foo
6
6
  attr_accessor :a
7
7
  attr_accessor :b
8
8
  attr_accessor :c
9
- attr_writer :template_takes_precedence
9
+
10
+ def check_if_false(call_info)
11
+ call_info.template_value == false
12
+ end
10
13
 
11
- template_method :a, :treat_as_nil => Proc.new { |value| value == false }
14
+ template_method :a, :treat_as_nil => Proc.new { |instance, call_info| instance.check_if_false(call_info) }
12
15
 
13
16
  attr_accessor :template
14
17
 
15
- def template_takes_precedence(method)
16
- @template_takes_precedence
17
- end
18
-
19
18
  def initialize
20
19
  @template = Bar.new('abc123')
21
20
  @a = 'abc'
22
21
  @b = '123'
23
- @template_takes_precedence = true
24
22
  end
25
23
  end
26
24
 
@@ -91,26 +89,9 @@ describe ProxyForTemplate do
91
89
  @f.a.should eq('abc')
92
90
  end
93
91
 
94
- it "should be able to control whether proxying is turned off or on" do
95
- @f.template_takes_precedence(:a).should eq(true)
96
- @f.template_takes_precedence = false
97
- @f.template_takes_precedence(:a).should eq(false)
98
- end
99
-
100
- it 'the template should not take precedence if its marked as a theme' do
101
- @f.template_takes_precedence = false
102
-
103
- @f.template_takes_precedence(:a).should eq(false)
104
-
105
- @f.a = 123
106
- @f.a.should eq(123)
107
- end
108
-
109
- it 'should be able to enumerate treat_false_as_nil methods' do
92
+ it 'should be able to enumerate treat_as_nil methods' do
110
93
  @f.treat_as_nil.keys.to_a.should eq([:a])
111
94
 
112
- @f.treat_as_nil[:a].call(false).should eq(true)
113
-
114
95
  @f.treat_as_nil?(:a, false).should eq(true)
115
96
  end
116
97
 
@@ -118,5 +99,4 @@ describe ProxyForTemplate do
118
99
  @f.template.a = false
119
100
  @f.a.should eq('abc')
120
101
  end
121
-
122
102
  end
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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Emery
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-18 00:00:00 -08:00
18
+ date: 2012-01-20 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency