Sha256: 1f33d25181943b7089038273300e5c86ed7fac4e0479d6370bd80e3dfddb4ddb
Contents?: true
Size: 1.23 KB
Versions: 2
Compression:
Stored size: 1.23 KB
Contents
# frozen_string_literal: true require "dry/struct" module Dry module Schema module Macros class StructToSchema < ::Dry::Struct::Compiler def call(struct) visit(struct.to_ast) end # strip away structs from AST def visit_struct(node) _, ast = node visit(ast) end end Hash.option :struct_compiler, default: proc { StructToSchema.new(schema_dsl.config.types) } Hash.prepend(::Module.new { def call(*args) if args.size >= 1 && struct?(args[0]) if block_given? raise ArgumentError, "blocks are not supported when using " \ "a struct class (#{name.inspect} => #{args[0]})" end schema = struct_compiler.(args[0]) super(schema, *args.drop(1)) type(schema_dsl.types[name].constructor(schema)) else super end end private def struct?(type) type.is_a?(::Class) && type <= ::Dry::Struct end }) end PredicateInferrer::Compiler.alias_method(:visit_struct, :visit_hash) PrimitiveInferrer::Compiler.alias_method(:visit_struct, :visit_hash) end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dry-schema-1.14.1 | lib/dry/schema/extensions/struct.rb |
dry-schema-1.14.0 | lib/dry/schema/extensions/struct.rb |