ializer 0.9.1 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 436adbeed83a5f03cb2f5efe5f1ed07f3e7647e7fa15732e7af92923bc6165f5
4
- data.tar.gz: c4d2dbc6a03164d51eb577a6a1fe084f35dca996739b75c6d78d7760dfb434cc
3
+ metadata.gz: 046653c46495303f818fed936f4017c82a8c951882ab704630b2eeb0f1ec0729
4
+ data.tar.gz: 71f59e15ab4fb65620a9b589e2f8b6711b0d3e16f9e3b39e86492d200c5a5224
5
5
  SHA512:
6
- metadata.gz: b563551fb9ebdb39f866c6c1fe58977b9e5c074d1ba4bf0e5a3d040efe191056f4728549e0e36cc34b6f33801aecd83638885c86f04affb07c2c28726ca2f38e
7
- data.tar.gz: 82aa9bb2072c937b1f23464418744eb7fff88cb90ea8d97b2ce5f9e99249aa71e59f6050b5da6d014bb9edc5d62c6561b127ac14204502ead6b5cfecd3a1dd89
6
+ metadata.gz: 14305639f9d74861e2e5013fb9dfe22bb8e6ba82c6d6cbf52d8c7f2f753205f04bdccf34f99939213df70f71b1db002841fcc7efe01c8ac8cc5cd43a2c40628e
7
+ data.tar.gz: 6cab0473cd8e107c8be3b63a1aade57b69f2464304e48c7ba73f43f559ee6c0bbcf97fcaec1b9aeca66bf15fe4bb1c77cac208b4725a5f421cfb8adcf8391d14
data/README.md CHANGED
@@ -427,6 +427,18 @@ class Customer < De::Ser::Ializer
427
427
  end
428
428
  ```
429
429
 
430
+ Setters can also be overridden:
431
+
432
+ ```ruby
433
+ class Customer < De::Ser::Ializer
434
+ string :name
435
+
436
+ def self.name=(object, value)
437
+ object.name = value.delete_suffix('Part 2')
438
+ end
439
+ end
440
+ ```
441
+
430
442
  Attributes can also use a different name by passing the original method or accessor with a proc shortcut:
431
443
 
432
444
  ```ruby
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', '~> 0.78.0'
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
@@ -35,7 +35,7 @@ module De
35
35
  end
36
36
 
37
37
  def parse_attribute(object, key, value)
38
- field = _attributes[key]
38
+ field = attributes[key]
39
39
 
40
40
  return unless field
41
41
 
@@ -52,6 +52,14 @@ module De
52
52
 
53
53
  private
54
54
 
55
+ def add_attribute(field)
56
+ super
57
+
58
+ define_singleton_method field.setter do |object, parsed_value|
59
+ object.public_send(field.setter, parsed_value)
60
+ end
61
+ end
62
+
55
63
  def parse_field(object, field, value)
56
64
  return if value.nil?
57
65
 
@@ -59,7 +67,7 @@ module De
59
67
 
60
68
  return if parsed_value.nil?
61
69
 
62
- object.public_send(field.setter, parsed_value)
70
+ public_send(field.setter, object, parsed_value)
63
71
  end
64
72
 
65
73
  def ostruct?(model_class)
@@ -77,7 +85,7 @@ module De
77
85
  end
78
86
 
79
87
  def parse_ostruct_field(object, key, value)
80
- field = _attributes[key]
88
+ field = attributes[key]
81
89
 
82
90
  return unless field
83
91
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ializer
4
- VERSION = '0.9.1'
4
+ VERSION = '0.12.0'
5
5
  end
@@ -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
@@ -47,7 +47,7 @@ module Ser
47
47
  end
48
48
 
49
49
  def with(deser)
50
- deser._attributes.values.each do |field|
50
+ deser.attributes.each_value do |field|
51
51
  add_composed_attribute(field, deser)
52
52
  end
53
53
  end
@@ -76,6 +76,8 @@ module Ser
76
76
  matchers.each do |matcher|
77
77
  method_registry[matcher.to_s.to_sym] = method_name
78
78
  end
79
+
80
+ deser_types[deser.to_s] = method_name.capitalize
79
81
  end
80
82
 
81
83
  def register_default(deser)
@@ -89,8 +91,22 @@ module Ser
89
91
  end
90
92
  end
91
93
 
94
+ # Maps a registered deser to the value type that it serializes
95
+ def deser_types
96
+ @deser_types ||= {}
97
+ end
98
+
92
99
  def attribute_names
93
- _attributes.values.map(&:name)
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
94
110
  end
95
111
 
96
112
  protected
@@ -111,15 +127,6 @@ module Ser
111
127
  method(method_name)
112
128
  end
113
129
 
114
- def _attributes
115
- @attributes ||=
116
- if equal? Ser::Ializer
117
- ActiveSupport::HashWithIndifferentAccess.new
118
- else
119
- superclass._attributes.dup
120
- end
121
- end
122
-
123
130
  private
124
131
 
125
132
  def warning_message(name)
@@ -127,7 +134,7 @@ module Ser
127
134
  end
128
135
 
129
136
  def add_attribute(field)
130
- _attributes[field.key] = field
137
+ attributes[field.key] = field
131
138
 
132
139
  define_singleton_method field.name do |object, context|
133
140
  value = field.get_value(object, context)
@@ -137,7 +144,7 @@ module Ser
137
144
  end
138
145
 
139
146
  def add_composed_attribute(field, deser)
140
- _attributes[field.key] = field
147
+ attributes[field.key] = field
141
148
 
142
149
  define_singleton_method field.name do |object, context|
143
150
  deser.public_send(field.name, object, context)
@@ -169,9 +176,9 @@ module Ser
169
176
  def fields_for_serialization(context)
170
177
  field_names = fields_names_for_serialization(context)
171
178
 
172
- return _attributes.values unless field_names
179
+ return attributes.values unless field_names
173
180
 
174
- _attributes.values_at(*field_names).compact
181
+ attributes.values_at(*field_names).compact
175
182
  end
176
183
 
177
184
  def fields_names_for_serialization(context)
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.9.1
4
+ version: 0.12.0
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: 2021-07-02 00:00:00.000000000 Z
11
+ date: 2022-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -100,28 +100,28 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 0.78.0
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: 0.78.0
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: '0'
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: '0'
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.6
197
- signing_key:
196
+ rubygems_version: 3.2.22
197
+ signing_key:
198
198
  specification_version: 4
199
199
  summary: Simple (de)serializers for Ruby objects
200
200
  test_files: []