ializer 0.8.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -1
- data/ializer.gemspec +2 -2
- data/lib/de/ser/ializer.rb +2 -2
- data/lib/ializer/version.rb +1 -1
- data/lib/ser/ializer/field.rb +3 -1
- data/lib/ser/ializer.rb +45 -14
- metadata +16 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c00a69cfd664143494407679066729ac9251cbf300d6168d1a6fa43dec1966c2
|
4
|
+
data.tar.gz: 0dc7bb3dcc2354ed6faf3d677297194aa730ea5a5f72547796eb786b09972722
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ccbcefea07e0df88dd2114fa53df0e98e27da13a2eaaeb54fff01663055f8a6304c9d39f0c691318cd5a54d7abaef6c9bb48f7b5068dfe8a6c33de0bcde67d5
|
7
|
+
data.tar.gz: d7cb6b38f6c6408131552a5dc93aa48bd70582ed6a0af1720cb326323bdf30db29b65e01d5b69f824f16f578a8961764371191eaf40ab9bcaa8cbf284655b875
|
data/README.md
CHANGED
@@ -292,7 +292,7 @@ class OrderDeSer < De::Ser::Ializer
|
|
292
292
|
|
293
293
|
nested :items, deser: OrderItemDeSer, model_class: OrderItem
|
294
294
|
# OR
|
295
|
-
property :items, deser: OrderItemDeSer,
|
295
|
+
property :items, deser: OrderItemDeSer, model_class: OrderItem
|
296
296
|
|
297
297
|
nested :customer, model_class: Customer do
|
298
298
|
string :name
|
@@ -458,6 +458,27 @@ end
|
|
458
458
|
CustomerDeSer.serialize(order, current_user)
|
459
459
|
```
|
460
460
|
|
461
|
+
#### Using the context to serialize a subset of attributes
|
462
|
+
|
463
|
+
There are special keywords/method names on the Serialization Context that can be used to limit the attributes that are serialized. This is different from conditional attributes below. The conditions would still apply to the subset.
|
464
|
+
|
465
|
+
If your serialization context is a `Hash`, you can use the hash keys `:attributes` or `:include` to define the limited subset of attributes for serialization.
|
466
|
+
|
467
|
+
```ruby
|
468
|
+
CustomerDeSer.serialize(order, attributes: [:name])
|
469
|
+
```
|
470
|
+
|
471
|
+
If your serialization context is a ruby object, a method named `attributes` that returns an array of attribute names can be used.
|
472
|
+
|
473
|
+
```ruby
|
474
|
+
class AttributeSubsetContext
|
475
|
+
attr_accessor :attributes
|
476
|
+
end
|
477
|
+
|
478
|
+
context = AttributeSubsetContext.new(attributes: [:name])
|
479
|
+
CustomerDeSer.serialize(order, context)
|
480
|
+
```
|
481
|
+
|
461
482
|
### Conditional Attributes
|
462
483
|
|
463
484
|
Conditional attributes can be defined by passing a Proc to the `if` key on the `property` method. Return `truthy` if the attribute should be serialized, and `falsey` if not. The record and any params passed to the serializer are available inside the Proc as the first and second parameters, respectively.
|
data/ializer.gemspec
CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.add_development_dependency 'pry'
|
29
29
|
spec.add_development_dependency 'rake'
|
30
30
|
spec.add_development_dependency 'rspec'
|
31
|
-
spec.add_development_dependency 'rubocop'
|
32
|
-
spec.add_development_dependency 'rubocop-rspec'
|
31
|
+
spec.add_development_dependency 'rubocop', '~> 1.19'
|
32
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 2.8'
|
33
33
|
spec.add_development_dependency 'simplecov'
|
34
34
|
end
|
data/lib/de/ser/ializer.rb
CHANGED
@@ -35,7 +35,7 @@ module De
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def parse_attribute(object, key, value)
|
38
|
-
field =
|
38
|
+
field = attributes[key]
|
39
39
|
|
40
40
|
return unless field
|
41
41
|
|
@@ -77,7 +77,7 @@ module De
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def parse_ostruct_field(object, key, value)
|
80
|
-
field =
|
80
|
+
field = attributes[key]
|
81
81
|
|
82
82
|
return unless field
|
83
83
|
|
data/lib/ializer/version.rb
CHANGED
data/lib/ser/ializer/field.rb
CHANGED
@@ -11,7 +11,7 @@ module Ser
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
attr_reader :name, :setter, :key, :deser, :model_class, :if_condition, :block
|
14
|
+
attr_reader :name, :setter, :key, :deser, :model_class, :if_condition, :block, :description, :documentation
|
15
15
|
|
16
16
|
def initialize(name, options, config, &block)
|
17
17
|
@name = name
|
@@ -20,6 +20,8 @@ module Ser
|
|
20
20
|
@deser = options[:deser]
|
21
21
|
@if_condition = options[:if]
|
22
22
|
@model_class = options[:model_class]
|
23
|
+
@description = options[:desc]
|
24
|
+
@documentation = options[:documentation]
|
23
25
|
@block = block
|
24
26
|
end
|
25
27
|
|
data/lib/ser/ializer.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'active_support/hash_with_indifferent_access'
|
4
|
+
require 'active_support/core_ext/hash'
|
5
|
+
|
3
6
|
module Ser
|
4
7
|
class Ializer # rubocop:disable Metrics/ClassLength
|
5
8
|
@@method_registry = {} # rubocop:disable Style/ClassVars
|
@@ -44,7 +47,7 @@ module Ser
|
|
44
47
|
end
|
45
48
|
|
46
49
|
def with(deser)
|
47
|
-
deser.
|
50
|
+
deser.attributes.each_value do |field|
|
48
51
|
add_composed_attribute(field, deser)
|
49
52
|
end
|
50
53
|
end
|
@@ -73,6 +76,8 @@ module Ser
|
|
73
76
|
matchers.each do |matcher|
|
74
77
|
method_registry[matcher.to_s.to_sym] = method_name
|
75
78
|
end
|
79
|
+
|
80
|
+
deser_types[deser.to_s] = method_name.capitalize
|
76
81
|
end
|
77
82
|
|
78
83
|
def register_default(deser)
|
@@ -86,8 +91,22 @@ module Ser
|
|
86
91
|
end
|
87
92
|
end
|
88
93
|
|
94
|
+
# Maps a registered deser to the value type that it serializes
|
95
|
+
def deser_types
|
96
|
+
@deser_types ||= {}
|
97
|
+
end
|
98
|
+
|
89
99
|
def attribute_names
|
90
|
-
|
100
|
+
attributes.values.map(&:name)
|
101
|
+
end
|
102
|
+
|
103
|
+
def attributes
|
104
|
+
@attributes ||=
|
105
|
+
if equal? Ser::Ializer
|
106
|
+
ActiveSupport::HashWithIndifferentAccess.new
|
107
|
+
else
|
108
|
+
superclass.attributes.dup
|
109
|
+
end
|
91
110
|
end
|
92
111
|
|
93
112
|
protected
|
@@ -108,15 +127,6 @@ module Ser
|
|
108
127
|
method(method_name)
|
109
128
|
end
|
110
129
|
|
111
|
-
def _attributes
|
112
|
-
@attributes ||=
|
113
|
-
if equal? Ser::Ializer
|
114
|
-
{}
|
115
|
-
else
|
116
|
-
superclass._attributes.dup
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
130
|
private
|
121
131
|
|
122
132
|
def warning_message(name)
|
@@ -124,7 +134,7 @@ module Ser
|
|
124
134
|
end
|
125
135
|
|
126
136
|
def add_attribute(field)
|
127
|
-
|
137
|
+
attributes[field.key] = field
|
128
138
|
|
129
139
|
define_singleton_method field.name do |object, context|
|
130
140
|
value = field.get_value(object, context)
|
@@ -134,7 +144,7 @@ module Ser
|
|
134
144
|
end
|
135
145
|
|
136
146
|
def add_composed_attribute(field, deser)
|
137
|
-
|
147
|
+
attributes[field.key] = field
|
138
148
|
|
139
149
|
define_singleton_method field.name do |object, context|
|
140
150
|
deser.public_send(field.name, object, context)
|
@@ -152,7 +162,7 @@ module Ser
|
|
152
162
|
end
|
153
163
|
|
154
164
|
def serialize_one(object, context)
|
155
|
-
|
165
|
+
fields_for_serialization(context).each_with_object({}) do |field, data|
|
156
166
|
next unless field.valid_for_context?(object, context)
|
157
167
|
|
158
168
|
value = public_send(field.name, object, context)
|
@@ -163,6 +173,27 @@ module Ser
|
|
163
173
|
end
|
164
174
|
end
|
165
175
|
|
176
|
+
def fields_for_serialization(context)
|
177
|
+
field_names = fields_names_for_serialization(context)
|
178
|
+
|
179
|
+
return attributes.values unless field_names
|
180
|
+
|
181
|
+
attributes.values_at(*field_names).compact
|
182
|
+
end
|
183
|
+
|
184
|
+
def fields_names_for_serialization(context)
|
185
|
+
return nil unless context
|
186
|
+
|
187
|
+
if context.is_a?(Hash)
|
188
|
+
context = context.with_indifferent_access
|
189
|
+
return context[:attributes] || context[:include]
|
190
|
+
end
|
191
|
+
|
192
|
+
return context.attributes if context.respond_to?(:attributes)
|
193
|
+
|
194
|
+
nil
|
195
|
+
end
|
196
|
+
|
166
197
|
def valid_enumerable?(object)
|
167
198
|
return true if object.is_a? Array
|
168
199
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Steinberg
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -98,30 +98,30 @@ dependencies:
|
|
98
98
|
name: rubocop
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- - "
|
101
|
+
- - "~>"
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: '
|
103
|
+
version: '1.19'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - "
|
108
|
+
- - "~>"
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: '
|
110
|
+
version: '1.19'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: rubocop-rspec
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- - "
|
115
|
+
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
117
|
+
version: '2.8'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- - "
|
122
|
+
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: '
|
124
|
+
version: '2.8'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: simplecov
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,8 +136,8 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
-
description:
|
140
|
-
email:
|
139
|
+
description:
|
140
|
+
email:
|
141
141
|
executables:
|
142
142
|
- console
|
143
143
|
- setup
|
@@ -178,7 +178,7 @@ homepage: https://github.com/jsteinberg/ializer
|
|
178
178
|
licenses:
|
179
179
|
- MIT
|
180
180
|
metadata: {}
|
181
|
-
post_install_message:
|
181
|
+
post_install_message:
|
182
182
|
rdoc_options: []
|
183
183
|
require_paths:
|
184
184
|
- lib
|
@@ -193,8 +193,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
193
|
- !ruby/object:Gem::Version
|
194
194
|
version: '0'
|
195
195
|
requirements: []
|
196
|
-
rubygems_version: 3.0.
|
197
|
-
signing_key:
|
196
|
+
rubygems_version: 3.0.9
|
197
|
+
signing_key:
|
198
198
|
specification_version: 4
|
199
199
|
summary: Simple (de)serializers for Ruby objects
|
200
200
|
test_files: []
|