avromatic 4.2.0 → 4.3.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: 5d9619909ed1f1301168536a2907146f30da67a08dd2bb4377d7cb0aa6cb4b6f
4
- data.tar.gz: 3ff48eef3f8880fbd2f36eeda17008723be7bdc269a5546f432e1f5e6c09635d
3
+ metadata.gz: '0018c670f7a1c729e244281b731633db6c50a580171c61173aab1f68c34071ba'
4
+ data.tar.gz: e61f2cedbbe52a4dd787779920c17b94b8366a98ee1708b06967f084a8df96eb
5
5
  SHA512:
6
- metadata.gz: 61cc46b27e9544faea7b411f5f10676d206c0504fabb69f2ebf0ad00b7d6adc0610973c4c27a6716ca1e7676c6436f4be8eb1c8298978fe7b4ece5c8910fe6ae
7
- data.tar.gz: 5ec19a93b881b723589b5f8afdaf1a0e41b15d601ca2ab840f8c627f74db3eeeab21b678c396840675caae75a3a92ccf97b872ea355cb94b2850e58d22179e12
6
+ metadata.gz: 18272c67e78be2702842b7c2f7153400cfeeca908f0404f452222bd8112760a5b4f5052f66e757fb1c64fea54536897801acb1316d05c262d1516ccafd30262e
7
+ data.tar.gz: dff850adf8388d2903873555c115c0672eaa3c91ee4a82452c5c99b50085d4f85adae2c4f38dbe45105c7194e61fb8b0e93ffaa6f4fb9ed65190210a6227b268
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+ require 'bigdecimal/util'
5
+ require 'avromatic/model/types/abstract_type'
6
+
7
+ module Avromatic
8
+ module Model
9
+ module Types
10
+ class DecimalType < AbstractType
11
+ VALUE_CLASSES = [::BigDecimal].freeze
12
+ INPUT_CLASSES = [::BigDecimal, ::Float, ::Integer].freeze
13
+
14
+ attr_reader :precision, :scale
15
+
16
+ def initialize(precision:, scale: 0)
17
+ super()
18
+ @precision = precision
19
+ @scale = scale
20
+ end
21
+
22
+ def value_classes
23
+ VALUE_CLASSES
24
+ end
25
+
26
+ def input_classes
27
+ INPUT_CLASSES
28
+ end
29
+
30
+ def name
31
+ "decimal(#{precision}, #{scale})"
32
+ end
33
+
34
+ def coerce(input)
35
+ case input
36
+ when ::NilClass, ::BigDecimal
37
+ input
38
+ when ::Float, ::Integer
39
+ input.to_d
40
+ else
41
+ raise ArgumentError.new("Could not coerce '#{input.inspect}' to #{name}")
42
+ end
43
+ end
44
+
45
+ def coercible?(input)
46
+ input.nil? || input_classes.any? { |input_class| input.is_a?(input_class) }
47
+ end
48
+
49
+ def coerced?(value)
50
+ value.nil? || value_classes.any? { |value_class| value.is_a?(value_class) }
51
+ end
52
+
53
+ def serialize(value, _strict)
54
+ value
55
+ end
56
+
57
+ def referenced_model_classes
58
+ EMPTY_ARRAY
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -4,6 +4,7 @@ require 'avromatic/model/types/array_type'
4
4
  require 'avromatic/model/types/boolean_type'
5
5
  require 'avromatic/model/types/custom_type'
6
6
  require 'avromatic/model/types/date_type'
7
+ require 'avromatic/model/types/decimal_type'
7
8
  require 'avromatic/model/types/enum_type'
8
9
  require 'avromatic/model/types/fixed_type'
9
10
  require 'avromatic/model/types/float_type'
@@ -50,6 +51,9 @@ module Avromatic
50
51
  )
51
52
  elsif schema.respond_to?(:logical_type) && SINGLETON_TYPES.include?(schema.logical_type)
52
53
  SINGLETON_TYPES.fetch(schema.logical_type)
54
+ elsif schema.respond_to?(:logical_type) && schema.logical_type == 'decimal' &&
55
+ Avromatic.allow_decimal_logical_type?
56
+ Avromatic::Model::Types::DecimalType.new(precision: schema.precision, scale: schema.scale || 0)
53
57
  elsif SINGLETON_TYPES.include?(schema.type)
54
58
  SINGLETON_TYPES.fetch(schema.type)
55
59
  else
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Avromatic
4
- VERSION = '4.2.0'
4
+ VERSION = '4.3.0'
5
5
  end
data/lib/avromatic.rb CHANGED
@@ -90,6 +90,10 @@ module Avromatic
90
90
  @eager_load_model_names&.each { |model_name| model_name.constantize.register! }
91
91
  end
92
92
  private_class_method :eager_load_models!
93
+
94
+ def self.allow_decimal_logical_type?
95
+ ::Gem::Requirement.new('>= 1.11.0').satisfied_by?(::Gem::Version.new(::Avro::VERSION))
96
+ end
93
97
  end
94
98
 
95
99
  require 'avromatic/railtie' if defined?(Rails)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avromatic
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 4.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Salsify Engineering
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-28 00:00:00.000000000 Z
11
+ date: 2023-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -301,6 +301,7 @@ files:
301
301
  - lib/avromatic/model/types/boolean_type.rb
302
302
  - lib/avromatic/model/types/custom_type.rb
303
303
  - lib/avromatic/model/types/date_type.rb
304
+ - lib/avromatic/model/types/decimal_type.rb
304
305
  - lib/avromatic/model/types/enum_type.rb
305
306
  - lib/avromatic/model/types/fixed_type.rb
306
307
  - lib/avromatic/model/types/float_type.rb
@@ -342,7 +343,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
342
343
  - !ruby/object:Gem::Version
343
344
  version: '0'
344
345
  requirements: []
345
- rubygems_version: 3.3.7
346
+ rubygems_version: 3.3.26
346
347
  signing_key:
347
348
  specification_version: 4
348
349
  summary: Generate Ruby models from Avro schemas