Class: Validate::AST::Rules::Negative

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) ⇒ Negative

Returns a new instance of Negative.



347
348
349
# File 'lib/validate/ast.rb', line 347

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



373
374
375
376
377
# File 'lib/validate/ast.rb', line 373

def inspect
  return "!#{@constraints.first.inspect}" if @constraints.one?

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

#messageObject



363
364
365
366
367
368
369
370
371
# File 'lib/validate/ast.rb', line 363

def message
  return "not [#{constraint_message(0)}]" if @constraints.one?

  'neither ' + @constraints
               .size
               .times
               .map { |i| "[#{constraint_message(i)}]" }
               .join(', nor ')
end

#nameObject



359
360
361
# File 'lib/validate/ast.rb', line 359

def name
  'neither_' + @constraints.map(&:name).sort.join('_nor_')
end

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

Returns:

  • (Boolean)


351
352
353
354
355
356
357
# File 'lib/validate/ast.rb', line 351

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