delegate_all_for 0.0.5 → 0.0.6

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.
@@ -1,3 +1,3 @@
1
1
  module DelegateAllFor
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -16,23 +16,28 @@ module DelegateAllFor
16
16
  # An array of column names to exclude from delegation
17
17
  # [:also_include]
18
18
  # An array of method names to also delegate
19
+ # [:prefix]
20
+ # Prefixes accessors. See the <tt>delegate</tt> documentation.
21
+ # [:allow_nil]
22
+ # Allows accessor to be nil. See the <tt>delegate</tt> documentation.
19
23
  def delegate_all_for(*attr_names)
20
- options = { except: [], also_include: [] }
24
+ options = { except: [], also_include: [], prefix: false, allow_nil: false }
21
25
  options.update(attr_names.extract_options!)
22
- options.assert_valid_keys(:except, :also_include)
26
+ options.assert_valid_keys(:except, :also_include, :prefix, :allow_nil)
23
27
 
24
28
  exclude_columns = self.column_names.dup.concat(options[:except].map(&:to_s))
25
29
  attr_names.each do |association_name|
26
30
  if reflection = reflect_on_association(association_name)
31
+ delegate_opts = options.slice(:prefix, :allow_nil).merge(to: association_name)
27
32
  options[:also_include].each do |m|
28
- class_eval(%{delegate :#{m}, :to => :#{association_name}})
33
+ class_eval(%{delegate :#{m}, #{delegate_opts}})
29
34
  end
30
35
  (reflection.klass.column_names - exclude_columns).each do |column_name|
31
36
  next if column_name.in?(reflection.foreign_key, 'updated_at', 'updated_on', 'created_at', 'created_on')
32
37
  class_eval <<-eoruby, __FILE__, __LINE__ + 1
33
- delegate :#{column_name}, :to => :#{association_name}
34
- delegate :#{column_name}=, :to => :#{association_name}
35
- delegate :#{column_name}?, :to => :#{association_name}
38
+ delegate :#{column_name}, #{delegate_opts}
39
+ delegate :#{column_name}=, #{delegate_opts}
40
+ delegate :#{column_name}?, #{delegate_opts}
36
41
  eoruby
37
42
  end
38
43
  else
@@ -9,7 +9,7 @@ end
9
9
 
10
10
  class Parent < ActiveRecord::Base
11
11
  has_one :child
12
- delegate_all_for :child, except: [:three], also_include: [:extra]
12
+ delegate_all_for :child, except: [:three], also_include: [:extra], allow_nil: true
13
13
  def two; 'parent' end
14
14
  end
15
15
 
@@ -23,7 +23,7 @@ describe DelegateAllFor do
23
23
  end
24
24
  end
25
25
 
26
- context 'does not respond to three' do
26
+ describe ':except option' do
27
27
  it 'reader' do
28
28
  lambda { subject.three }.should raise_error NoMethodError
29
29
  end
@@ -35,9 +35,17 @@ describe DelegateAllFor do
35
35
  end
36
36
  end
37
37
 
38
- context 'delegates to child attributes' do
39
- its(:four) { should == 'four' }
38
+ describe ':also_include option' do
40
39
  its(:extra) { should be_true }
40
+ end
41
+
42
+ describe ':allow_nil option' do
43
+ subject { Parent.new }
44
+ its(:four) { should be_nil }
45
+ end
46
+
47
+ describe 'delegates to child attributes' do
48
+ its(:four) { should == 'four' }
41
49
  its(:two) { should == 'parent' }
42
50
 
43
51
  it 'does not delegate to association attributes' do
@@ -49,7 +57,7 @@ describe DelegateAllFor do
49
57
  end
50
58
  end
51
59
 
52
- context 'guards against user error' do
60
+ describe 'guards against user error' do
53
61
  it 'notices when an association is wrong' do
54
62
  lambda do
55
63
  class Parent < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delegate_all_for
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-04 00:00:00.000000000 Z
12
+ date: 2012-05-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70289233393260 !ruby/object:Gem::Requirement
16
+ requirement: &70104482200140 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.2.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70289233393260
24
+ version_requirements: *70104482200140
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70289233392460 !ruby/object:Gem::Requirement
27
+ requirement: &70104482199360 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.9.2.2
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70289233392460
35
+ version_requirements: *70104482199360
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70289233381860 !ruby/object:Gem::Requirement
38
+ requirement: &70104482198200 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.9.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70289233381860
46
+ version_requirements: *70104482198200
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sqlite3
49
- requirement: &70289233381000 !ruby/object:Gem::Requirement
49
+ requirement: &70104482197160 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 1.3.6
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70289233381000
57
+ version_requirements: *70104482197160
58
58
  description: Easy delegation of all columns of an ActiveRecord association
59
59
  email:
60
60
  - jason@lessonplanet.com