json_schematize 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 41661866a0f6debaa7bacc0897f12f00fe0ee156deb2c18dbacd28ef205a1079
4
- data.tar.gz: d053b83bc1b4a3fb39b80ad18e126fa535102beeb988e036ab0d76564934af22
3
+ metadata.gz: 4a137c2d8b5ad986a86ac234f6c03802af2c175b06fc85ff69cf57794a378cca
4
+ data.tar.gz: b449ba15bc26b4f3026d8904bb25a76bcbd26e8ac50ab40deee36f6c1e165544
5
5
  SHA512:
6
- metadata.gz: 7c8d4f0c708c44330f76d64b280b0dfdc2f8a3cf8aa8f99de36ad5ad4bdb0a9e41ba1329b128730942016195e456cd1842bae61d3782108462305a174244f7de
7
- data.tar.gz: e64e2b257ea5c8cf8db6e1e01d88297a7f3a15724cb8efacb4bac4a2953aad4e2208e70f1f579e428181d5265170fbfa07f2a8bb736a08fe8193b66556627003
6
+ metadata.gz: 18739039d4c14428e0ec8ad33ed4b229669cceb8cecb11fa851e13a7ca04eb73a78c2faf0bfd34b49957cea90494d1fecd0d31c94c76ea67afc798007ca08288
7
+ data.tar.gz: 8b51b98b196a7f1a106c409fb957aa259a5751e07d7ae18b823a1439f877987f359a91eb53bd6ff2d1a597fd9e91dafa307f3eb7beef890427eef44267701b5f
data/.circleci/config.yml CHANGED
@@ -12,10 +12,10 @@ workflows:
12
12
  - cst/enforce-gem-version-bump
13
13
  - cst/rspec-ruby:
14
14
  rspec-system-args: "SIMPLE_COV_RUN=true"
15
- cc-report-collect-ruby: "2.7.5"
15
+ cc-report-collect-ruby: "3.3.6"
16
16
  matrix:
17
17
  parameters:
18
- ruby-version: ["2.7.5" , "3.0.0", "3.0.3"]
18
+ ruby-version: ["3.2.4", "3.3.6"]
19
19
  alias: required-matrix-tests
20
20
  name: test-ruby<< matrix.ruby-version >>
21
21
  - cst/publish-gem:
data/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM ruby:3.0.1
1
+ FROM ruby:3.3.6
2
2
  RUN cd /tmp && curl -L --output ghr.tar.gz https://github.com/tcnksm/ghr/releases/download/v0.12.0/ghr_v0.12.0_linux_amd64.tar.gz && \
