Class: DryStructGenerator::StructGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/dry_struct_generator/struct_generator.rb

Constant Summary collapse

@@definitions =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#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) ⇒ StructGenerator

Returns a new instance of StructGenerator.



9
10
11
12
13
14
15
16
17
# File 'lib/dry_struct_generator/struct_generator.rb', line 9

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

Instance Attribute Details

#struct_classObject

Returns the value of attribute struct_class.



7
8
9
# File 'lib/dry_struct_generator/struct_generator.rb', line 7

def struct_class
  @struct_class
end

#type_to_dry_typeObject

Returns the value of attribute type_to_dry_type.



7
8
9
# File 'lib/dry_struct_generator/struct_generator.rb', line 7

def type_to_dry_type
  @type_to_dry_type
end

#validation_schema_parserObject

Returns the value of attribute validation_schema_parser.



7
8
9
# File 'lib/dry_struct_generator/struct_generator.rb', line 7

def validation_schema_parser
  @validation_schema_parser
end

Class Method Details

.definitionsObject



19
20
21
# File 'lib/dry_struct_generator/struct_generator.rb', line 19

def self.definitions
  @@definitions
end

Instance Method Details

#call(validator, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/dry_struct_generator/struct_generator.rb', line 23

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

#generate(validator_definition) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/dry_struct_generator/struct_generator.rb', line 34

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

#get_field_type(schema) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dry_struct_generator/struct_generator.rb', line 44

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