ializer 0.1.0 → 0.6.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: b39a084832bc15cc9d722c7269b3200b9e4a6af8c4318ea06390f91acf6a27d5
4
- data.tar.gz: '0355388f6e02ac9f8c156706230e172d36b3698d0f22df9f5a90ebf0f121a0b4'
3
+ metadata.gz: 65d2290293ec3f08da6cf2b4b4ce3f30c7240ace2157380f6394c578902f6057
4
+ data.tar.gz: b7c0e5883f3133c4ea215648af2133cdb8d00153dbd1b53c2a08f3518c62bac7
5
5
  SHA512:
6
- metadata.gz: 0b4123ae5e65828108879cf191062ef3f05e980bd1fab81b1b51c262d576ea69664cc2d2a2e75d902049fdf52b33d9c25d26021a90d1c0fda7ae228b65315a25
7
- data.tar.gz: ceab7b40078424c4bb23634e059a99ce7fd157bee574e1e7fa6e9202a64d9482a0458acf29d978349ca38f38e71ac58a316acf9e9a3e10a19b48a4ef70998b6a
6
+ metadata.gz: 14e4e920d24d0a3857dea918cbb318030887c4d5c2ee2c8c27bff61d547fb8c86cc54b573ff142d13c32daf78fbf9efc289d4204d16615cbcee1fbf1b583b5b5
7
+ data.tar.gz: bfa543caebda04f6bb6ccec07c93bda3b8e57e2f94a63b3221ffa04294467fb88ff9cdbb72ade621450554ff44f103efb932734f81aaa66cc7018cc8b16799d1
@@ -22,7 +22,7 @@ jobs:
22
22
  mkdir -p $HOME/.gem
23
23
  touch $HOME/.gem/credentials
24
24
  chmod 0600 $HOME/.gem/credentials
25
- printf -- "---\n:github: Bearer ${GITHUB_TOKEN}\n:rubygems: Bearer ${RUBYGEMS_API_KEY}\n" > $HOME/.gem/credentials
25
+ printf -- "---\n:github: Bearer ${GITHUB_TOKEN}\n:rubygems_api_key: Bearer ${RUBYGEMS_API_KEY}\n" > $HOME/.gem/credentials
26
26
  env:
27
27
  GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
28
28
  RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}}
@@ -36,4 +36,4 @@ jobs:
36
36
  - name: Publish to RubyGems
37
37
  run: |
38
38
  gem build *.gemspec
39
- gem push --KEY rubygems *.gem
39
+ gem push --KEY rubygems_api_key *.gem
@@ -4,6 +4,9 @@ AllCops:
4
4
  TargetRubyVersion:
5
5
  2.6
6
6
 
7
+ Gemspec/RequiredRubyVersion:
8
+ Enabled: false
9
+
7
10
  Layout/LineLength:
8
11
  Max: 120
