Class: Validate::AST::Rules::Affirmative

Inherits:
Constraint
  • Object
show all
Includes:
Combinator, Validate::Arguments
Defined in:
lib/validate/ast.rb

Instance Method Summary collapse

Methods included from Validate::Arguments

included

Methods included from Combinator

#respond_to_missing?

Methods inherited from Constraint

#==, create_class, inherited, #method_missing, #respond_to_missing?, #to_s

Constructor Details

#initialize(constraints) ⇒ Affirmative

Returns a new instance of Affirmative.



306
307
308
# File 'lib/validate/ast.rb', line 306

def initialize(constraints)
  @constraints = constraints.freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Validate::Constraint

Instance Method Details

#inspectObject



322
323
324
325
326
# File 'lib/validate/ast.rb', line 322

def inspect
  return @constraints.first.inspect if @constraints.one?

  "(#{@constraints.map(&:inspect).join(' | ')})"
end

#messageObject



328
329
330
331
332
333
334
# File 'lib/validate/ast.rb', line 328

def message
  'either ' + @constraints
              .size
              .times
              .map { |i| "[#{constraint_message(i)}]" }
              .join(', or ')
end

#nameObject



318
319
320
# File 'lib/validate/ast.rb', line 318

def name
  'either_' + @constraints.map(&:name).sort.join('_or_')
end

#valid?(value, _ = Constraints::ValidationContext.none) ⇒ Boolean

Returns:

  • (Boolean)


310
311
312
313
314
315
316
# File 'lib/validate/ast.rb', line 310

def valid?(value, _ = Constraints::ValidationContext.none)
  ctx = Constraints::ValidationContext.root(value)
  @constraints.any? do |c|
    ctx.clear_violations
    c.valid?(value, ctx) && !ctx.has_violations?
  end
end