Class: Dry::Generators::ValidationGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/generators/dry/validation/validation_generator.rb

Constant Summary

Constants inherited from Base

Base::DRY_PATH, Base::MONADS_PATH, Base::SPEC_PATH, Base::STRUCTS_PATH, Base::TEST_PATH, Base::VALIDATIONS_PATH

Instance Method Summary collapse

Instance Method Details

#check_requirementsObject



9
10
11
# File 'lib/generators/dry/validation/validation_generator.rb', line 9

def check_requirements
  raise 'NAME must be provided' unless file_name.present?
end

#copy_specObject



41
42
43
44
45
46
47
# File 'lib/generators/dry/validation/validation_generator.rb', line 41

def copy_spec
  if Dir.exist? File.join(Rails.root, 'spec')
    spec_path =  File.join(SPEC_PATH, 'validations')
    path = File.join(spec_path, "#{@name}_spec.rb")
    template 'validation_spec_template.erb', path
  end
end

#copy_testObject



49
50
51
52
53
54
55
# File 'lib/generators/dry/validation/validation_generator.rb', line 49

def copy_test
  if Dir.exist? File.join(Rails.root, 'test')
    test_path =  File.join(TEST_PATH, 'validations')
    path = File.join(test_path, "#{@name}_test.rb")
    template 'validation_test_template.erb', path
  end
end

#copy_validatorObject



36
37
38
39
# File 'lib/generators/dry/validation/validation_generator.rb', line 36

def copy_validator
  path = File.join(VALIDATIONS_PATH, "#{@name}.rb")
  template 'validation_template.erb', path
end

#process_paramsObject

:name, :type, :required


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/generators/dry/validation/validation_generator.rb', line 20

def process_params
  options['params'].each do |field|
    fields = field.split(':')
    required = false
    type = 'string'

    if fields.length == 3
      required = true
      fields.pop
    end

    type = fields.pop if fields.length == 2
    @fields << { name: fields.first, type: type, required: required }
  end
end

#set_instance_variablesObject



13
14
15
16
17
# File 'lib/generators/dry/validation/validation_generator.rb', line 13

def set_instance_variables
  @name = file_name.underscore
  @class_name = "#{@name.camelize}"
  @fields = []
end