jbuilder-schema 2.4.0 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d21f87d24f37c97ea05dfcdef1688249098d973c62bfec84a98495ae6c57976f
4
- data.tar.gz: c13ab2aeb505785a8915547ce0cf84c7987e0dc0def4e2b20ce5a47be92749d8
3
+ metadata.gz: 7b91da7438556fc92910dbf0f50ac15c54f62593985f1baacb9d1cda87c09e2e
4
+ data.tar.gz: bd9e173ce9473d32ca6c3a62d0af33411e76782a927c6ec6b0542a23a8f340dc
5
5
  SHA512:
6
- metadata.gz: 2aef6970a39b78310a23e6dbbe441ef64ddb03133968cedec87e0e97ecf8b8b83a1a1842d27ff29aa378f8d751963d041233794a1579bc6ecb722e218f0e3cf2
7
- data.tar.gz: '028fb5e6a97b3a714365c1bcdf94b1f584710839685e81f7c4dd2bc514a68fcccbbcf046ddcc890559573f65696407c02023e8321f642861df3ae9d6b6419b91'
6
+ metadata.gz: d5be408235c5190215d78d4d5946f4bb3a28becd80ccbb3a727225efc5f9c48f48903b8ed719296b344bcbf648f9a0d9a2e93ccedf4e475e4e874ca7142b6fa0
7
+ data.tar.gz: 89775b8f6a5819561eb33c8e0af6847167db2de6c0f719981de6bf393e072234fd36baf763dace85e773c5cd761dc93781c81c2a7548f19811111cefa24114e6
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "jbuilder/jbuilder_template"
4
4
  require "active_support/inflections"
5
+ require "method_source"
5
6
 
6
7
  class Jbuilder::Schema
7
8
  class Template < ::JbuilderTemplate
@@ -52,6 +53,7 @@ class Jbuilder::Schema
52
53
  @configuration = Configuration.new(**options)
53
54
  super(context)
54
55
  @ignore_nil = false
56
+ @within_block = false
55
57
  end
56
58
 
57
59
  class TargetWrapper
@@ -65,7 +67,7 @@ class Jbuilder::Schema
65
67
  def to_s
66
68
  @object
67
69
  end
68
- alias unwrap_target! to_s
70
+ alias_method :unwrap_target!, :to_s
69
71
  end
70
72
 
71
73
  def target!
@@ -73,11 +75,6 @@ class Jbuilder::Schema
73
75
  end
74
76
 
75
77
  def schema!
76
- # TODO: Not sure why it was like that, when it was meant to be used,
77
- # but that seems to fix the problem with disappearance of root properties
78
- # https://github.com/bullet-train-co/jbuilder-schema/issues/46
79
- # if ([@attributes] + @attributes.each_value.grep(::Hash)).any? { _1[:type] == :array && _1.key?(:items) }
80
-
81
78
  if [@attributes, *@attributes.first].select { |a| a.is_a?(::Hash) && a[:type] == :array && a.key?(:items) }.any?
82
79
  @attributes
83
80
  else
@@ -87,6 +84,7 @@ class Jbuilder::Schema
87
84
 
88
85
  def set!(key, value = BLANK, *args, schema: nil, **options, &block)
89
86
  old_configuration, @configuration = @configuration, Configuration.build(**schema) if schema&.dig(:object)
87
+ @within_block = _within_block?(&block)
90
88
 
91
89
  _with_schema_overrides(key => schema) do
92
90
  keys = args.presence || _extract_possible_keys(value)
@@ -102,6 +100,7 @@ class Jbuilder::Schema
102
100
  end
103
101
  ensure
104
102
  @configuration = old_configuration if old_configuration
103
+ @within_block = false
105
104
  end
106
105
 
107
106
  alias_method :method_missing, :set! # TODO: Remove once Jbuilder passes keyword arguments along to `set!` in its `method_missing`.
@@ -110,10 +109,14 @@ class Jbuilder::Schema
110
109
  if _partial_options?(options)
111
110
  partial!(collection: collection, **options)
112
111
  else
112
+ @within_block = _within_block?(&block)
113
+
113
114
  _with_schema_overrides(schema) do
114
115
  _attributes.merge! type: :array, items: _scope { super(collection, *args, &block) }
115
116
  end
116
117
  end
118
+ ensure
119
+ @within_block = false
117
120
  end
118
121
 
119
122
  def extract!(object, *attributes, schema: nil)
@@ -128,7 +131,12 @@ class Jbuilder::Schema
128
131
  local = options.except(:partial, :as, :collection, :cached, :schema).first
129
132
  as = options[:as] || ((local.is_a?(::Array) && local.size == 2 && local.first.is_a?(::Symbol) && local.last.is_a?(::Object)) ? local.first.to_s : nil)
130
133
 
131
- _set_ref(as&.to_s || partial || model, array: collection&.any?)
134
+ if @within_block || collection.present?
135
+ _set_ref(as&.to_s || partial || model, array: collection&.any?)
136
+ else
137
+ json = ::Jbuilder::Schema.renderer.original_render partial: model || partial, locals: options
138
+ json.each { |key, value| set!(key, value) }
139
+ end
132
140
  end
133
141
  end
134
142
 
@@ -254,5 +262,15 @@ class Jbuilder::Schema
254
262
  value = _object(value, _required!(value.keys)) unless value[:type] == :array || value.key?(:$ref)
255
263
  _merge_values(current_value, value)
256
264
  end
265
+
266
+ def _within_block?(&block)
267
+ block.present? && _one_line?(block.source)
268
+ end
269
+
270
+ def _one_line?(text)
271
+ text = text.gsub("{", " do\n").gsub("}", "\nend").tr(";", "\n")
272
+ lines = text.lines[1..-2].reject { |line| line.strip.empty? || line.strip.start_with?("#") }
273
+ lines.size == 1
274
+ end
257
275
  end
258
276
  end
@@ -1,4 +1,4 @@
1
1
  # We can't use the standard `Jbuilder::Schema::VERSION =` because
2
2
  # `Jbuilder` isn't a regular module namespace, but a class …which also loads Active Support.
3
3
  # So we use trickery, and assign the proper version once `jbuilder/schema.rb` is loaded.
4
- JBUILDER_SCHEMA_VERSION = "2.4.0"
4
+ JBUILDER_SCHEMA_VERSION = "2.5.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Sidorov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-09 00:00:00.000000000 Z
11
+ date: 2023-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jbuilder
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 5.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: method_source
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Generate JSON Schema from Jbuilder files
42
56
  email:
43
57
  - hey@yurisidorov.com