simple_form_object 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38e75708032f5ece35823c27133168dd7fbb3e0b
4
- data.tar.gz: b4f81546088524d43bb1c089fcea2faffb970672
3
+ metadata.gz: 73485885f010300cc9a362e85612e3f279f52926
4
+ data.tar.gz: 908ce72ef544b469e02cbd9328065f0a18a28b4d
5
5
  SHA512:
6
- metadata.gz: 09391560a4129904907e4226eacf3658629c0fa9444cfa136b2751f288cc230167959fd2029d11f9877b4e4307c3304e0291fb1dcf195dcba34414e84c00b2bd
7
- data.tar.gz: 2f053446f2c8a30044e79e598e90e00d10b0e14718d086a1f081703efe11f2c33c592782db188adb8e654dee10cdbda77149e8fba5b9bd1903c9e6ba99b47800
6
+ metadata.gz: f761ced4db494a667e2734d2fe591c0e5756ad545ad9d2eab0adc6ecd73c8cf4aff623600484e90e42be89099f207c8c2b593f41af74872752d70a9bf2f68b40
7
+ data.tar.gz: 5d819b5438d3d7caccddbf3231b3cc9e4ec2715712a532077547c2619340de44ba553226ef9a0c50a01ecf3afe721f65351172034601064d976fba935a32b1b5
@@ -1,3 +1,3 @@
1
1
  module SimpleFormObject
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -102,12 +102,20 @@ module SimpleFormObject
102
102
 
103
103
  def apply_default_to(form)
104
104
  if form.send(@name).nil?
105
- form.send("#{@name}=", @default) if @apply_default
105
+ form.send("#{@name}=", default_value(form)) if @apply_default
106
106
  end
107
107
  end
108
108
 
109
109
  private
110
110
 
111
+ def default_value(context)
112
+ if @default.respond_to?(:call)
113
+ context.instance_eval(&@default)
114
+ else
115
+ @default
116
+ end
117
+ end
118
+
111
119
  def extract_options
112
120
  @apply_default = true
113
121
  @default = options.fetch(:default) { @apply_default = false; nil }
@@ -54,5 +54,23 @@ describe SimpleFormObject::Attribute do
54
54
  end
55
55
  end
56
56
 
57
+ context 'with a lambda/proc default' do
58
+ let(:default) { Proc.new { :bar } }
59
+
60
+ it 'calls the lambda' do
61
+ expect(form).to receive("#{name}=").with(:bar)
62
+ attribute.apply_default_to(form)
63
+ end
64
+
65
+ context 'when the proc references the form' do
66
+ let(:default) { Proc.new { current_foo } }
67
+ let(:form) { double('Form', foo: nil, current_foo: :bar) }
68
+
69
+ it 'calls the lambda' do
70
+ expect(form).to receive("#{name}=").with(:bar)
71
+ attribute.apply_default_to(form)
72
+ end
73
+ end
74
+ end
57
75
  end
58
76
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leonard Garvey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-10 00:00:00.000000000 Z
11
+ date: 2015-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel