Class: ValidateParams::Validatable::ParamBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/validate_params/param_builder.rb

Defined Under Namespace

Classes: Validation

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil, validations: []) ⇒ ParamBuilder

Returns a new instance of ParamBuilder.



40
41
42
43
# File 'lib/validate_params/param_builder.rb', line 40

def initialize(parent: nil, validations: [])
  @parent = parent
  @validations = validations
end

Instance Method Details

#param(field, type, options = {}) ⇒ Object



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

def param(field, type, options = {})
  options = ValidateParams::Validatable.configuration.to_h.merge(options)
  validation = Validation.new(field, type, options, [], @parent)

  if block_given?
    if ![Array, Hash].include?(type)
      raise "#{type} type cannot have nested definitions, only Array or Hash are supported"
    end

    yield ParamBuilder.new(parent: validation)
  end

  if @parent
    @parent.children << validation
  else
    @validations << validation
  end
end