proxy_for_template 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile ADDED
@@ -0,0 +1,9 @@
1
+ h1. What does this do?
2
+
3
+ Here at SPARQCode we have a use case where we have objects that want to delegate specific methods to a "template". The template's value should take precedence if present, otherwise the value of the "child" should be used.
4
+
5
+ The easiest way to see how this gem behaves is to look at the rspec tests.
6
+
7
+ h1. How do I use this?
8
+
9
+ Generally speaking you just need an object that responds to "template". More specifically you need to include the ProxyForTemplate module, and then use template_method to tell the gem which method calls should be delegated. The tests have a pretty comprehensive example of how this works.
@@ -3,6 +3,10 @@ require "proxy_for_template/version"
3
3
  module ProxyForTemplate
4
4
 
5
5
  module ClassMethods
6
+
7
+ def treat_false_as_nil
8
+ @treat_false_as_nil ||= Set.new
9
+ end
6
10
 
7
11
  def templated_methods
8
12
  @templated_methods ||= Set.new
@@ -16,8 +20,14 @@ module ProxyForTemplate
16
20
  end
17
21
 
18
22
  def template_method(*method_names)
23
+ options = {}
24
+
25
+ if(method_names.last.is_a?(Hash))
26
+ options = method_names.pop
27
+ end
28
+
19
29
  method_names.each do |method|
20
- define_proxy(method)
30
+ define_proxy(method, options[:treat_false_as_nil])
21
31
  end
22
32
  end
23
33
 
@@ -25,9 +35,13 @@ module ProxyForTemplate
25
35
  @rails ||= defined?(ActiveRecord::Base)
26
36
  end
27
37
 
28
- def define_proxy(method_name)
38
+ def define_proxy(method_name, special_false = false)
29
39
  templated_methods << method_name
30
40
 
41
+ if special_false
42
+ treat_false_as_nil << method_name
43
+ end
44
+
31
45
  define_attribute_methods if respond_to?(:define_attribute_methods)
32
46
 
33
47
  read_method = method_name.to_s
@@ -60,6 +74,10 @@ module ProxyForTemplate
60
74
  true
61
75
  end
62
76
 
77
+ def treat_false_as_nil
78
+ self.class.treat_false_as_nil
79
+ end
80
+
63
81
  def templated_methods
64
82
  self.class.templated_methods
65
83
  end
@@ -91,6 +109,10 @@ module ProxyForTemplate
91
109
  template_value = template.send(method)
92
110
  end
93
111
 
112
+ if(template_value == false && treat_false_as_nil.include?(method.to_sym))
113
+ template_value = nil
114
+ end
115
+
94
116
  self_value = self.send(method + '_orig')
95
117
 
96
118
  if template_takes_precedence(method) && !(template_value.nil? || template_value == '')
@@ -1,3 +1,3 @@
1
1
  module ProxyForTemplate
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/proxy_spec.rb CHANGED
@@ -3,10 +3,13 @@ require 'spec_helper'
3
3
  class Foo
4
4
  include ProxyForTemplate
5
5
 
6
- template_accessor :a
6
+ attr_accessor :a
7
7
  attr_accessor :b
8
+ attr_accessor :c
8
9
  attr_writer :template_takes_precedence
9
10
 
11
+ template_method :a, :treat_false_as_nil => true
12
+
10
13
  attr_accessor :template
11
14
 
12
15
  def template_takes_precedence(method)
@@ -23,9 +26,11 @@ end
23
26
 
24
27
  class Bar
25
28
  attr_accessor :a
29
+ attr_accessor :c
26
30
 
27
31
  def initialize(a)
28
32
  @a = a
33
+ @c = false
29
34
  end
30
35
  end
31
36
 
@@ -100,4 +105,14 @@ describe ProxyForTemplate do
100
105
  @f.a = 123
101
106
  @f.a.should eq(123)
102
107
  end
108
+
109
+ it 'should be able to enumerate treat_false_as_nil methods' do
110
+ @f.treat_false_as_nil.to_a.should eq([:a])
111
+ end
112
+
113
+ it 'should obey treat false as nil' do
114
+ @f.template.a = false
115
+ @f.a.should eq('abc')
116
+ end
117
+
103
118
  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: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Emery
@@ -15,7 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-11 00:00:00 Z
18
+ date: 2012-01-17 00:00:00 -08:00
19
+ default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: rspec
@@ -103,6 +104,7 @@ extra_rdoc_files: []
103
104
  files:
104
105
  - .gitignore
105
106
  - Gemfile
107
+ - README.textile
106
108
  - Rakefile
107
109
  - lib/proxy_for_template.rb
108
110
  - lib/proxy_for_template/version.rb
@@ -110,6 +112,7 @@ files:
110
112
  - spec/activerecord_spec.rb
111
113
  - spec/proxy_spec.rb
112
114
  - spec/spec_helper.rb
115
+ has_rdoc: true
113
116
  homepage: ""
114
117
  licenses: []
115
118
 
@@ -139,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
142
  requirements: []
140
143
 
141
144
  rubyforge_project: proxy_for_template
142
- rubygems_version: 1.8.10
145
+ rubygems_version: 1.6.2
143
146
  signing_key:
144
147
  specification_version: 3
145
148
  summary: Allows method calls to be delegated to a template object.