Class: Validate::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/validate/parser.rb

Overview

The rules for parsing validations are such:

  • Validations are method calls (starting with the string ‘validates_`)

  • followed by field names as regular arguments (as symbols)

  • any options are included in an options hash, eg. is: String

  • and native blocks are reserved for children-validations

For example:

validates_subhash :iap1, :iap2, when: -> { type == :iap } do
  validates_type_of :id,   is: String
  validates_type_of :tier, is: Numeric
end

Class Method Summary collapse

Class Method Details

.parse(&block) ⇒ Object



20
21
22
23
24
25
# File 'lib/validate/parser.rb', line 20

def self.parse(&block)
  # execute block, return array of validation methods called
  context = BlockParsingContext.new
  context.instance_exec(&block)
  context.validations
end