setsumei 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
@@ -11,7 +11,7 @@ module Setsumei
|
|
11
11
|
attr_accessor :klass, :options
|
12
12
|
|
13
13
|
def set_value_on(object, options = {})
|
14
|
-
return nil if options.empty? || options[:from_value_in].
|
14
|
+
return nil if options.empty? || options[:from_value_in].nil?
|
15
15
|
|
16
16
|
array( extract_from_hash options[:from_value_in] ).each do |data|
|
17
17
|
object << Build.a(klass, from: data)
|
@@ -23,7 +23,7 @@ module Setsumei
|
|
23
23
|
[thing].flatten(1).compact
|
24
24
|
end
|
25
25
|
def extract_from_hash(hash)
|
26
|
-
hash[ extract_key_for hash ]
|
26
|
+
(options[:direct_collection] && hash) || hash[ extract_key_for hash ]
|
27
27
|
end
|
28
28
|
def extract_key_for(hash)
|
29
29
|
Build::Key.for configured_key, given: hash_keys(hash)
|
data/lib/setsumei/version.rb
CHANGED
@@ -19,6 +19,63 @@ module Setsumei
|
|
19
19
|
its(:options) { should == options }
|
20
20
|
end
|
21
21
|
|
22
|
+
describe "#set_value_on(object, from_value_in: data) where data is a direct single or collection of values" do
|
23
|
+
let(:object) { mock "object", :<< => nil }
|
24
|
+
|
25
|
+
let(:klass) { mock "a klass" }
|
26
|
+
let(:collection) { Collection.of klass, from_value_in: data, direct_collection: true }
|
27
|
+
|
28
|
+
subject { collection.set_value_on object, from_value_in: data }
|
29
|
+
|
30
|
+
context "nil value" do
|
31
|
+
let(:data) { nil }
|
32
|
+
it "should protect against instances where there are attributes, but not for our defined element keys" do
|
33
|
+
object.should_not_receive(:<<)
|
34
|
+
subject
|
35
|
+
end
|
36
|
+
end
|
37
|
+
context "single value" do
|
38
|
+
let(:data) { mock "data" }
|
39
|
+
let(:single_instance) { mock "single_instance" }
|
40
|
+
|
41
|
+
before { Build.stub(:a).and_return single_instance }
|
42
|
+
|
43
|
+
it "should build a single instance of klass" do
|
44
|
+
Build.should_receive(:a).with(klass, from: data).and_return(single_instance)
|
45
|
+
subject
|
46
|
+
end
|
47
|
+
it "should append this to the object" do
|
48
|
+
object.should_receive(:<<).with(single_instance)
|
49
|
+
subject
|
50
|
+
end
|
51
|
+
end
|
52
|
+
context "multiple values" do
|
53
|
+
let(:a_value) { mock "a_value" }
|
54
|
+
let(:another_value) { mock "another_value" }
|
55
|
+
let(:more_values) { mock "more_values" }
|
56
|
+
|
57
|
+
let(:first_instance) { mock "first_instance" }
|
58
|
+
let(:second_instance) { mock "second_instance" }
|
59
|
+
let(:final_instance) { mock "final_instance" }
|
60
|
+
|
61
|
+
let(:data) { [a_value,another_value,more_values] }
|
62
|
+
|
63
|
+
before { Build.stub(:a).and_return( first_instance, second_instance, final_instance ) }
|
64
|
+
|
65
|
+
it "should build multiple instances of klass" do
|
66
|
+
Build.should_receive(:a).with(klass, from: a_value)#and_return(first_instance)
|
67
|
+
Build.should_receive(:a).with(klass, from: another_value)#and_return(second_instance)
|
68
|
+
Build.should_receive(:a).with(klass, from: more_values)#and_return(final_instance)
|
69
|
+
subject
|
70
|
+
end
|
71
|
+
it "should append all of these to the object" do
|
72
|
+
object.should_receive(:<<).with(first_instance)
|
73
|
+
object.should_receive(:<<).with(second_instance)
|
74
|
+
object.should_receive(:<<).with(final_instance)
|
75
|
+
subject
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
22
79
|
describe "#set_value_on(object, from_value_in: hash)" do
|
23
80
|
let(:object) { mock "object" }
|
24
81
|
let(:hash) { Hash.new }
|