Class: Validate::AST::Rules::Unanimous

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

Returns a new instance of Unanimous.



266
267
268
# File 'lib/validate/ast.rb', line 266

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



281
282
283
284
285
# File 'lib/validate/ast.rb', line 281

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

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

#messageObject



287
288
289
290
291
292
293
# File 'lib/validate/ast.rb', line 287

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

#nameObject



277
278
279
# File 'lib/validate/ast.rb', line 277

def name
  'both_' + @constraints.map(&:name).sort.join('_and_')
end

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

Returns:

  • (Boolean)


270
271
272
273
274
275
# File 'lib/validate/ast.rb', line 270

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