Class: Dry::Schema::Processor

Inherits:
Object
  • Object
show all
Extended by:
Configurable, Initializer
Includes:
Logic::Operators, Info::SchemaMethods, JSONSchema::SchemaMethods
Defined in:
lib/dry/schema/processor.rb

Overview

Processes input data using objects configured within the DSL Processing is split into steps represented by ‘ProcessorSteps`.

Direct Known Subclasses

JSON, Params

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from JSONSchema::SchemaMethods

#json_schema

Methods included from Info::SchemaMethods

#info

Class Attribute Details

.definitionDSL (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return DSL configured via #define

Returns:



34
35
36
# File 'lib/dry/schema/processor.rb', line 34

def definition
  @definition
end

Class Method Details

.define(&block) ⇒ Class

Define a schema for your processor class

Returns:

  • (Class)

See Also:

  • Schema#define
  • Schema#Params
  • Schema#JSON


45
46
47
48
49
50
# File 'lib/dry/schema/processor.rb', line 45

def define(&block)
  @definition ||= DSL.new(
    processor_type: self, parent: superclass.definition, **config.to_h, &block
  )
  self
end

.new(options = nil, &block) ⇒ Processor

Build a new processor object

Returns:



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dry/schema/processor.rb', line 57

def new(options = nil, &block)
  if options || block
    processor = super(**(options || EMPTY_HASH))
    yield(processor) if block
    processor
  elsif definition
    definition.call
  else
    raise ArgumentError, "Cannot create a schema without a definition"
  end
end

Instance Method Details

#call(input) ⇒ Result Also known as: []

Apply processing steps to the provided input

Parameters:

  • input (Hash)

Returns:



77
78
79
80
81
# File 'lib/dry/schema/processor.rb', line 77

def call(input)
  Result.new(input.dup, message_compiler: message_compiler) do |result|
    steps.call(result)
  end
end

#configDry::Types::Config

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the rules config

Returns:

  • (Dry::Types::Config)


161
162
163
# File 'lib/dry/schema/processor.rb', line 161

def config
  @config ||= rule_applier.config
end

#filter_rules?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if there are filter rules

Returns:

  • (Boolean)


201
202
203
# File 'lib/dry/schema/processor.rb', line 201

def filter_rules?
  @filter_rules_predicate ||= schema_dsl.filter_rules?
end

#filter_schemaObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return filter schema



208
209
210
# File 'lib/dry/schema/processor.rb', line 208

def filter_schema
  @filter_schema ||= schema_dsl.filter_schema
end

#inspectString

Return string representation

Returns:

  • (String)


115
116
117
118
119
# File 'lib/dry/schema/processor.rb', line 115

def inspect
  <<~STR.strip
    #<#{self.class.name} keys=#{key_map.map(&:dump)} rules=#{rules.transform_values(&:to_s)}>
  STR
end

#key_mapKeyMap

Return the key map

Returns:



126
127
128
# File 'lib/dry/schema/processor.rb', line 126

def key_map
  steps.key_map
end

#merge(other) ⇒ Processor, ...

Merge with another schema

Parameters:

Returns:



97
98
99
# File 'lib/dry/schema/processor.rb', line 97

def merge(other)
  schema_dsl.merge(other.schema_dsl).()
end

#message_compilerMessageCompiler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the message compiler

Returns:



178
179
180
# File 'lib/dry/schema/processor.rb', line 178

def message_compiler
  rule_applier.message_compiler
end

#rule_applierObject Also known as: to_rule

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the rule applier



151
152
153
# File 'lib/dry/schema/processor.rb', line 151

def rule_applier
  steps.rule_applier
end

#rulesMessageCompiler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the rules from rule applier

Returns:



187
188
189
# File 'lib/dry/schema/processor.rb', line 187

def rules
  rule_applier.rules
end

#strict_type_schemaDry::Types::Schema

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return type schema used when composing subschemas

Returns:

  • (Dry::Types::Schema)


144
145
146
# File 'lib/dry/schema/processor.rb', line 144

def strict_type_schema
  schema_dsl.strict_type_schema
end

#to_astObject Also known as: ast

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return AST representation of the rules



168
169
170
# File 'lib/dry/schema/processor.rb', line 168

def to_ast(*)
  rule_applier.to_ast
end

#to_procProc

Return a proc that acts like a schema object

Returns:

  • (Proc)


106
107
108
# File 'lib/dry/schema/processor.rb', line 106

def to_proc
  ->(input) { call(input) }
end

#type_schemaDry::Types::Lax

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the type schema

Returns:

  • (Dry::Types::Lax)


135
136
137
# File 'lib/dry/schema/processor.rb', line 135

def type_schema
  steps.type_schema
end

#typesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the types from the schema DSL



194
195
196
# File 'lib/dry/schema/processor.rb', line 194

def types
  schema_dsl.types
end

#xor(_other) ⇒ Object Also known as: ^

Raises:

  • (NotImplementedError)


85
86
87
# File 'lib/dry/schema/processor.rb', line 85

def xor(_other)
  raise NotImplementedError, "composing schemas using `xor` operator is not supported yet"
end