Class: Meta::JsonSchema::StagingSchema

Inherits:
BaseSchema
  • Object
show all
Defined in:
lib/meta/json_schema/schemas/staging_schema.rb

Overview

内含 param_schema, render_schema, default_schema,分别用于不同的阶段。

Instance Attribute Summary collapse

Attributes inherited from BaseSchema

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseSchema

#filter?, #find_schema, #if?, #scoped, #to_schema, #value?

Constructor Details

#initialize(param_schema:, render_schema:) ⇒ StagingSchema

Returns a new instance of StagingSchema.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
# File 'lib/meta/json_schema/schemas/staging_schema.rb', line 13

def initialize(param_schema:, render_schema:)
  raise ArgumentError, 'param_schema 选项重复提交为 StagingSchema' if param_schema.is_a?(StagingSchema)
  raise ArgumentError, 'render_schema 选项重复提交为 StagingSchema' if render_schema.is_a?(StagingSchema)

  @param_schema = param_schema
  @render_schema = render_schema
end

Instance Attribute Details

#param_schemaObject (readonly)

Returns the value of attribute param_schema.



11
12
13
# File 'lib/meta/json_schema/schemas/staging_schema.rb', line 11

def param_schema
  @param_schema
end

#render_schemaObject (readonly)

Returns the value of attribute render_schema.



11
12
13
# File 'lib/meta/json_schema/schemas/staging_schema.rb', line 11

def render_schema
  @render_schema
end

Class Method Details

.build_from_options(options, build_schema = ->(opts) { BaseSchema.new(opts) }) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/meta/json_schema/schemas/staging_schema.rb', line 49

def self.build_from_options(options, build_schema = ->(opts) { BaseSchema.new(opts) })
  param_opts, render_opts, common_opts = SchemaOptions.divide_to_param_and_render(options)
  if param_opts == common_opts && render_opts == common_opts
    return ScopingSchema.build_from_options(common_opts, build_schema)
  else
    StagingSchema.new(
      param_schema: param_opts ? ScopingSchema.build_from_options(param_opts, build_schema) : UnsupportedSchema.new(:stage, :param),
      render_schema: render_opts ? ScopingSchema.build_from_options(render_opts, build_schema) : UnsupportedSchema.new(:stage, :render)
    )
  end
end

Instance Method Details

#defined_scopes(stage:, **kwargs) ⇒ Object



41
42
43
# File 'lib/meta/json_schema/schemas/staging_schema.rb', line 41

def defined_scopes(stage:, **kwargs)
  staged(stage).defined_scopes(stage: stage, **kwargs)
end

#filter(value, user_options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/meta/json_schema/schemas/staging_schema.rb', line 21

def filter(value, user_options = {})
  if user_options[:stage] == :param
    param_schema.filter(value, user_options)
  elsif user_options[:stage] == :render
    render_schema.filter(value, user_options)
  else
    raise ArgumentError, "stage 选项必须是 :param 或 :render"
  end
end

#staged(stage) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/meta/json_schema/schemas/staging_schema.rb', line 31

def staged(stage)
  if stage == :param
    param_schema
  elsif stage == :render
    render_schema
  else
    raise ArgumentError, "stage 选项必须是 :param 或 :render"
  end
end

#to_schema_doc(stage:, **kwargs) ⇒ Object



45
46
47
# File 'lib/meta/json_schema/schemas/staging_schema.rb', line 45

def to_schema_doc(stage:, **kwargs)
  staged(stage).to_schema_doc(stage: stage, **kwargs)
end