9
12
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Build](https://github.com/jsteinberg/ializer/workflows/Ruby/badge.svg)](https://github.com/jsteinberg/ializer/actions)
1
+ [![Build](https://github.com/jsteinberg/ializer/workflows/Ruby/badge.svg)](https://github.com/jsteinberg/ializer/actions) [![Gem Version](https://badge.fury.io/rb/ializer.svg)](https://badge.fury.io/rb/ializer)
2
2
 
3
3
  # {De | Ser} Ializer
4
4
 
@@ -306,9 +306,10 @@ The following types are included with `ializer`
306
306
  | String | `string()` | String, :millis |
307
307
  | Symbol | `symbol()` | Symbol, :symbol, :sym |
308
308
  | Time | `timestamp()`| Time, DateTime, :timestamp |
309
+ | JSON | `json()` | :json |
309
310
  | Default | `default()` | :default |
310
311
 
311
- **Note: Default just uses the current value of the field and will only properly deserialize if it is a standard json value type(number, string, boolean).**
312
+ **Note: JSON/Default just uses the current value of the field and will only properly deserialize if it is a standard json value type(number, string, boolean).**
312
313
 
313
314
  #### Default Attribute Configuration Options
314
315
 
@@ -503,21 +504,30 @@ class UserDeSer < De::Ser::Ializer
503
504
  end
504
505
  ```
505
506
 
506
- **Note:** Because of scoping, including a deser using `with` will not include any method overrides
507
+ **Note:** Including a deser using `with` will include any method overrides.
507
508
 
508
509
  ```ruby
509
510
  class BaseApiDeSer < De::Ser::Ializer
510
511
  integer :id
511
512
  timestamp :created_at
513
+ timestamp :updated_at
512
514
 
513
515
  def self.created_at(object, context)
514
- # NOT INCLUDED IN UserDeSer. UserDeSer will call `.created_at` on any serialization target
516
+ # INCLUDED IN UserDeSer
517
+ end
518
+
519
+ def self.updated_at(object, context)
520
+ # NOT INCLUDED IN UserDeSer(Overridden below)
515
521
  end
516
522
  end
517
523
 
518
524
  class UserDeSer < De::Ser::Ializer
519
525
  with BaseApiDeSer
520
526
  string :email
527
+
528
+ def self.updated_at(object, context)
529
+ # INCLUDED IN UserDeSer.
530
+ end
521
531
  end
522
532
  ```
523
533
 
@@ -19,8 +19,9 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
21
  spec.require_paths = ['lib']
22
+ spec.required_ruby_version = '>= 2.4.0'
22
23
 
23
- spec.add_dependency 'activesupport'
24
+ spec.add_dependency 'activesupport', '>= 4.0'
24
25
  spec.add_dependency 'multi_json', '~> 1.0'
25
26
 
26
27
  spec.add_development_dependency 'bundler'
@@ -53,6 +53,8 @@ module De
53
53
  private
54
54
 
55
55
  def parse_field(object, field, value)
56
+ return if value.nil?
57
+
56
58
  parsed_value = field.parse(value)
57
59
 
58
60
  return if parsed_value.nil?
@@ -5,10 +5,14 @@ require 'bigdecimal'
5
5
  module Ializer
6
6
  class BigDecimalDeSer
7
7
  def self.serialize(value, _context = nil)
8
+ value = value.to_d unless value.is_a? BigDecimal
9
+
8
10
  value.to_s('F')
9
11
  end
10
12
 
11
13
  def self.parse(value)
14
+ return nil if value.nil?
15
+
12
16
  BigDecimal(value)
13
17
  end
14
18
  end
@@ -10,7 +10,7 @@ module Ializer
10
10
 
11
11
  def self.parse(value)
12
12
  Date.parse(value)
13
- rescue ArgumentError
13
+ rescue ArgumentError, TypeError
14
14
  nil
15
15
  end
16
16
  end
@@ -13,3 +13,4 @@ module Ializer
13
13
  end
14
14
 
15
15
  Ser::Ializer.register_default(Ializer::DefaultDeSer)
16
+ Ser::Ializer.register('json', Ializer::DefaultDeSer, :json)
@@ -9,6 +9,8 @@ module Ializer
9
9
  end
10
10
 
11
11
  def self.parse(value)
12
+ return nil if value.nil?
13
+
12
14
  Time.at(value / 1000.0)
13
15
  end
14
16
  end
@@ -7,6 +7,8 @@ module Ializer
7
7
  end
8
8
 
9
9
  def self.parse(value)
10
+ return nil if value.nil?
11
+
10
12
  value.to_sym
11
13
  end
12
14
  end
@@ -10,6 +10,8 @@ module Ializer
10
10
 
11
11
  def self.parse(value)
12
12
  DateTime.iso8601 value
13
+ rescue ArgumentError
14
+ nil
13
15
  end
14
16
  end
15
17
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ializer
4
- VERSION = '0.1.0'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -29,13 +29,17 @@ module Ser
29
29
  end
30
30
 
31
31
  def with(deser)
32
- deser._attributes.values.map(&:dup).each(&method(:add_attribute))
32
+ deser._attributes.values.each do |field|
33
+ add_composed_attribute(field, deser)
34
+ end
33
35
  end
34
36
  # End Public DSL
35
37
 
36
38
  def serialize(object, context = nil)
37
39
  return serialize_one(object, context) unless valid_enumerable?(object)
38
40
 
41
+ return [] if object.empty?
42
+
39
43
  object.map { |o| serialize_one(o, context) }
40
44
  end
41
45
 
@@ -107,26 +111,18 @@ module Ser
107
111
  def add_attribute(field)
108
112
  _attributes[field.key] = field
109
113
 
110
- if field.block
111
- add_attribute_with_block(field)
112
- else
113
- add_attribute_with_method(field)
114
- end
115
- end
116
-
117
- def add_attribute_with_block(field)
118
114
  define_singleton_method field.name do |object, context|
119
- value = field.block.call(object, context)
115
+ value = field.get_value(object, context)
120
116
 
121
117
  serialize_field(field, value, context)
122
118
  end
123
119
  end
124
120
 
125
- def add_attribute_with_method(field)
126
- define_singleton_method field.name do |object, context|
127
- value = object.public_send(field.name)
121
+ def add_composed_attribute(field, deser)
122
+ _attributes[field.key] = field
128
123
 
129
- serialize_field(field, value, context)
124
+ define_singleton_method field.name do |object, context|
125
+ deser.public_send(field.name, object, context)
130
126
  end
131
127
  end
132
128
 
@@ -135,6 +131,8 @@ module Ser
135
131
 
136
132
  return field.serialize(value, context) unless valid_enumerable?(value)
137
133
 
134
+ return nil if value.empty?
135
+
138
136
  value.map { |v| field.serialize(v, context) }
139
137
  end
140
138
 
@@ -23,6 +23,12 @@ module Ser
23
23
  @block = block
24
24
  end
25
25
 
26
+ def get_value(object, context)
27
+ return object.public_send(name) unless block
28
+
29
+ block.call(object, context)
30
+ end
31
+
26
32
  def serialize(value, context)
27
33
  deser.serialize(value, context)
28
34
  end
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.1.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Steinberg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-07 00:00:00.000000000 Z
11
+ date: 2020-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '4.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '4.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: multi_json
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -186,7 +186,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
186
  requirements:
187
187
  - - ">="
188
188
  - !ruby/object:Gem::Version
189
- version: '0'
189
+ version: 2.4.0
190
190
  required_rubygems_version: !ruby/object:Gem::Requirement
191
191
  requirements:
192
192
  - - ">="