3
3
  tar -xzvf ghr.tar.gz && chmod +x ghr_v0.12.0_linux_amd64/ghr && mv ghr_v0.12.0_linux_amd64/ghr /usr/local/bin/ghr && rm -rf /tmp/*
4
4
 
data/Gemfile.lock CHANGED
@@ -1,29 +1,43 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schematize (0.9.0)
5
- class_composer (~> 1.0)
4
+ json_schematize (0.11.0)
5
+ class_composer (>= 1.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (7.0.4.3)
11
- concurrent-ruby (~> 1.0, >= 1.0.2)
10
+ activesupport (8.0.1)
11
+ base64
12
+ benchmark (>= 0.3)
13
+ bigdecimal
14
+ concurrent-ruby (~> 1.0, >= 1.3.1)
15
+ connection_pool (>= 2.2.5)
16
+ drb
12
17
  i18n (>= 1.6, < 2)
18
+ logger (>= 1.4.2)
13
19
  minitest (>= 5.1)
14
- tzinfo (~> 2.0)
20
+ securerandom (>= 0.3)
21
+ tzinfo (~> 2.0, >= 2.0.5)
22
+ uri (>= 0.13.1)
23
+ base64 (0.2.0)
24
+ benchmark (0.4.0)
25
+ bigdecimal (3.1.9)
15
26
  byebug (11.1.3)
16
- class_composer (1.0.2)
27
+ class_composer (2.1.0)
17
28
  coderay (1.1.3)
18
- concurrent-ruby (1.2.2)
19
- diff-lcs (1.5.0)
20
- docile (1.4.0)
21
- faker (3.1.1)
29
+ concurrent-ruby (1.3.4)
30
+ connection_pool (2.4.1)
31
+ diff-lcs (1.5.1)
32
+ docile (1.4.1)
33
+ drb (2.2.1)
34
+ faker (3.5.1)
22
35
  i18n (>= 1.8.11, < 2)
23
- i18n (1.12.0)
36
+ i18n (1.14.6)
24
37
  concurrent-ruby (~> 1.0)
25
- method_source (1.0.0)
26
- minitest (5.18.0)
38
+ logger (1.6.4)
39
+ method_source (1.1.0)
40
+ minitest (5.25.4)
27
41
  pry (0.14.2)
28
42
  coderay (~> 1.1)
29
43
  method_source (~> 1.0)
@@ -31,31 +45,34 @@ GEM
31
45
  byebug (~> 11.0)
32
46
  pry (>= 0.13, < 0.15)
33
47
  rake (12.3.3)
34
- rspec (3.12.0)
35
- rspec-core (~> 3.12.0)
36
- rspec-expectations (~> 3.12.0)
37
- rspec-mocks (~> 3.12.0)
38
- rspec-core (3.12.1)
39
- rspec-support (~> 3.12.0)
40
- rspec-expectations (3.12.2)
48
+ rspec (3.13.0)
49
+ rspec-core (~> 3.13.0)
50
+ rspec-expectations (~> 3.13.0)
51
+ rspec-mocks (~> 3.13.0)
52
+ rspec-core (3.13.2)
53
+ rspec-support (~> 3.13.0)
54
+ rspec-expectations (3.13.3)
41
55
  diff-lcs (>= 1.2.0, < 2.0)
42
- rspec-support (~> 3.12.0)
43
- rspec-mocks (3.12.5)
56
+ rspec-support (~> 3.13.0)
57
+ rspec-mocks (3.13.2)
44
58
  diff-lcs (>= 1.2.0, < 2.0)
45
- rspec-support (~> 3.12.0)
46
- rspec-support (3.12.0)
59
+ rspec-support (~> 3.13.0)
60
+ rspec-support (3.13.2)
47
61
  rspec_junit_formatter (0.6.0)
48
62
  rspec-core (>= 2, < 4, != 2.12.0)
63
+ securerandom (0.4.1)
49
64
  simplecov (0.22.0)
50
65
  docile (~> 1.1)
51
66
  simplecov-html (~> 0.11)
52
67
  simplecov_json_formatter (~> 0.1)
53
- simplecov-html (0.12.3)
68
+ simplecov-html (0.13.1)
54
69
  simplecov_json_formatter (0.1.4)
55
70
  tzinfo (2.0.6)
56
71
  concurrent-ruby (~> 1.0)
72
+ uri (1.0.2)
57
73
 
58
74
  PLATFORMS
75
+ aarch64-linux
59
76
  x86_64-darwin-20
60
77
  x86_64-linux
61
78
 
@@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "https://github.com/matt-taylor/json_schematize"
14
14
  spec.license = "MIT"
15
15
 
16
- spec.required_ruby_version = Gem::Requirement.new(">= 2.7")
16
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.2")
17
17
 
18
18
  spec.metadata = {
19
19
  "homepage_uri" => spec.homepage,
@@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
30
  spec.require_paths = ["lib"]
31
31
 
32
- spec.add_dependency "class_composer", "~> 1.0"
32
+ spec.add_dependency "class_composer", ">= 1.0"
33
33
 
34
34
  spec.add_development_dependency "pry-byebug"
35
35
  spec.add_development_dependency "rake", "~> 12.0"
@@ -2,12 +2,11 @@
2
2
 
3
3
  module JsonSchematize::Cache::ClassMethods
4
4
  module Client
5
-
6
5
  def __deserialize_keys__(key_hash_bytes)
7
6
  return [{}, 0] if key_hash_bytes.nil?
8
7
 
9
8
  min_expire = Time.now.to_i
10
- key_hash = Marshal.load(key_hash_bytes)
9
+ key_hash = __marshalize__(key_hash_bytes)
11
10
  keys_removed = 0
12
11
  key_hash.each do |key, expire|
13
12
  next if expire.to_i > min_expire
@@ -22,6 +21,10 @@ module JsonSchematize::Cache::ClassMethods
22
21
  [{}, 0]
23
22
  end
24
23
 
24
+ def __marshalize__(key_hash_bytes)
25
+ Marshal.load(key_hash_bytes)
26
+ end
27
+
25
28
  def __update_record_keeper__!(expire:, cache_key:, delete_key: false)
26
29
  record_of_truth = cached_keys(with_expire: true)
27
30
  if delete_key
@@ -5,7 +5,7 @@ require 'json_schematize/field_validators'
5
5
 
6
6
  class JsonSchematize::Field
7
7
 
8
- attr_reader :name, :types, :dig, :dig_type, :symbol, :validator, :empty_value
8
+ attr_reader :name, :types, :type, :dig, :dig_type, :symbol, :validator, :empty_value
9
9
  attr_reader :acceptable_types, :required, :converter, :array_of_types
10
10
 
11
11
  EXPECTED_DIG_TYPE = [DIG_SYMBOL = :symbol, DEFAULT_DIG = DIG_NONE =:none, DIG_STRING = :string]
@@ -89,7 +89,11 @@ class JsonSchematize::Field
89
89
  private
90
90
 
91
91
  def validate_acceptable_types(val:)
92
- (all_allowed_types + @acceptable_types).include?(val.class)
92
+ types = (all_allowed_types + @acceptable_types)
93
+ return true if types.include?(val.class)
94
+
95
+ # Allow inheritance here as well -- this only works when a custom converter is defined
96
+ Class === val ? types.any? { _1 > val } : false
93
97
  end
94
98
 
95
99
  def all_allowed_types
@@ -108,8 +112,14 @@ class JsonSchematize::Field
108
112
  raise JsonSchematize::InvalidFieldByArrayOfTypes, ":#{name} expected to be an array based on :array_of_types flag. Given #{value.class}"
109
113
  end
110
114
 
111
- value.map do |val|
112
- raw_converter_call(value: val)
115
+ if value.all? { JsonSchematize::Generator === _1 }
116
+ # We have already done the work to convert it into a Schematizable object
117
+ # Return the array and allow the remaining validations to take place
118
+ value
119
+ else
120
+ value.map do |val|
121
+ raw_converter_call(value: val)
122
+ end
113
123
  end
114
124
  end
115
125
 
@@ -2,13 +2,15 @@
2
2
 
3
3
  require "json_schematize/cache"
4
4
  require "json_schematize/field"
5
- require "json_schematize/introspect"
5
+ require "json_schematize/introspect_instance_methods"
6
+ require "json_schematize/introspect_class_methods"
6
7
 
7
8
  class JsonSchematize::Generator
8
9
  EMPTY_VALIDATOR = ->(_transformed_value, _raw_value) { true }
9
10
  PROTECTED_METHODS = [:assign_values!, :convenience_methods, :validate_required!, :validate_optional!, :validate_value]
10
11
 
11
- include JsonSchematize::Introspect
12
+ include JsonSchematize::Introspect::InstanceMethods
13
+ extend JsonSchematize::Introspect::ClassMethods
12
14
 
13
15
  def self.add_field(name:, type: nil, types: nil, dig_type: nil, dig: nil, validator: nil, required: nil, converter: nil, array_of_types: nil, empty_value: nil, hash_of_types: nil, hash_of_types_key: "name")
14
16
  require "json_schematize/empty_value"
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonSchematize::Introspect
4
+ module ClassMethods
5
+ def introspect(deep: false, prepend_dig: [], introspection: {})
6
+ fields.each do |field|
7
+ naming = (prepend_dig + field.dig).compact
8
+ type_metadata = __field_type__(field)
9
+ introspection[naming.join(".")] = {
10
+ required: field.required,
11
+ allowed: type_metadata[:humanize],
12
+ }
13
+
14
+ if deep && type_metadata[:deep_introspection]
15
+ prepended_naming = if field.array_of_types
16
+ naming.dup.tap { _1[-1] = "#{_1[-1]}[x]"}
17
+ else
18
+ naming.dup
19
+ end
20
+ type_metadata[:types][0].introspect(deep:, prepend_dig: prepended_naming, introspection: introspection)
21
+ end
22
+ end
23
+
24
+ introspection
25
+ end
26
+
27
+ def __field_type__(field)
28
+ types = Array(field.type || field.types)
29
+ deep_introspection = false
30
+ type_string = if types.length == 0
31
+ "Anything"
32
+ elsif types.length == 1
33
+ type = types.first
34
+ if JsonSchematize::Generator > type
35
+ deep_introspection = true
36
+ if field.array_of_types
37
+ "Array of #{type}"
38
+ else
39
+ type
40
+ end
41
+ else
42
+ type
43
+ end
44
+ else
45
+ "One of [#{types}]"
46
+ end
47
+
48
+ {
49
+ humanize: type_string,
50
+ types:,
51
+ deep_introspection:,
52
+ }
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JsonSchematize::Introspect
4
+ module InstanceMethods
5
+ def to_h
6
+ self.class.fields.map do |field|
7
+ value = method(:"#{field.name}").()
8
+ if field.array_of_types
9
+ [field.name, value.map(&:to_h)]
10
+ elsif value.class == Class
11
+ [field.name, value.to_s]
12
+ elsif JsonSchematize::Generator > value.class
13
+ [field.name, value.to_h]
14
+ else
15
+ [field.name, value]
16
+ end
17
+ end.to_h
18
+ end
19
+ alias :to_hash :to_h
20
+
21
+ def deep_inspect(with_raw_params: false, with_field: false)
22
+ self.class.fields.map do |field|
23
+ value = {
24
+ required: field.required,
25
+ acceptable_types: field.acceptable_types,
26
+ value: instance_variable_get(:"@#{field.name}"),
27
+ }
28
+ value[:field] = field if with_field
29
+ value[:raw_params] = @__raw_params if with_raw_params
30
+ [field.name, value]
31
+ end.to_h
32
+ end
33
+
34
+ def inspect
35
+ stringify = to_h.map { |k, v| "#{k}:#{v}" }.join(", ")
36
+ "#<#{self.class} - required fields: #{self.class.required_fields.map(&:name)}; #{stringify}>"
37
+ end
38
+ end
39
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonSchematize
4
- VERSION = "0.9.0"
4
+ VERSION = "0.11.0"
5
5
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schematize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-03 00:00:00.000000000 Z
11
+ date: 2024-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: class_composer
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.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
26
  version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
@@ -90,7 +90,6 @@ files:
90
90
  - ".circleci/config.yml"
91
91
  - ".gitignore"
92
92
  - ".rspec"
93
- - ".ruby-version"
94
93
  - CHANGELOG.md
95
94
  - CODEOWNERS
96
95
  - Dockerfile
@@ -116,7 +115,8 @@ files:
116
115
  - lib/json_schematize/field_transformations.rb
117
116
  - lib/json_schematize/field_validators.rb
118
117
  - lib/json_schematize/generator.rb
119
- - lib/json_schematize/introspect.rb
118
+ - lib/json_schematize/introspect_class_methods.rb
119
+ - lib/json_schematize/introspect_instance_methods.rb
120
120
  - lib/json_schematize/version.rb
121
121
  homepage: https://github.com/matt-taylor/json_schematize
122
122
  licenses:
@@ -132,14 +132,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
132
  requirements:
133
133
  - - ">="
134
134
  - !ruby/object:Gem::Version
135
- version: '2.7'
135
+ version: '3.2'
136
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  requirements:
138
138
  - - ">="
139
139
  - !ruby/object:Gem::Version
140
140
  version: '0'
141
141
  requirements: []
142
- rubygems_version: 3.4.10
142
+ rubygems_version: 3.5.9
143
143
  signing_key:
144
144
  specification_version: 4
145
145
  summary: This gem gives you the ability to turn API results into a standardized schema's
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.7.4
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module JsonSchematize::Introspect
4
- def to_h
5
- self.class.fields.map do |field|
6
- [field.name, instance_variable_get(:"@#{field.name}")]
7
- end.to_h
8
- end
9
- alias :to_hash :to_h
10
-
11
- def deep_inspect(with_raw_params: false, with_field: false)
12
- self.class.fields.map do |field|
13
- value = {
14
- required: field.required,
15
- acceptable_types: field.acceptable_types,
16
- value: instance_variable_get(:"@#{field.name}"),
17
- }
18
- value[:field] = field if with_field
19
- value[:raw_params] = @__raw_params if with_raw_params
20
- [field.name, value]
21
- end.to_h
22
- end
23
-
24
- def inspect
25
- stringify = to_h.map { |k, v| "#{k}:#{v}" }.join(", ")
26
- "#<#{self.class} - required fields: #{self.class.required_fields.map(&:name)}; #{stringify}>"
27
- end
28
- end