delegate_all_for 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module DelegateAllFor
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -28,16 +28,33 @@ module DelegateAllFor
28
28
  exclude_columns = self.column_names.dup.concat(options[:except].map(&:to_s))
29
29
  attr_names.each do |association_name|
30
30
  if reflection = reflect_on_association(association_name)
31
- delegate_opts = options.slice(:prefix, :allow_nil).merge(to: association_name)
31
+ to = association_name
32
+ delegate_opts = options.slice(:prefix, :allow_nil).merge(to: to)
33
+ method_prefix = delegate_opts[:prefix] ? "#{prefix == true ? to : prefix}_" : ''
34
+
32
35
  options[:also_include].each do |m|
33
36
  class_eval(%{delegate :#{m}, #{delegate_opts}})
34
37
  end
35
- (reflection.klass.column_names - exclude_columns).each do |column_name|
36
- next if column_name.in?(reflection.foreign_key, 'updated_at', 'updated_on', 'created_at', 'created_on')
38
+
39
+ (reflection.klass.column_names - exclude_columns).each do |method|
40
+ next if method.in?(reflection.foreign_key, 'updated_at', 'updated_on', 'created_at', 'created_on')
41
+ class_eval <<-eoruby, __FILE__, __LINE__ + 1
42
+ delegate :#{method}, #{delegate_opts}
43
+ delegate :#{method}?, #{delegate_opts}
44
+ eoruby
45
+
46
+ # Create the setter with support for using nested attributes to set it if the delegated object is not present
47
+ exception = %(raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
37
48
  class_eval <<-eoruby, __FILE__, __LINE__ + 1
38
- delegate :#{column_name}, #{delegate_opts}
39
- delegate :#{column_name}=, #{delegate_opts}
40
- delegate :#{column_name}?, #{delegate_opts}
49
+ def #{method_prefix}#{method}=(*args, &block) # def customer_name(*args, &block)
50
+ if #{to} || #{to}.respond_to?(:#{method}) # if client || client.respond_to?(:name)
51
+ #{to}.__send__(:#{method}=, *args, &block) # client.__send__(:name, *args, &block)
52
+ elsif self.respond_to?(:#{to}_attributes=) # elsif self.respond_to?(:client_attributes=)
53
+ self.__send__(:#{to}_attributes=, :#{method} => args.first, &block) # self.__send__(:client_attributes=, :name => args.first, &block)
54
+ else # else
55
+ #{exception} # # add helpful exception
56
+ end # end
57
+ end # end
41
58
  eoruby
42
59
  end
43
60
  else
@@ -55,6 +55,29 @@ describe DelegateAllFor do
55
55
  it 'does not delegate to timestamp attributes' do
56
56
  lambda { subject.created_at }.should raise_error NoMethodError
57
57
  end
58
+
59
+ describe 'writer' do
60
+ it 'sets the value using delegation if child is set' do
61
+ subject.child.should be_an_instance_of(Child)
62
+ subject.four = 'FOUR'
63
+ subject.four.should == 'FOUR'
64
+ subject.child.four.should == 'FOUR'
65
+ end
66
+
67
+ it 'sets the value using nested attributes if child is not set and nested attributes are supported' do
68
+ class NestedParent < Parent; accepts_nested_attributes_for :child end
69
+ subject = NestedParent.new
70
+ subject.four = 'FOUR'
71
+ subject.child.should be_an_instance_of(Child)
72
+ subject.four.should == 'FOUR'
73
+ subject.child.four.should == 'FOUR'
74
+ end
75
+
76
+ it 'raises an exception if child is not set' do
77
+ subject = Parent.new
78
+ lambda { subject.four = 'FOUR' }.should raise_error RuntimeError
79
+ end
80
+ end
58
81
  end
59
82
 
60
83
  describe 'guards against user error' do
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.6
4
+ version: 0.0.7
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-08 00:00:00.000000000 Z
12
+ date: 2012-05-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70104482200140 !ruby/object:Gem::Requirement
16
+ requirement: &70148514385220 !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: *70104482200140
24
+ version_requirements: *70148514385220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &70104482199360 !ruby/object:Gem::Requirement
27
+ requirement: &70148514384040 !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: *70104482199360
35
+ version_requirements: *70148514384040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70104482198200 !ruby/object:Gem::Requirement
38
+ requirement: &70148514383580 !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: *70104482198200
46
+ version_requirements: *70148514383580
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: sqlite3
49
- requirement: &70104482197160 !ruby/object:Gem::Requirement
49
+ requirement: &70148514383080 !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: *70104482197160
57
+ version_requirements: *70148514383080
58
58
  description: Easy delegation of all columns of an ActiveRecord association
59
59
  email:
60
60
  - jason@lessonplanet.com