Class: RailsValidationApi::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_validation_api/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL



7
8
9
# File 'lib/rails_validation_api/dsl.rb', line 7

def initialize
  @rules = {}
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



5
6
7
# File 'lib/rails_validation_api/dsl.rb', line 5

def rules
  @rules
end

Instance Method Details

#param!(name, type, **opts, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rails_validation_api/dsl.rb', line 11

def param!(name, type, **opts, &block)
  key = :"#{name}_validate"
  rule = { field: name, type: type, opts: [opts].compact }

  if block_given?
    nested = DSL.new
    nested.instance_eval(&block)
    rule[:items] = nested.rules.values
  end

  @rules[key] = rule
end