Class: Validate::AST::DefinitionContext

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

Defined Under Namespace

Modules: Builder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDefinitionContext

Returns a new instance of DefinitionContext.



52
53
54
# File 'lib/validate/ast.rb', line 52

def initialize
  @constraints = {}
end

Class Method Details

.create(*args, &block) ⇒ Object



45
46
47
48
49
50
# File 'lib/validate/ast.rb', line 45

def self.create(*args, &block)
  ast = AST.build(*args, &block)
  context = new
  ast.each { |node| context.add_constraint(Builder.send(*node)) }
  context
end

Instance Method Details

#add_constraint(constraint) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/validate/ast.rb', line 56

def add_constraint(constraint)
  if @constraints.include?(constraint.name)
    raise Error::ValidationRuleError,
          "duplicate constraint #{constraint.name}"
  end

  @constraints[constraint.name] = constraint
  self
end

#evaluate(ctx) ⇒ Object



66
67
68
69
70
71
# File 'lib/validate/ast.rb', line 66

def evaluate(ctx)
  @constraints.each_value
              .reject { |c| catch(:pending) { c.valid?(ctx.value, ctx) } }
              .each { |c| ctx.add_violation(c) }
  ctx
end