deserializer 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +38 -1
- data/lib/deserializer/base.rb +12 -4
- data/lib/deserializer/version.rb +1 -1
- data/test/lib/deserializers.rb +30 -0
- data/test/unit/deserializer_test.rb +21 -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: d2d925b16aaf70730f68ed799b22236a2b4a9c1b
|
4
|
+
data.tar.gz: 291d5ba90c44ddd31eeab94c314f16f414ecf7df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df1caed559319d4c42f6770408172d600c8511260126a8eec742d645586d14861e8468999f094d195f0a9462c0bf09efe2afc3f289166f3c42f330e750e96e1d
|
7
|
+
data.tar.gz: 2a87db6f696784599033d89f4b41abad6bb2094890f7a403c83182afab53094c0714a7ff8d276a0a113c00a9e9b9990498f5b723721c5b23dc317dee305e36c5
|
data/README.md
CHANGED
@@ -130,7 +130,7 @@ with params `{"title" => "lorem", "body" => "ipsum"}`, will give you a hash of `
|
|
130
130
|
`attribute` is the singular version of `attributes`, but like `ActiveModel::Serializer` it can take a `:key`
|
131
131
|
```ruby
|
132
132
|
class PostDeserializer < Deserializer::Base
|
133
|
-
attribute :title
|
133
|
+
attribute :title, ignore_empty: true
|
134
134
|
attribute :body, key: :text
|
135
135
|
end
|
136
136
|
```
|
@@ -138,6 +138,43 @@ It is symmetric with `ActiveModel::Serializer`, so that :text is what it will ge
|
|
138
138
|
|
139
139
|
For example with params of `{"title" => "lorem", "text" => "ipsum"}` this desrerializer will produce `{title: "lorem", body: "ipsum"}`.
|
140
140
|
|
141
|
+
`ignore_empty` is an option to ignore `false`/`nil`/`""`/`[]`/`{}` values that may come into the deserializer. By defualt it will pass the value through. With this option, it will drop the key from the result, turning `{"title" => "", "text" => nil}` into `{}`
|
142
|
+
|
143
|
+
`convert_with` allows the deserializer to deserialize and convert a value at the same time. For example, if we have a `Post` model that looks like
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
class Post < ActiveRecord::Base
|
147
|
+
belongs_to :post_type # this is a domain table
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
and we serialize with
|
152
|
+
```ruby
|
153
|
+
class PostSerializer < ActiveModel::Serializer
|
154
|
+
attribute :type
|
155
|
+
|
156
|
+
def type
|
157
|
+
object.post_type.symbolic_name
|
158
|
+
end
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
Then, when we if we get a symbolic name from the controller, but want to work with an id in the backend, we can do something like:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
class PostDeserializer < Deserializer::Base
|
166
|
+
attribute :title, ignore_empty: true
|
167
|
+
attribute :body
|
168
|
+
attribute :post_type_id, key: type, convert_with: to_type_id
|
169
|
+
|
170
|
+
def to_type_id(value)
|
171
|
+
Type.find_by_symbolic_name.id
|
172
|
+
end
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
which would take the params `{"title" => "lorem", "body" => "ipsum", "type" => "BLAGABLAG"}` and produce `{title: "lorem", body: "ipsum", post_type_id: 1}`
|
177
|
+
|
141
178
|
|
142
179
|
#### has_one
|
143
180
|
NOTE: This is the only association currently supported by `Deserializer`.
|
data/lib/deserializer/base.rb
CHANGED
@@ -101,10 +101,18 @@ module Deserializer
|
|
101
101
|
def assign_value( attribute, value, options = {} )
|
102
102
|
if options[:ignore_empty] && empty?(value)
|
103
103
|
return
|
104
|
-
# other options go here
|
105
|
-
else
|
106
|
-
self.object[attribute] = value
|
107
104
|
end
|
105
|
+
if options[:convert_with]
|
106
|
+
method = options[:convert_with]
|
107
|
+
if self.respond_to? method
|
108
|
+
self.object[attribute] = self.send method, value
|
109
|
+
return
|
110
|
+
end
|
111
|
+
end
|
112
|
+
# other options go here
|
113
|
+
|
114
|
+
self.object[attribute] = value
|
115
|
+
|
108
116
|
end
|
109
117
|
|
110
118
|
def empty?(value)
|
@@ -114,4 +122,4 @@ module Deserializer
|
|
114
122
|
value == []
|
115
123
|
end
|
116
124
|
end
|
117
|
-
end
|
125
|
+
end
|
data/lib/deserializer/version.rb
CHANGED
data/test/lib/deserializers.rb
CHANGED
@@ -80,4 +80,34 @@ class HasOneWithObjectTargetDeserializer < Deserializer::Base
|
|
80
80
|
def other_thing
|
81
81
|
object
|
82
82
|
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class ConversionDeserializer < Deserializer::Base
|
86
|
+
attribute :real_range, convert_with: :to_range
|
87
|
+
attribute :bad_range, convert_with: :to_range
|
88
|
+
|
89
|
+
def to_range(value)
|
90
|
+
range_array = Array(value)
|
91
|
+
(range_array[0]..range_array[-1])
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class KeyedConversionDeserializer < Deserializer::Base
|
96
|
+
attribute :real_range, convert_with: :to_range, key: :real
|
97
|
+
attribute :bad_range, convert_with: :to_range, key: :bad
|
98
|
+
|
99
|
+
def to_range(value)
|
100
|
+
range_array = Array(value)
|
101
|
+
(range_array[0]..range_array[-1])
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class NillableConversionDeserializer < Deserializer::Base
|
106
|
+
attribute :real_range, convert_with: :to_range, key: :real, ignore_empty: true
|
107
|
+
attribute :bad_range, convert_with: :to_range, key: :bad, ignore_empty: true
|
108
|
+
|
109
|
+
def to_range(value)
|
110
|
+
range_array = Array(value)
|
111
|
+
(range_array[0]..range_array[-1])
|
112
|
+
end
|
83
113
|
end
|
@@ -121,4 +121,25 @@ class DeserializerTest < Minitest::Test
|
|
121
121
|
BasicDeserializer.belongs_to :explody
|
122
122
|
end
|
123
123
|
end
|
124
|
+
|
125
|
+
def test_supports_conversions
|
126
|
+
expected = { real_range: (1..4), bad_range: (1..1)}
|
127
|
+
params = { real_range: [1, 12, 4], bad_range: 1}
|
128
|
+
|
129
|
+
assert_equal expected, ConversionDeserializer.from_params( params )
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_supports_conversions_with_key
|
133
|
+
expected = { real_range: (1..4), bad_range: (1..1)}
|
134
|
+
params = { real: [1, 4], bad: 1}
|
135
|
+
|
136
|
+
assert_equal expected, KeyedConversionDeserializer.from_params( params )
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_supports_conversions_with_ignore_empty
|
140
|
+
expected = { real_range: (1..4)}
|
141
|
+
params = { real: [1, 4], bad: nil}
|
142
|
+
|
143
|
+
assert_equal expected, NillableConversionDeserializer.from_params( params )
|
144
|
+
end
|
124
145
|
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.3.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: 2015-
|
11
|
+
date: 2015-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|