proxy_for_template 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module ProxyForTemplate
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -4,8 +4,8 @@ module ProxyForTemplate
4
4
 
5
5
  module ClassMethods
6
6
 
7
- def treat_false_as_nil
8
- @treat_false_as_nil ||= Set.new
7
+ def treat_as_nil
8
+ @treat_as_nil ||= {}
9
9
  end
10
10
 
11
11
  def templated_methods
@@ -27,7 +27,7 @@ module ProxyForTemplate
27
27
  end
28
28
 
29
29
  method_names.each do |method|
30
- define_proxy(method, options[:treat_false_as_nil])
30
+ define_proxy(method, options[:treat_as_nil])
31
31
  end
32
32
  end
33
33
 
@@ -35,11 +35,11 @@ module ProxyForTemplate
35
35
  @rails ||= defined?(ActiveRecord::Base)
36
36
  end
37
37
 
38
- def define_proxy(method_name, special_false = false)
38
+ def define_proxy(method_name, false_eval = false)
39
39
  templated_methods << method_name
40
40
 
41
- if special_false
42
- treat_false_as_nil << method_name
41
+ if false_eval
42
+ treat_as_nil[method_name] = false_eval
43
43
  end
44
44
 
45
45
  define_attribute_methods if respond_to?(:define_attribute_methods)
@@ -74,8 +74,21 @@ module ProxyForTemplate
74
74
  true
75
75
  end
76
76
 
77
- def treat_false_as_nil
78
- self.class.treat_false_as_nil
77
+ def treat_as_nil
78
+ self.class.treat_as_nil
79
+ end
80
+
81
+ def treat_as_nil?(method, value)
82
+
83
+ proc = treat_as_nil[method.to_sym]
84
+
85
+ is_nil = false
86
+
87
+ if(proc)
88
+ is_nil = proc.call(value)
89
+ end
90
+
91
+ is_nil
79
92
  end
80
93
 
81
94
  def templated_methods
@@ -109,7 +122,7 @@ module ProxyForTemplate
109
122
  template_value = template.send(method)
110
123
  end
111
124
 
112
- if(template_value == false && treat_false_as_nil.include?(method.to_sym))
125
+ if(template_value && treat_as_nil?(method, template_value))
113
126
  template_value = nil
114
127
  end
115
128
 
data/spec/proxy_spec.rb CHANGED
@@ -8,7 +8,7 @@ class Foo
8
8
  attr_accessor :c
9
9
  attr_writer :template_takes_precedence
10
10
 
11
- template_method :a, :treat_false_as_nil => true
11
+ template_method :a, :treat_as_nil => Proc.new { |value| value == false }
12
12
 
13
13
  attr_accessor :template
14
14
 
@@ -107,7 +107,11 @@ describe ProxyForTemplate do
107
107
  end
108
108
 
109
109
  it 'should be able to enumerate treat_false_as_nil methods' do
110
- @f.treat_false_as_nil.to_a.should eq([:a])
110
+ @f.treat_as_nil.keys.to_a.should eq([:a])
111
+
112
+ @f.treat_as_nil[:a].call(false).should eq(true)
113
+
114
+ @f.treat_as_nil?(:a, false).should eq(true)
111
115
  end
112
116
 
113
117
  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: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Emery