json_model_rb 0.1.3 → 0.1.4

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: cc1b5efa89714c5e34d9ffc8c8f7f8a4da4cec9abd280d0ca50d9229e090f723
4
- data.tar.gz: 0f13c5aaa27994bf1c05131cf81f8e43b18678ff80556cabe2c2ce43c3a63a06
3
+ metadata.gz: d8629addf96efee3c996feeeff0f3b1806ceb78bbc3ecffd6470ea1f96db1da5
4
+ data.tar.gz: 9821968d8a12620d7b4f53c5884cbe6214998e3af9b6785719f8390a3148725f
5
5
  SHA512:
6
- metadata.gz: 97ce1787bb3d26ee9359211f73df8de3b99a58860cb45632b1c56990abdab2965310f3fdd6bce0b562ff6f5a48f5b988e5deb906285f90ec778b1f6342699bd0
7
- data.tar.gz: d2a93f7a96fac2c66add234b5b2ee3bdf65404b82bdf591e348afcc4b27bafdebcf489109da25246be9db4e5db0b307aa06733ef871c990d358ca4735518b5ce
6
+ metadata.gz: ae72851a9d485cc0cbd7df518d48a40ff03a115025a4e3a04177fb6207783c2df3377c7778b1131cee053fc13711e60b9dafe220b831303f06d19eec56ef3eb6
7
+ data.tar.gz: e51539c6a3beb3319949bb0e052e5817304772c012d742f96b744289c2cb33afd8d9864b4593e3a0ea9013ee9039a5183e4ce1351fa670534dae1e1b45b10ab4
@@ -7,14 +7,21 @@ module JsonModel
7
7
  included do
8
8
  extend(ActiveSupport::DescendantsTracker)
9
9
  include(ActiveModel::Validations)
10
+ end
10
11
 
11
- class_attribute(:attributes, instance_writer: false, default: {})
12
- class_attribute(:properties, instance_writer: false, default: {})
12
+ # @return [Hash]
13
+ def attributes
14
+ @attributes ||= {}
13
15
  end
14
16
 
15
17
  class_methods do
18
+ # @return [Hash]
19
+ def properties
20
+ @properties ||= {}
21
+ end
22
+
16
23
  # @param [Symbol] name
17
- # @param [Object] type
24
+ # @param [Object, Class] type
18
25
  # @param [Hash] options
19
26
  def property(name, type:, **options)
20
27
  property_options = options.slice(:default, :optional, :ref_mode, :as)
@@ -13,20 +13,24 @@ module JsonModel
13
13
  }.freeze
14
14
 
15
15
  included do
16
- class_attribute(:meta_attributes, instance_writer: false, default: {})
17
- class_attribute(:ref_schema, instance_writer: false)
18
-
19
16
  # @param [Class] subclass
20
17
  def self.inherited(subclass)
21
18
  super
19
+ subclass.schema_id(JsonModel.config.schema_id_naming_strategy.call(self))
20
+ subclass.additional_properties(false)
22
21
  subclass.meta_attributes[:$ref] = schema_id
23
22
  end
24
23
 
25
- schema_id(name)
24
+ schema_id(JsonModel.config.schema_id_naming_strategy.call(self))
26
25
  additional_properties(false)
27
26
  end
28
27
 
29
28
  class_methods do
29
+ # @return [Hash]
30
+ def meta_attributes
31
+ @meta_attributes ||= {}
32
+ end
33
+
30
34
  # @param [String, nil] description
31
35
  # @return [String, nil]
32
36
  def description(description = nil)
@@ -30,7 +30,7 @@ module JsonModel
30
30
  end
31
31
 
32
32
  class << self
33
- # @param [Object] type
33
+ # @param [Object, Class] type
34
34
  # @param [Hash] options
35
35
  # @return [TypeSpec]
36
36
  def resolve(type, **options)
@@ -48,7 +48,7 @@ module JsonModel
48
48
 
49
49
  private
50
50
 
51
- # @param [Object] type
51
+ # @param [Object, Class] type
52
52
  # @param [Hash] options
53
53
  # @return [TypeSpec]
54
54
  def resolve_type_from_class(type, **options)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonModel
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
data/spec/schema_spec.rb CHANGED
@@ -200,6 +200,14 @@ RSpec.describe(JsonModel::Schema) do
200
200
  let(:child) do
201
201
  Class.new(klass) do
202
202
  schema_id('https://example.com/schemas/child.json')
203
+ property(:baz, type: String)
204
+ end
205
+ end
206
+ let(:second_child) do
207
+ Class.new(klass) do
208
+ schema_id('https://example.com/schemas/second-child.json')
209
+ title('SecondChild')
210
+ property(:bar, type: String)
203
211
  end
204
212
  end
205
213
 
@@ -215,9 +223,34 @@ RSpec.describe(JsonModel::Schema) do
215
223
  '$ref': 'https://example.com/schemas/example.json',
216
224
  type: 'object',
217
225
  additionalProperties: false,
226
+ properties: { baz: { type: 'string' } },
227
+ required: %i(baz),
218
228
  },
219
229
  ),
220
230
  )
231
+
232
+ expect(second_child.as_schema)
233
+ .to(
234
+ eq(
235
+ {
236
+ '$id': 'https://example.com/schemas/second-child.json',
237
+ '$ref': 'https://example.com/schemas/example.json',
238
+ title: 'SecondChild',
239
+ type: 'object',
240
+ additionalProperties: false,
241
+ properties: { bar: { type: 'string' } },
242
+ required: %i(bar),
243
+ },
244
+ ),
245
+ )
246
+
247
+ instance = child.new(foo: 'foo', baz: 'baz')
248
+ expect(instance.foo).to(eq('foo'))
249
+ expect(instance.baz).to(eq('baz'))
250
+
251
+ second_instance = second_child.new(foo: 'foo', bar: 'bar')
252
+ expect(second_instance.foo).to(eq('foo'))
253
+ expect(second_instance.bar).to(eq('bar'))
221
254
  end
222
255
  end
223
256
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_model_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Gillesberger
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-12-28 00:00:00.000000000 Z
10
+ date: 2025-12-29 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rake