Sha256: 66be983cb23de156399265b07445ff4e407adc70b4959c702da5847dc65d3d60

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

module DryStructGenerator
  class StructGenerator
    @@definitions = {}

    attr_accessor :struct_class, :type_to_dry_type, :validation_schema_parser

    def initialize(
      struct_class: Config::GeneratorConfiguration.struct_class,
      validation_schema_parser: Config::GeneratorConfiguration.validation_schema_parser,
      type_to_dry_type: Config::GeneratorConfiguration.type_to_dry_type
    )
      self.struct_class = struct_class
      self.type_to_dry_type = type_to_dry_type
      self.validation_schema_parser = validation_schema_parser
    end

    def self.definitions
      @@definitions
    end

    def call(validator, options = {})
      return @@definitions[validator] if @@definitions[validator]

      validator_definition = validation_schema_parser.new.call(validator).keys.merge(options)
      result = generate(validator_definition)

      @@definitions[validator] = result

      result
    end

    def generate(validator_definition)
      instance = self
      Class.new(struct_class) do
        validator_definition.each do |field_name, schema|
          type = instance.get_field_type(schema)
          schema[:required] ? attribute(field_name.to_sym, type) : attribute?(field_name.to_sym, type)
        end
      end
    end

    def get_field_type(schema)
      if schema[:array]
        type = type_to_dry_type[:array]
        if schema[:keys]
          type = type.of(generate(schema[:keys].to_sym))
        elsif schema[:type]
          type = type.of(type_to_dry_type[schema[:type].to_sym])
        end
      elsif schema[:keys]
        type = generate(schema[:keys])
      else
        type = type_to_dry_type[schema[:type].to_sym]
      end

      type = type.optional if schema[:nullable]
      type = type.default(schema[:default].freeze) if schema[:default]

      type
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry_struct_generator-0.3.2 lib/dry_struct_generator/struct_generator.rb
dry_struct_generator-0.3.1 lib/dry_struct_generator/struct_generator.rb