smooth_operator 1.10.19 → 1.10.20
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 +8 -8
- data/lib/smooth_operator/attribute_assignment.rb +2 -2
- data/lib/smooth_operator/attributes/base.rb +7 -3
- data/lib/smooth_operator/attributes/dirty.rb +6 -6
- data/lib/smooth_operator/attributes/normal.rb +2 -2
- data/lib/smooth_operator/model_schema.rb +1 -1
- data/lib/smooth_operator/version.rb +1 -1
- data/spec/smooth_operator/attribute_assignment_spec.rb +28 -0
- data/spec/support/models/user_with_address_and_posts.rb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NzJiNWZiMGU0ZTczYjY5OTM2ZTZlYTc4OGU2ZjdkMjQyYzA3NjljMA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
NzM0MmE2ZDJhZDYwZGVmNDAwMTNkMjMzZTFhOWQxZTQzYTEyNjE4Ng==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzhjODI0NTRkZWZiOTIzMDJiZWRhNjlkYTE1MmFjNmIzMDQxNTYwNjQ4MGVl
|
10
|
+
MTUxNGY4NTliNDc4YjA5MDA3MjAxMWM4ZWJkMzA3OGNmNDg4YjI0MjNmYjZm
|
11
|
+
MDJjNmFmNjU0YmVkYmUwMzE5YzUzM2IzOGQ5NjE5MDZmMDc0ZDQ=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OTZiNGE4NjhmYjE2Y2FiNjlmN2E5YjY0ZWVlMjE5ZjIzMzhkZWRiNTU4ZWMw
|
14
|
+
OTBkNzM1OGI5YTliMzE4MzE4NGY5MjA0ZTg2ZTUyMTE1MzdiZWI5ODBhY2Vk
|
15
|
+
NDk2MTIxMTkyNTIyN2E4Y2E3MjdiYzI1YzhlOTQ4NDBkN2Y0ZWE=
|
@@ -141,7 +141,7 @@ module SmoothOperator
|
|
141
141
|
|
142
142
|
def update_internal_data(attribute_name, attribute_value)
|
143
143
|
if self.class.dirty_attributes?
|
144
|
-
internal_data[attribute_name].set_value(attribute_value, self
|
144
|
+
internal_data[attribute_name].set_value(attribute_value, self)
|
145
145
|
else
|
146
146
|
internal_data[attribute_name] = new_attribute_object(attribute_name, attribute_value).value
|
147
147
|
end
|
@@ -150,7 +150,7 @@ module SmoothOperator
|
|
150
150
|
def new_attribute_object(attribute_name, attribute_value)
|
151
151
|
attribute_class = self.class.dirty_attributes? ? Attributes::Dirty : Attributes::Normal
|
152
152
|
|
153
|
-
attribute_class.new(attribute_name, attribute_value,
|
153
|
+
attribute_class.new(attribute_name, attribute_value, self)
|
154
154
|
end
|
155
155
|
|
156
156
|
end
|
@@ -5,10 +5,14 @@ module SmoothOperator
|
|
5
5
|
|
6
6
|
protected ##################### PROTECTED ########################
|
7
7
|
|
8
|
-
def cast_to_type(name, value,
|
8
|
+
def cast_to_type(name, value, parent_object)
|
9
|
+
known_by_schema, type, unknown_hash_class = parent_object.internal_structure.include?(name), parent_object.internal_structure[name], parent_object.class.unknown_hash_class
|
10
|
+
|
11
|
+
return Helpers.duplicate(value) if known_by_schema && type.nil?
|
12
|
+
|
9
13
|
case value
|
10
14
|
when Array
|
11
|
-
value.map { |array_entry| self.class.new(name, array_entry,
|
15
|
+
value.map { |array_entry| self.class.new(name, array_entry, parent_object).value }
|
12
16
|
when Hash
|
13
17
|
type.nil? ? new_unknown_hash(value, unknown_hash_class, parent_object) : type.new(value, parent_object: parent_object)
|
14
18
|
else
|
@@ -91,7 +95,7 @@ module SmoothOperator
|
|
91
95
|
hash = {}
|
92
96
|
|
93
97
|
attributes.each do |key, value|
|
94
|
-
hash[key] = cast_to_type(key, value,
|
98
|
+
hash[key] = cast_to_type(key, value, parent_object)
|
95
99
|
end
|
96
100
|
|
97
101
|
hash
|
@@ -3,16 +3,16 @@ module SmoothOperator
|
|
3
3
|
|
4
4
|
class Dirty < Base
|
5
5
|
|
6
|
-
attr_reader :original_name, :original_value, :first_value, :value
|
6
|
+
attr_reader :original_name, :original_value, :first_value, :value
|
7
7
|
|
8
|
-
def initialize(name, value,
|
9
|
-
@original_name, @original_value
|
8
|
+
def initialize(name, value, parent_object)
|
9
|
+
@original_name, @original_value = name, value
|
10
10
|
|
11
|
-
@first_value = set_value(value,
|
11
|
+
@first_value = set_value(value, parent_object)
|
12
12
|
end
|
13
13
|
|
14
|
-
def set_value(new_value,
|
15
|
-
@value = cast_to_type(original_name, new_value,
|
14
|
+
def set_value(new_value, parent_object)
|
15
|
+
@value = cast_to_type(original_name, new_value, parent_object)
|
16
16
|
end
|
17
17
|
|
18
18
|
def changed?
|
@@ -5,8 +5,8 @@ module SmoothOperator
|
|
5
5
|
|
6
6
|
attr_reader :value
|
7
7
|
|
8
|
-
def initialize(name, value,
|
9
|
-
@value = cast_to_type(name, value,
|
8
|
+
def initialize(name, value, parent_object)
|
9
|
+
@value = cast_to_type(name, value, parent_object)
|
10
10
|
end
|
11
11
|
|
12
12
|
end
|
@@ -129,6 +129,34 @@ describe SmoothOperator::AttributeAssignment do
|
|
129
129
|
context "when there is a known schema and the received hash has an attribute" do
|
130
130
|
subject { UserWithAddressAndPosts::Son }
|
131
131
|
|
132
|
+
context "that is declared (in schema) as an nil" do
|
133
|
+
|
134
|
+
it "when the attributes's value is '1', should return '1'" do
|
135
|
+
expect(subject.new(complex_field: '1').complex_field).to eq('1')
|
136
|
+
end
|
137
|
+
|
138
|
+
it "when the attributes's value is ['1', '2'], should return ['1', '2']" do
|
139
|
+
expect(subject.new(complex_field: ['1', '2']).complex_field).to eq(['1', '2'])
|
140
|
+
end
|
141
|
+
|
142
|
+
it "when the attributes's value is 1, should be converted to 1" do
|
143
|
+
expect(subject.new(complex_field: 1).complex_field).to eq(1)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "when the attributes's value is { first_name: ['1', '2'] }, should be converted to { first_name: ['1', '2'] }" do
|
147
|
+
expect(subject.new(complex_field: { first_name: ['1', '2'] }).complex_field).to eq({ first_name: ['1', '2'] })
|
148
|
+
end
|
149
|
+
|
150
|
+
it "when the attributes's value is -1, should be converted to -1" do
|
151
|
+
expect(subject.new(complex_field: -1).complex_field).to eq(-1)
|
152
|
+
end
|
153
|
+
|
154
|
+
it "when the attributes's value is 0.35, should be converted to 0.35" do
|
155
|
+
expect(subject.new(complex_field: 0.35).complex_field).to eq(0.35)
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
132
160
|
context "that is declared (in schema) as an integer" do
|
133
161
|
|
134
162
|
it "when the attributes's value is '1', should be converted to 1" do
|