Class: Meta::JsonSchema::ObjectSchemaBuilder
- Inherits:
-
Object
- Object
- Meta::JsonSchema::ObjectSchemaBuilder
- Defined in:
- lib/meta/json_schema/builders/object_schema_builder.rb
Defined Under Namespace
Classes: Locked
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ ObjectSchemaBuilder
constructor
A new instance of ObjectSchemaBuilder.
- #lock(key, value) ⇒ Object
- #locked(options) ⇒ Object
- #property(name, options = {}, &block) ⇒ Object (also: #expose, #param)
-
#schema_name(schema_name_resolver) ⇒ Object
设置 schema_name.
- #to_schema(locked_options = nil) ⇒ Object
-
#use(proc) ⇒ Object
能且仅能 ObjectSchemaBuilder 内能使用 use 方法.
Constructor Details
#initialize(options = {}) ⇒ ObjectSchemaBuilder
Returns a new instance of ObjectSchemaBuilder.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 8 def initialize( = {}, &) raise 'type 选项必须是 object' if ![:type].nil? && [:type] != 'object' @properties = {} @required = [] @validations = {} = .merge(type: 'object') properties = .delete(:properties) = properties&.each do |name, | property name, end instance_exec(&) if block_given? end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object (private)
80 81 82 83 84 85 86 87 |
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 80 def method_missing(method, *args) if method =~ /^lock_(\w+)$/ key = Regexp.last_match(1) lock(key.to_sym, *args) else super end end |
Instance Method Details
#lock(key, value) ⇒ Object
62 63 64 |
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 62 def lock(key, value) locked(key => value) end |
#locked(options) ⇒ Object
66 67 68 |
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 66 def locked() Locked.new(self, ) end |
#property(name, options = {}, &block) ⇒ Object Also known as: expose, param
45 46 47 |
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 45 def property(name, = {}, &block) @properties[name.to_sym] = Properties.build_property(, ->() { SchemaBuilderTool.build(, &block) }) end |
#schema_name(schema_name_resolver) ⇒ Object
设置 schema_name.
一、可以传递一个块,该块会接收 locked_scope 参数,需要返回一个带有 param 和 render 键的 Hash. 二、可以传递一个 Hash,它包含 param 和 render 键。三、可以传递一个字符串。
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 31 def schema_name(schema_name_resolver) if schema_name_resolver.is_a?(Proc) @schema_name_resolver = schema_name_resolver elsif schema_name_resolver.is_a?(Hash) @schema_name_resolver = proc { |stage, locked_scopes| schema_name_resolver[stage] } elsif schema_name_resolver.is_a?(String) @schema_name_resolver = proc { |stage, locked_scopes| schema_name_resolver } elsif schema_name_resolver.nil? @schema_name_resolver = proc { nil } else raise TypeError, "schema_name_resolver 必须是一个 Proc、Hash 或 String,当前是:#{schema_name_resolver.class}" end end |
#to_schema(locked_options = nil) ⇒ Object
58 59 60 |
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 58 def to_schema( = nil) ObjectSchema.new(properties: @properties, options: , locked_options: , schema_name_resolver: @schema_name_resolver) end |
#use(proc) ⇒ Object
能且仅能 ObjectSchemaBuilder 内能使用 use 方法
53 54 55 56 |
# File 'lib/meta/json_schema/builders/object_schema_builder.rb', line 53 def use(proc) proc = proc.to_proc if proc.respond_to?(:to_proc) instance_exec(&proc) end |