simple_form_object 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.
- checksums.yaml +4 -4
- data/lib/simple_form_object.rb +40 -1
- data/lib/simple_form_object/version.rb +1 -1
- data/spec/delegation_spec.rb +52 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 38e75708032f5ece35823c27133168dd7fbb3e0b
|
4
|
+
data.tar.gz: b4f81546088524d43bb1c089fcea2faffb970672
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 09391560a4129904907e4226eacf3658629c0fa9444cfa136b2751f288cc230167959fd2029d11f9877b4e4307c3304e0291fb1dcf195dcba34414e84c00b2bd
|
7
|
+
data.tar.gz: 2f053446f2c8a30044e79e598e90e00d10b0e14718d086a1f081703efe11f2c33c592782db188adb8e654dee10cdbda77149e8fba5b9bd1903c9e6ba99b47800
|
data/lib/simple_form_object.rb
CHANGED
@@ -4,7 +4,6 @@ require "active_support"
|
|
4
4
|
|
5
5
|
module SimpleFormObject
|
6
6
|
extend ActiveSupport::Concern
|
7
|
-
|
8
7
|
include ActiveModel::Model
|
9
8
|
|
10
9
|
module ClassMethods
|
@@ -14,6 +13,14 @@ module SimpleFormObject
|
|
14
13
|
_attributes << Attribute.new(name, type, options)
|
15
14
|
end
|
16
15
|
|
16
|
+
def delegate_all(options = {})
|
17
|
+
@_delegation_target = options.fetch(:to)
|
18
|
+
end
|
19
|
+
|
20
|
+
def _delegation_target
|
21
|
+
@_delegation_target
|
22
|
+
end
|
23
|
+
|
17
24
|
def _attributes
|
18
25
|
@_attributes ||= []
|
19
26
|
end
|
@@ -27,6 +34,38 @@ module SimpleFormObject
|
|
27
34
|
end
|
28
35
|
end
|
29
36
|
|
37
|
+
def method_missing(method, *args, &block)
|
38
|
+
return super unless delegatable?(method)
|
39
|
+
|
40
|
+
# TODO: Figure out why self.class.delegate(method, to: self.class._delegation_target)
|
41
|
+
# doesn't work.
|
42
|
+
|
43
|
+
self.class.send(:define_method, method) do |*args, &block|
|
44
|
+
_delegation_target.send(method, *args, &block)
|
45
|
+
end
|
46
|
+
|
47
|
+
send(method, *args, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
def delegatable?(method)
|
51
|
+
|
52
|
+
if !_delegation_target.nil?
|
53
|
+
_delegation_target.respond_to?(method)
|
54
|
+
else
|
55
|
+
false
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def _delegation_target
|
60
|
+
target = self.class._delegation_target
|
61
|
+
|
62
|
+
if target.is_a? Symbol
|
63
|
+
self.send(target)
|
64
|
+
else
|
65
|
+
target
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
30
69
|
def column_for_attribute(attribute)
|
31
70
|
self.class._attribute(attribute).fake_column
|
32
71
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'SimpleFormObject delegation' do
|
4
|
+
let(:base_klass) { Class.new.tap{|k| k.include SimpleFormObject } }
|
5
|
+
let(:klass) { base_klass }
|
6
|
+
let(:instance) { klass.new }
|
7
|
+
|
8
|
+
describe '.delegate_all' do
|
9
|
+
let(:target) { double }
|
10
|
+
|
11
|
+
before do
|
12
|
+
klass.delegate_all to: target
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should delegate foo to the target' do
|
16
|
+
expect(target).to receive(:foo)
|
17
|
+
instance.foo
|
18
|
+
|
19
|
+
expect(target).to receive(:foo).with([1,2,3,4])
|
20
|
+
instance.foo([1,2,3,4])
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'a more complex delegation' do
|
25
|
+
let(:base_klass) do
|
26
|
+
Class.new do
|
27
|
+
include SimpleFormObject
|
28
|
+
|
29
|
+
def initialize(target_object, attributes = {})
|
30
|
+
@target_object = target_object
|
31
|
+
super(attributes)
|
32
|
+
end
|
33
|
+
|
34
|
+
attr_reader :target_object
|
35
|
+
|
36
|
+
delegate_all to: :target_object
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
let(:target) { double }
|
41
|
+
let(:instance) { klass.new(target) }
|
42
|
+
|
43
|
+
it 'should delegate foo to the target' do
|
44
|
+
expect(target).to receive(:foo)
|
45
|
+
instance.foo
|
46
|
+
|
47
|
+
expect(target).to receive(:foo).with([1,2,3,4])
|
48
|
+
instance.foo([1,2,3,4])
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
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.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Leonard Garvey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- lib/simple_form_object/version.rb
|
97
97
|
- simple_form_objects.gemspec
|
98
98
|
- spec/attribute_spec.rb
|
99
|
+
- spec/delegation_spec.rb
|
99
100
|
- spec/model_spec.rb
|
100
101
|
- spec/spec_helper.rb
|
101
102
|
homepage: http://github.com/reinteractive-open/simple_form_object
|
@@ -118,11 +119,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
119
|
version: '0'
|
119
120
|
requirements: []
|
120
121
|
rubyforge_project:
|
121
|
-
rubygems_version: 2.2.
|
122
|
+
rubygems_version: 2.2.2
|
122
123
|
signing_key:
|
123
124
|
specification_version: 4
|
124
125
|
summary: Simple form objects for simple form and rails
|
125
126
|
test_files:
|
126
127
|
- spec/attribute_spec.rb
|
128
|
+
- spec/delegation_spec.rb
|
127
129
|
- spec/model_spec.rb
|
128
130
|
- spec/spec_helper.rb
|