Class: Ask::Schema::DSL::ConditionalContext

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/schema/dsl/conditionals.rb

Overview

Execution context for given blocks, providing requires, validates, and otherwise DSL methods.

Instance Method Summary collapse

Constructor Details

#initialize(then_builder, else_builder) ⇒ ConditionalContext

Returns a new instance of ConditionalContext.

Parameters:



122
123
124
125
# File 'lib/ask/schema/dsl/conditionals.rb', line 122

def initialize(then_builder, else_builder)
  @then_builder = then_builder
  @else_builder = else_builder
end

Instance Method Details

#otherwise(&block) ⇒ Object

Define the “else” clause for when the condition is not met.

Parameters:

  • block (Proc)

    Block declaring requirements



151
152
153
# File 'lib/ask/schema/dsl/conditionals.rb', line 151

def otherwise(&block)
  @else_builder.instance_eval(&block)
end

#requires(*fields) ⇒ Object

Mark fields as required when the condition is met.

Parameters:

  • fields (Array<Symbol>)

    Field names to require



129
130
131
# File 'lib/ask/schema/dsl/conditionals.rb', line 129

def requires(*fields)
  @then_builder.requires(*fields)
end

#validates(field, **options) ⇒ Object

Add validation constraints for a field.

Parameters:

  • field (Symbol)

    Field name

  • options (Hash)

    Validation constraints

Options Hash (**options):

  • :type (Symbol)

    Expected JSON type

  • :const (Object)

    Expected constant value

  • :enum (Array)

    Allowed values

  • :not_value (Object)

    Disallowed value (maps to not)

  • :min_length (Integer)

    Minimum string length

  • :max_length (Integer)

    Maximum string length

  • :pattern (String, Regexp)

    Regex pattern

  • :minimum (Numeric)

    Minimum number

  • :maximum (Numeric)

    Maximum number



145
146
147
# File 'lib/ask/schema/dsl/conditionals.rb', line 145

def validates(field, **options)
  @then_builder.validates(field, **options)
end