deserializer 0.6.0 → 0.7.0
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/Gemfile.lock +2 -2
- data/lib/deserializer.rb +2 -0
- data/lib/deserializer/attributable.rb +11 -11
- data/lib/deserializer/attribute/has_one_association.rb +0 -9
- data/lib/deserializer/attribute/value_attribute.rb +1 -10
- data/lib/deserializer/base.rb +6 -5
- data/lib/deserializer/version.rb +1 -1
- data/test/lib/deserializers.rb +7 -0
- data/test/unit/deserializer_test.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7ead03cc6f7509656922f6a300a7416730840dc
|
4
|
+
data.tar.gz: f10fff12d9341c2c51cfcf83d4374e1cfbf82d7a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f922a56a0beab0caffce4555722f93c0a41c4dbcd6d41fde5bd30875346409f49e12d8d20e316f324ff026b6c5b2fd3e0112f2a73495c8dd506b43722ae6c459
|
7
|
+
data.tar.gz: 37cd5ed9ea2990d5af586cf309a19e4bbed380b3881802dd6f33abe2e890e2af28fb5270e882db1caa9b72cec6ec176f403b5d8577a41d9e28efe80ec210eb82
|
data/Gemfile.lock
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
deserializer (0.
|
4
|
+
deserializer (0.6.0)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
activesupport (4.2.
|
10
|
+
activesupport (4.2.5)
|
11
11
|
i18n (~> 0.7)
|
12
12
|
json (~> 1.7, >= 1.7.7)
|
13
13
|
minitest (~> 5.1)
|
data/lib/deserializer.rb
CHANGED
@@ -7,24 +7,24 @@ module Deserializer
|
|
7
7
|
# deserializer interface functions
|
8
8
|
|
9
9
|
def attributes( *attrs )
|
10
|
-
self.__attrs
|
10
|
+
self.__attrs = __attrs.dup
|
11
11
|
attrs.each do |attr|
|
12
|
-
|
12
|
+
attribute( attr, {} )
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
def attribute( name, opts = {} )
|
17
|
-
|
18
|
-
__attrs
|
17
|
+
attribute = Attribute::Attribute.new( Attribute::ValueAttribute, name, opts )
|
18
|
+
self.__attrs = __attrs.merge attribute.key => attribute
|
19
19
|
end
|
20
20
|
|
21
21
|
def has_one( name, opts = {} )
|
22
22
|
unless opts[:deserializer]
|
23
23
|
raise DeserializerError, class: self, message: "has_one associations need a deserilaizer"
|
24
24
|
end
|
25
|
-
|
26
|
-
|
27
|
-
__attrs
|
25
|
+
|
26
|
+
attribute = Attribute::Attribute.new( Attribute::HasOneAssociation, name, opts )
|
27
|
+
self.__attrs = __attrs.merge attribute.key => attribute
|
28
28
|
end
|
29
29
|
|
30
30
|
def has_many( name, opts = {} )
|
@@ -32,8 +32,8 @@ module Deserializer
|
|
32
32
|
raise DeserializerError, class: self, message: "has_many associations need a deserilaizer"
|
33
33
|
end
|
34
34
|
|
35
|
-
|
36
|
-
__attrs
|
35
|
+
attribute = Attribute::Attribute.new( Attribute::HasManyAssociation, name, opts )
|
36
|
+
self.__attrs = __attrs.merge attribute.key => attribute
|
37
37
|
end
|
38
38
|
|
39
39
|
def belongs_to( *args )
|
@@ -45,8 +45,8 @@ module Deserializer
|
|
45
45
|
raise DeserializerError, class: self, message: "nested associations need a deserilaizer"
|
46
46
|
end
|
47
47
|
|
48
|
-
|
49
|
-
__attrs
|
48
|
+
attribute = Attribute::Attribute.new( Attribute::NestedAssociation, name, opts )
|
49
|
+
self.__attrs = __attrs.merge attribute.key => attribute
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -18,15 +18,6 @@ module Deserializer
|
|
18
18
|
if target == object.object
|
19
19
|
return value
|
20
20
|
|
21
|
-
# has_one :thing, deserializer: ThingDeserializer
|
22
|
-
# has_one :gniht, deserializer: GnihtDeserializer
|
23
|
-
#
|
24
|
-
# def thing
|
25
|
-
# object[:gniht]
|
26
|
-
# end
|
27
|
-
elsif target.is_a? Hash
|
28
|
-
return target.merge value
|
29
|
-
|
30
21
|
# has_one :thing, deserializer: GnihtDeserializer
|
31
22
|
#
|
32
23
|
# def thing
|
@@ -5,7 +5,7 @@ module Deserializer
|
|
5
5
|
def value( params )
|
6
6
|
value = params[key]
|
7
7
|
|
8
|
-
if opts[:ignore_empty] &&
|
8
|
+
if opts[:ignore_empty] && value.blank?
|
9
9
|
return :ignore
|
10
10
|
end
|
11
11
|
|
@@ -19,15 +19,6 @@ module Deserializer
|
|
19
19
|
|
20
20
|
value
|
21
21
|
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def empty?(value)
|
26
|
-
!value ||
|
27
|
-
value == "" ||
|
28
|
-
value == {} ||
|
29
|
-
value == []
|
30
|
-
end
|
31
22
|
end
|
32
23
|
end
|
33
24
|
end
|
data/lib/deserializer/base.rb
CHANGED
@@ -4,17 +4,18 @@ module Deserializer
|
|
4
4
|
## atribute, has_one, nested, etc associations
|
5
5
|
include Deserializer::Attributable
|
6
6
|
|
7
|
+
class_attribute :__attrs
|
8
|
+
self.__attrs = {}
|
7
9
|
class << self
|
8
|
-
|
9
|
-
|
10
|
+
|
10
11
|
# deserializer usage functions
|
11
12
|
|
12
13
|
def from_params( params = {} )
|
13
|
-
|
14
|
+
new( params ).deserialize
|
14
15
|
end
|
15
16
|
|
16
17
|
def permitted_params
|
17
|
-
|
18
|
+
__attrs.keys
|
18
19
|
end
|
19
20
|
end
|
20
21
|
|
@@ -25,7 +26,7 @@ module Deserializer
|
|
25
26
|
object ||= {}
|
26
27
|
|
27
28
|
# deserialize
|
28
|
-
self.class.__attrs.each do |attr|
|
29
|
+
self.class.__attrs.each do |_, attr|
|
29
30
|
object.merge!( attr.to_hash( params, self ) ) do |key, old_value, new_value|
|
30
31
|
# in the case that 2 has_ones merge into the same key. Not sure i want to support this
|
31
32
|
if old_value.is_a?( Hash ) && new_value.is_a?( Hash )
|
data/lib/deserializer/version.rb
CHANGED
data/test/lib/deserializers.rb
CHANGED
@@ -127,4 +127,11 @@ end
|
|
127
127
|
class HasManyDeserializer < Deserializer::Base
|
128
128
|
attribute :id
|
129
129
|
has_many :attributes, deserializer: AttributeDeserializer
|
130
|
+
end
|
131
|
+
|
132
|
+
class InheritedDeserializer < HasOneWithTargetDeserializer
|
133
|
+
attributes :this,
|
134
|
+
:that
|
135
|
+
|
136
|
+
attribute :created_by
|
130
137
|
end
|
@@ -174,4 +174,14 @@ class DeserializerTest < Minitest::Test
|
|
174
174
|
def test_nested_handles_no_input
|
175
175
|
assert_equal ({nested_object: {}}), NestableDeserializer.from_params( {} )
|
176
176
|
end
|
177
|
+
|
178
|
+
def test_can_inherit_from_other_deserializer
|
179
|
+
base_permitted_params = HasOneWithTargetDeserializer.permitted_params
|
180
|
+
|
181
|
+
assert_equal [:external, :thing], base_permitted_params, "inheritance broke the base class"
|
182
|
+
|
183
|
+
inherited_permitted_params = base_permitted_params + [:this, :that, :created_by]
|
184
|
+
|
185
|
+
assert_equal inherited_permitted_params, InheritedDeserializer.permitted_params
|
186
|
+
end
|
177
187
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deserializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Greg Orlov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-01-
|
11
|
+
date: 2016-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|