Class: Meta::JsonSchema::RefSchema
- Inherits:
-
BaseSchema
- Object
- BaseSchema
- Meta::JsonSchema::RefSchema
- Defined in:
- lib/meta/json_schema/schemas/ref_schema.rb
Instance Attribute Summary collapse
-
#object_schema ⇒ Object
readonly
Returns the value of attribute object_schema.
Attributes inherited from BaseSchema
Instance Method Summary collapse
- #defined_scopes(stage:, defined_scopes_mapping:) ⇒ Object
- #filter(value, user_options = {}) ⇒ Object
-
#initialize(object_schema, options = {}) ⇒ RefSchema
constructor
A new instance of RefSchema.
- #to_schema_doc(user_options) ⇒ Object
Methods inherited from BaseSchema
#filter?, #find_schema, #if?, #scoped, #staged, #to_schema, #value?
Constructor Details
#initialize(object_schema, options = {}) ⇒ RefSchema
Returns a new instance of RefSchema.
10 11 12 13 14 15 |
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 10 def initialize(object_schema, = {}) raise ArgumentError, 'object_schema 必须是一个 ObjectSchema' unless object_schema.is_a?(ObjectSchema) super() @object_schema = object_schema end |
Instance Attribute Details
#object_schema ⇒ Object (readonly)
Returns the value of attribute object_schema.
8 9 10 |
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 8 def object_schema @object_schema end |
Instance Method Details
#defined_scopes(stage:, defined_scopes_mapping:) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 40 def defined_scopes(stage:, defined_scopes_mapping:) defined_scopes_mapping ||= {} if object_schema.properties.respond_to?(:schema_name) # 只有命名实体才会被缓存 schema_name = object_schema.properties.schema_name(stage) return defined_scopes_mapping[schema_name] if defined_scopes_mapping.key?(schema_name) end defined_scopes_mapping[schema_name] = [] # 求解 defined_scopes,最终结果去重 + 排序 defined_scopes = object_schema.properties.each.map do |name, property| property.defined_scopes(stage: stage, defined_scopes_mapping: defined_scopes_mapping) end.flatten.uniq.sort_by(&:name) defined_scopes_mapping[schema_name] = defined_scopes defined_scopes end |
#filter(value, user_options = {}) ⇒ Object
17 18 19 20 |
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 17 def filter(value, = {}) value = super object_schema.filter(value, ) end |
#to_schema_doc(user_options) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/meta/json_schema/schemas/ref_schema.rb', line 22 def to_schema_doc() raise '引用的 ObjectSchema 没有包含命名逻辑,无法生成文档' unless object_schema.naming? # 首先,要求出 defined_scopes defined_scopes = self.defined_scopes(stage: [:stage], defined_scopes_mapping: [:defined_scopes_mapping]) # 然后,求出 schema_name schema_name = object_schema.resolve_name([:stage], [:scope], defined_scopes) # 接着将 Schema 写进 schemas 选项中去 schema_components = [:schema_docs_mapping] || {} unless schema_components.key?(schema_name) schema_components[schema_name] = nil # 首先设置 schemas 防止出现无限循环 schema_components[schema_name] = object_schema.to_schema_doc(**) # 原地修改 schemas,无妨 end # 最后,返回这个 $ref 结构 { '$ref': "#/components/schemas/#{schema_name}" } end |