Sha256: e11dc1bee4d787eb88f61be01a79a7b6df205126fc1bc98f1f71e41350aeac3e

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

require 'dry/types/compiler'

module Dry
  class Struct
    # @private
    class StructBuilder < Dry::Types::Compiler
      attr_reader :struct

      def initialize(struct)
        super(Dry::Types)
        @struct = struct
      end

      # @param [Symbol|String] attr_name the name of the nested type
      # @param [Dry::Struct,Dry::Types::Type::Array] type the superclass of the nested struct
      # @yield the body of the nested struct
      def call(attr_name, type, &block)
        const_name = const_name(type, attr_name)
        check_name(const_name)

        new_type = Class.new(parent(type), &block)
        struct.const_set(const_name, new_type)

        if array?(type)
          type.of(new_type)
        else
          new_type
        end
      end

      private

      def array?(type)
        type.is_a?(Types::Type) && type.primitive.equal?(Array)
      end

      def parent(type)
        if array?(type)
          visit(type.to_ast)
        else
          type || default_superclass
        end
      end

      def default_superclass
        struct < Value ? Value : Struct
      end

      def const_name(type, attr_name)
        snake_name = if array?(type)
                       Dry::Core::Inflector.singularize(attr_name)
                     else
                       attr_name
                     end

        Dry::Core::Inflector.camelize(snake_name)
      end

      def check_name(name)
        raise(
          Struct::Error,
          "Can't create nested attribute - `#{struct}::#{name}` already defined"
        ) if struct.const_defined?(name)
      end

      def visit_constrained(node)
        definition, * = node
        visit(definition)
      end

      def visit_array(node)
        member, * = node
        member
      end

      def visit_definition(*)
        default_superclass
      end

      def visit_constructor(node)
        definition, * = node
        visit(definition)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-struct-0.5.0 lib/dry/struct/struct_builder.rb