class Validate::AST::Generator

Public Class Methods

new() click to toggle source
# File lib/validate/ast.rb, line 83
def initialize
  @stack = []
end

Public Instance Methods

!() click to toggle source
# File lib/validate/ast.rb, line 135
def !
  prev = @stack.pop
  if prev[0] == :no_constraints
    @stack << prev[1]
  elsif prev[0] == :all_constraints
    prev[0] = :no_constraints
    @stack << prev
  else
    @stack << [:no_constraints, prev]
  end
  self
end
&(other) click to toggle source
# File lib/validate/ast.rb, line 97
def &(other)
  unless other == self
    ::Kernel.raise(
      Error::ValidationRuleError,
      'bad rule, only constraints and &, |, and ! operators allowed'
    )
  end

  right = @stack.pop
  left = @stack.pop
  if right[0] == :all_constraints
    right.insert(1, left)
    @stack << right
  else
    @stack << [:all_constraints, left, right]
  end
  self
end
generate(*args, &block) click to toggle source
# File lib/validate/ast.rb, line 87
def generate(*args, &block)
  instance_exec(*args, &block)

  if @stack.one? && @stack.first[0] == :all_constraints
    return @stack.first[1..-1]
  end

  @stack
end
|(other) click to toggle source
# File lib/validate/ast.rb, line 116
def |(other)
  unless other == self
    ::Kernel.raise(
      Error::ValidationRuleError,
      'bad rule, only constraints and &, |, and ! operators allowed'
    )
  end

  right = @stack.pop
  left = @stack.pop
  if right[0] == :at_least_one_constraint
    right.insert(1, left)
    @stack << right
  else
    @stack << [:at_least_one_constraint, left, right]
  end
  self
end

Private Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/validate/ast.rb, line 150
def method_missing(method, *args, &block)
  return super unless respond_to_missing?(method)

  @stack << [
      :constraint,
      method,
      args.map { |arg| [:value, arg] },
      block,
      ::Kernel.caller
              .reject { |line| line.include?(__FILE__) }
  ]
  self
end
respond_to_missing?(method, _ = false) click to toggle source
# File lib/validate/ast.rb, line 164
def respond_to_missing?(method, _ = false)
  (defined?(Constraints) && Constraints.respond_to?(method)) || CORE_CONSTRAINTS.include?(method)
end