lib/dry/schema/processor.rb in dry-schema-0.3.0 vs lib/dry/schema/processor.rb in dry-schema-0.4.0

- old
+ new

@@ -30,46 +30,45 @@ setting :key_map_type setting :type_registry, TypeRegistry.new param :steps, default: -> { EMPTY_ARRAY.dup } - # Define a schema for your processor class - # - # @see Params - # @see JSON - # - # @return [Class] - # - # @api public - def self.define(&block) - @__definition__ ||= DSL.new( - processor_type: self, parent: superclass.definition, **config, &block - ) - self - end + class << self + # @!attribute [r] definition + # Return DSL configured via #define + # @return [DSL] + # @api private + attr_reader :definition - # Return DSL configured via #define - # - # @return [DSL] - # - # @api private - def self.definition - @__definition__ ||= nil - end + # Define a schema for your processor class + # + # @see Params + # @see JSON + # + # @return [Class] + # + # @api public + def define(&block) + @definition ||= DSL.new( + processor_type: self, parent: superclass.definition, **config, &block + ) + self + end - # Build a new processor object - # - # @return [Processor] - # - # @api public - def self.new(&block) - if block - super.tap(&block) - elsif definition - definition.call - else - raise ArgumentError, 'Cannot create a schema without a definition' + # Build a new processor object + # + # @return [Processor] + # + # @api public + def new(&block) + if block + super.tap(&block) + elsif definition + definition.call + else + raise ArgumentError, 'Cannot create a schema without a definition' + end end end # Append a step # @@ -94,36 +93,57 @@ output = step.(result) result.replace(output) if output.is_a?(::Hash) end end end + alias_method :[], :call + # Return a proc that acts like a schema object + # + # @return [Proc] + # + # @api public + def to_proc + ->(input) { call(input) } + end + # Return the key map # # @return [KeyMap] # # @api public def key_map - @__key_map__ ||= steps.detect { |s| s.is_a?(KeyCoercer) }.key_map + @key_map ||= steps.detect { |s| s.is_a?(KeyCoercer) }.key_map end + # Return string represntation + # + # @return [String] + # + # @api public + def inspect + <<~STR.strip + #<#{self.class.name} keys=#{key_map.map(&:dump)} rules=#{rules.map { |k, v| [k, v.to_s] }.to_h}> + STR + end + # Return the type schema # # @return [Dry::Types::Safe] # # @api private def type_schema - @__type_schema__ ||= steps.detect { |s| s.is_a?(ValueCoercer) }.type_schema + @type_schema ||= steps.detect { |s| s.is_a?(ValueCoercer) }.type_schema end # Return the rules config # # @return [Dry::Types::Config] # # @api private def config - @__config__ ||= steps.detect { |s| s.is_a?(RuleApplier) }.config + @config ||= rule_applier.config end # Return AST representation of the rules # # @api private @@ -151,11 +171,10 @@ # Return the rule applier # # @api private def rule_applier - # TODO: make this more explicit through class types - @__rule_applier__ ||= steps.last + @rule_applier ||= steps.last end alias_method :to_rule, :rule_applier end end end