blueprinter_schema 1.4.0 → 1.5.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: 820f85b8d22780549e1913cd8fa26f219c425c14746296b466b7966efc58bb2b
4
- data.tar.gz: c013ef3d11fe9c214a21e0400ba2f0c66aa2ca2be0fb62e9feafe0fed0b55e46
3
+ metadata.gz: 392b3344dd71f597ca8c1bfc35e1e3e47c7d01417ba38cd5bf170d6f7e63f1cc
4
+ data.tar.gz: 7aadb18ef24453084f6f23fcb3b0e681c13b7d82b5e12c2d56572e3365f13e3f
5
5
  SHA512:
6
- metadata.gz: 6075da5a95d77f0f0c2142f7d1a9f94e067c863f4f1aa6c042945e5793367633147005824bbd12fff807e10568c653ebd734cebcb9b67d261710178053f61410
7
- data.tar.gz: 7b89616467a6d82202bc2b105f4e948392a4c1fd841c56a6dcb4c66130897ad49d94b2140362576297ca59d615cb37b1eaf2f340eabf01b2702a05790f3808a2
6
+ metadata.gz: 96b8ebe6804c446804b240e1962f223e9130990144421217139e019c1db8e12a891100b1cdb37423e67bc27afd38eb85e1106103a02439032a527511f701d2f0
7
+ data.tar.gz: 0e244fdb17fac6bb6d1ac367f77d97511d5736625850f2a55189dd3da58cceb114dd485b8d7a8f25eab79fd4c79b85339e0128334681cc1100fb07124febe3c7
data/README.md CHANGED
@@ -177,6 +177,49 @@ BlueprinterSchema.generate(
177
177
  )
178
178
  ```
179
179
 
180
+ ### Active Model Support
181
+
182
+ ```rb
183
+ class Restrictions
184
+ include ActiveModel::Model
185
+ include ActiveModel::Attributes
186
+
187
+ attribute :min_units, :integer
188
+ attribute :max_units, :integer
189
+ attribute :label, :string
190
+ end
191
+
192
+ class RestrictionsSerializer < Blueprinter::Base
193
+ field :min_units
194
+ field :max_units
195
+ field :label, type: [:string, :null]
196
+ end
197
+
198
+ BlueprinterSchema.generate(serializer: RestrictionsSerializer, model: Restrictions)
199
+ ```
200
+
201
+ ```rb
202
+ {
203
+ "type" => "object",
204
+ "title" => "Restrictions",
205
+ "properties" => {
206
+ "min_units" => {
207
+ "type" => "integer"
208
+ },
209
+ "max_units" => {
210
+ "type" => "integer"
211
+ },
212
+ "label" => {
213
+ "type" => ["string", "null"]
214
+ }
215
+ },
216
+ "required" => ["max_units", "min_units", "label"],
217
+ "additionalProperties" => false
218
+ }
219
+ ```
220
+
221
+ Note: Attributes are assumed to be non-null. If an attribute is nullable, specify it explicitly in the serializer.
222
+
180
223
  ## Development
181
224
 
182
225
  Devcontainer / Codespaces / Native
@@ -81,13 +81,23 @@ module BlueprinterSchema
81
81
  if field.options[:type]
82
82
  type_definition['type'] = ensure_valid_json_schema_types!(field)
83
83
  elsif @model
84
- column = @model.columns_hash[field.name.to_s]
85
- type_definition = ar_column_to_json_schema(column)
84
+ type_definition = model_attribute_to_json_schema(field.name.to_s)
86
85
  end
87
86
 
88
87
  merge_field_options(type_definition, field.options)
89
88
  end
90
89
 
90
+ def model_attribute_to_json_schema(name)
91
+ if @model.respond_to?(:columns_hash)
92
+ column = @model.columns_hash[name]
93
+ type_to_json_schema(column&.type, column&.null)
94
+ elsif @model.respond_to?(:type_for_attribute)
95
+ type_to_json_schema(@model.type_for_attribute(name)&.type, false)
96
+ else
97
+ @fallback_definition.dup
98
+ end
99
+ end
100
+
91
101
  def merge_field_options(type_definition, options)
92
102
  type_definition['enum'] = options[:enum] if options[:enum]
93
103
  type_definition['items'] = options[:items].deep_stringify_keys if options[:items]
@@ -108,22 +118,24 @@ module BlueprinterSchema
108
118
 
109
119
  # rubocop:disable Metrics/MethodLength
110
120
  # rubocop:disable Metrics/CyclomaticComplexity
111
- def ar_column_to_json_schema(column)
112
- case column&.type
121
+ def type_to_json_schema(type, null)
122
+ case type
113
123
  when :string, :text
114
- build_json_schema_type('string', column.null)
124
+ build_json_schema_type('string', null)
115
125
  when :integer
116
- build_json_schema_type('integer', column.null)
126
+ build_json_schema_type('integer', null)
117
127
  when :float, :decimal
118
- build_json_schema_type('number', column.null)
128
+ build_json_schema_type('number', null)
119
129
  when :boolean
120
- build_json_schema_type('boolean', column.null)
130
+ build_json_schema_type('boolean', null)
121
131
  when :date
122
- build_json_schema_type('string', column.null, 'date')
132
+ build_json_schema_type('string', null, 'date')
123
133
  when :datetime, :timestamp
124
- build_json_schema_type('string', column.null, 'date-time')
134
+ build_json_schema_type('string', null, 'date-time')
125
135
  when :uuid
126
- build_json_schema_type('string', column.null, 'uuid')
136
+ build_json_schema_type('string', null, 'uuid')
137
+ else
138
+ @fallback_definition.dup
127
139
  end
128
140
  end
129
141
  # rubocop:enable Metrics/MethodLength
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlueprinterSchema
4
- VERSION = '1.4.0'
4
+ VERSION = '1.5.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprinter_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thisismydesign