Class: Meta::JsonSchema::ObjectSchemaBuilder

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Locked::LockedMethodAlias
Defined in:
lib/meta/json_schema/builders/object_schema_builder.rb

Defined Under Namespace

Classes: Locked, WithCommonOptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Locked::LockedMethodAlias

#add_scope, #lock, #method_missing

Constructor Details

#initialize(options = {}) ⇒ ObjectSchemaBuilder

Returns a new instance of ObjectSchemaBuilder.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 90

def initialize(options = {}, &)
  raise 'type 选项必须是 object' if !options[:type].nil? && options[:type] != 'object'

  @schema_cache = {} # 用于缓存已经生成的 schema,重复利用

  @properties = {} # 所有的属性已经生成
  @required = []
  @validations = {}

  options = options.merge(type: 'object')
  properties = options.delete(:properties)
  @options = options

  properties&.each do |name, property_options|
    if property_options.is_a?(Hash)
      property name, property_options
    elsif property_options.is_a?(BaseSchema)
      @properties[name.to_sym] = property_options
    else
      raise ArgumentError, "属性 #{name} 的类型不正确"
    end
  end

  instance_exec(&) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Meta::JsonSchema::ObjectSchemaBuilder::Locked::LockedMethodAlias

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



88
89
90
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 88

def properties
  @properties
end

Instance Method Details

#locked(options) ⇒ Object



174
175
176
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 174

def locked(options)
  Locked.new(self, **options)
end

#merge(schema_builder) ⇒ Object



155
156
157
158
159
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 155

def merge(schema_builder)
  schema_builder = schema_builder.schema_builder if schema_builder.respond_to?(:schema_builder)

  @properties.merge!(schema_builder.properties)
end

#params(options = {}, &block) ⇒ Object



147
148
149
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 147

def params(options = {}, &block)
  with_common_options(**options, render: false, &block)
end

#property(name, options = {}, &block) ⇒ Object Also known as: expose, param



125
126
127
128
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 125

def property(name, options = {}, &block)
  @properties[name.to_sym] = Properties.build_property(options, ->(options) { SchemaBuilderTool.build(options, &block) })
  # @properties[name.to_sym] = SchemaBuilderTool.build(options, &block)
end

#render(options = {}, &block) ⇒ Object



151
152
153
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 151

def render(options = {}, &block)
  with_common_options(**options, param: false, &block)
end

#schema_name(schema_base_name = nil) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 116

def schema_name(schema_base_name = nil)
  if schema_base_name
    raise TypeError, "schema_base_name 必须是一个 String,当前是:#{schema_base_name.class}" unless schema_base_name.is_a?(String)
    @schema_name = schema_base_name
  else
    @schema_name
  end
end

#scope(scope, options = {}) ⇒ Object



143
144
145
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 143

def scope(scope, options = {}, &)
  with_common_options(**options, scope: scope, &)
end

#to_schema(locked_options = {}) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 166

def to_schema(locked_options = {})
  locked_options = SchemaOptions::UserOptions::Filter.check(locked_options.compact)
  return @schema_cache[locked_options] if @schema_cache[locked_options]

  properties = @schema_name ? NamedProperties.new(@properties, @schema_name) : Properties.new(@properties)
  @schema_cache[locked_options] = ObjectSchema.new(properties: properties, options: @options, locked_options: locked_options)
end

#use(proc) ⇒ Object

能且仅能 ObjectSchemaBuilder 内能使用 use 方法



134
135
136
137
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 134

def use(proc)
  proc = proc.to_proc if proc.respond_to?(:to_proc)
  instance_exec(&proc)
end

#with_common_options(common_options, &block) ⇒ Object



139
140
141
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 139

def with_common_options(common_options, &block)
  WithCommonOptions.new(self, common_options, &block)
end

#within(*properties) ⇒ Object Also known as: []



161
162
163
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 161

def within(*properties)
  to_schema.properties.within(*properties)
end