rasti-model 2.1.0 → 2.1.1

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: efc4252fe73eba82ba9eb35be17934864d7384a01428355706c401def3abb613
4
- data.tar.gz: 2906e648b0c76fb766024a168a3c7cd09d2661a15283c5f3e6341ebe0f167c10
3
+ metadata.gz: f31f21d81848581016911f75ce3b781e7603ef7bb39c3ea52ba860c7d12a8f01
4
+ data.tar.gz: e0ce2ae0c8303f5d2f87b4b0b3d4437f9ff8117eac6e9f6e44c71e3e5d16c190
5
5
  SHA512:
6
- metadata.gz: '08942eb37b0335bae2cc80f1cf17692cd9d736e6801d93b285be8fa34c1500d76e3129557425b858f47c449ba4e6a2e9588e3efd38793da3b0f9949a99a0a8a3'
7
- data.tar.gz: 1cd16e7e5ce96a2ab0d7bd2c9ba22917451ceb90d7009e6c02d3c488d823a2712d7eaadd50597e9e4a4d81256d1b0ecbad17f872c76909ddcf41b332797060fa
6
+ metadata.gz: d6ffb39110babca82a4a714f2a08ad3fcc75028ebe083fd0b1386441a98a53e0d0d98d0e0b519eef91bc4e1f0da7e87b78dfae14cff8c41d6eae6db630ef6ac1
7
+ data.tar.gz: e5b31f2fbc7282d7ed3fb7757a4a8fb3e8b33f27e50c2c4d9c523bfa14b586fc5d65b53a8cc9f6befb568562295c704206c81426e1f5d882b04cc79cba25e9b1
data/README.md CHANGED
@@ -3,7 +3,6 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/rasti-model.svg)](https://rubygems.org/gems/rasti-model)
4
4
  [![CI](https://github.com/gabynaiman/rasti-model/actions/workflows/ci.yml/badge.svg)](https://github.com/gabynaiman/rasti-model/actions/workflows/ci.yml)
5
5
  [![Coverage Status](https://coveralls.io/repos/github/gabynaiman/rasti-model/badge.svg?branch=master)](https://coveralls.io/github/gabynaiman/rasti-model?branch=master)
6
- [![Code Climate](https://codeclimate.com/github/gabynaiman/rasti-model.svg)](https://codeclimate.com/github/gabynaiman/rasti-model)
7
6
 
8
7
  Domain models with typed attributes
9
8
 
@@ -7,9 +7,9 @@ module Rasti
7
7
  type_serializers[type] = block || serialized_type
8
8
  end
9
9
 
10
- def serialize(model_class)
10
+ def serialize(model_class, visited=Set.new)
11
11
  attributes = model_class.attributes.map do |attribute|
12
- serialize_attribute(attribute).merge(name: attribute.name)
12
+ serialize_attribute(attribute, visited).merge(name: attribute.name)
13
13
  end
14
14
 
15
15
  {
@@ -24,8 +24,8 @@ module Rasti
24
24
  @type_serializers ||= {}
25
25
  end
26
26
 
27
- def serialize_attribute(attribute)
28
- serialization = serialize_type(attribute.type)
27
+ def serialize_attribute(attribute, visited)
28
+ serialization = serialize_type(attribute.type, visited)
29
29
 
30
30
  options = attribute.send(:options)
31
31
  serialization[:options] = options unless options.empty?
@@ -33,7 +33,7 @@ module Rasti
33
33
  serialization
34
34
  end
35
35
 
36
- def serialize_type(type)
36
+ def serialize_type(type, visited=Set.new)
37
37
  if type.nil?
38
38
  return {type: :any}
39
39
  end
@@ -67,11 +67,17 @@ module Rasti
67
67
  elsif type.respond_to?(:values)
68
68
  {type: :enum, values: type.values}
69
69
  elsif type.is_a?(Types::Array)
70
- {type: :array, items: serialize_type(type.type)}
70
+ {type: :array, items: serialize_type(type.type, visited)}
71
71
  elsif type.is_a?(Types::Hash)
72
- {type: :hash, key_type: serialize_type(type.key_type), value_type: serialize_type(type.value_type)}
72
+ {type: :hash, key_type: serialize_type(type.key_type, visited), value_type: serialize_type(type.value_type, visited)}
73
73
  elsif type.is_a?(Types::Model)
74
- {type: :model, model: type.model.name || type.model.to_s, schema: serialize(type.model)}
74
+ model = type.model
75
+ model_name = model.name || model.to_s
76
+ if visited.include?(model)
77
+ {type: :model, model: model_name, schema: {model: model_name, attributes: []}}
78
+ else
79
+ {type: :model, model: model_name, schema: serialize(model, visited | Set[model])}
80
+ end
75
81
  else
76
82
  {type: :unknown, details: type.to_s}
77
83
  end
@@ -1,5 +1,5 @@
1
1
  module Rasti
2
2
  class Model
3
- VERSION = '2.1.0'
3
+ VERSION = '2.1.1'
4
4
  end
5
5
  end
data/lib/rasti/model.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'multi_require'
2
2
  require 'rasti-types'
3
+ require 'set'
3
4
 
4
5
  module Rasti
5
6
  class Model
data/spec/model_spec.rb CHANGED
@@ -364,6 +364,65 @@ describe Rasti::Model do
364
364
  })
365
365
  end
366
366
 
367
+ it 'Self-referential model' do
368
+ node_class = Class.new(Rasti::Model) do
369
+ attribute :name, T::String
370
+ end
371
+ node_class.send(:attribute, :child, T::Model[node_class])
372
+
373
+ node_name = node_class.to_s
374
+
375
+ node_class.to_schema.must_equal({
376
+ model: node_name,
377
+ attributes: [
378
+ {type: :string, name: :name},
379
+ {type: :model, model: node_name, schema: {
380
+ model: node_name,
381
+ attributes: [
382
+ {type: :string, name: :name},
383
+ {type: :model, model: node_name, schema: {model: node_name, attributes: []}, name: :child}
384
+ ]
385
+ }, name: :child}
386
+ ]
387
+ })
388
+ end
389
+
390
+ it 'Mutually recursive models' do
391
+ person_class = Class.new(Rasti::Model) do
392
+ attribute :name, T::String
393
+ end
394
+
395
+ company_class = Class.new(Rasti::Model) do
396
+ attribute :name, T::String
397
+ end
398
+
399
+ person_class.send(:attribute, :employer, T::Model[company_class])
400
+ company_class.send(:attribute, :ceo, T::Model[person_class])
401
+
402
+ person_name = person_class.to_s
403
+ company_name = company_class.to_s
404
+
405
+ person_class.to_schema.must_equal({
406
+ model: person_name,
407
+ attributes: [
408
+ {type: :string, name: :name},
409
+ {type: :model, model: company_name, schema: {
410
+ model: company_name,
411
+ attributes: [
412
+ {type: :string, name: :name},
413
+ {type: :model, model: person_name, schema: {
414
+ model: person_name,
415
+ attributes: [
416
+ {type: :string, name: :name},
417
+ {type: :model, model: company_name, schema: {model: company_name, attributes: []}, name: :employer}
418
+ ]
419
+ }, name: :ceo}
420
+ ]
421
+ }, name: :employer}
422
+ ]
423
+ })
424
+ end
425
+
367
426
  end
368
427
 
369
428
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rasti-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-01-03 00:00:00.000000000 Z
11
+ date: 2026-06-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_require