json_model_rb 0.1.2 → 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: 9599ecfecdb61edf6173927d8e33bf9a96686fe765aa94274611788eff07312a
4
- data.tar.gz: 160000519dda11b0ce29648dd17d12436e86514acbc013de01da96f23949effe
3
+ metadata.gz: d8629addf96efee3c996feeeff0f3b1806ceb78bbc3ecffd6470ea1f96db1da5
4
+ data.tar.gz: 9821968d8a12620d7b4f53c5884cbe6214998e3af9b6785719f8390a3148725f
5
5
  SHA512:
6
- metadata.gz: 12a4b646df7225003ec60e07dccbbb6c5115e405736af120712d0659fc14fea268373701e30f7f3e2b07f3f095b3f0a797946e97794d07b60b15ea368d48b7cf
7
- data.tar.gz: dee7c155478f1e4532348e481587d00045b325744e0014638acfe4c28074f0569f19e1210a01f54de848f4eea3ce4b42d3d2830a0f81d2f1f9abae7aa1faeca6
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)
@@ -37,6 +41,16 @@ module JsonModel
37
41
  end
38
42
  end
39
43
 
44
+ # @param [String, nil] title
45
+ # @return [String, nil]
46
+ def title(title = nil)
47
+ if title
48
+ meta_attributes[:title] = title
49
+ else
50
+ meta_attributes[:title]
51
+ end
52
+ end
53
+
40
54
  # @param [String, nil] id
41
55
  # @return [String, nil]
42
56
  def schema_id(id = 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.2'
4
+ VERSION = '0.1.4'
5
5
  end
@@ -76,6 +76,26 @@ RSpec.describe(JsonModel::SchemaMeta) do
76
76
  end
77
77
  end
78
78
 
79
+ describe('.title') do
80
+ let(:klass) do
81
+ Class.new do
82
+ include(JsonModel::SchemaMeta)
83
+ end
84
+ end
85
+
86
+ it('is nil by default') do
87
+ expect(klass.title)
88
+ .to(be_nil)
89
+ end
90
+
91
+ it('can be changed') do
92
+ klass.title('This is a test title.')
93
+
94
+ expect(klass.title)
95
+ .to(eq('This is a test title.'))
96
+ end
97
+ end
98
+
79
99
  describe('.additional_properties') do
80
100
  let(:klass) do
81
101
  Class.new do
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.2
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