class Validate::Constraint

Attributes

options[R]

Public Class Methods

create_class(name, **defaults, &constraint_block) click to toggle source
# File lib/validate/constraint.rb, line 54
def self.create_class(name, **defaults, &constraint_block)
  Class.new(self) do
    @supported_options = common_options.transform_values do |option|
      defaults.include?(option.name) ? option.replace_default(defaults[option.name]) : option
    end
    include(@constraint_user_methods = Module.new)
    @constraint_user_methods.define_method(:name) { name.to_s }
    class_eval(&constraint_block)
    if instance_variable_defined?(:@supported_options)
      initialize { |**options| options }
    end
  end
end
inherited(child) click to toggle source
# File lib/validate/constraint.rb, line 50
def self.inherited(child)
  child.extend DSL
end
new(**options) click to toggle source
# File lib/validate/constraint.rb, line 187
def initialize(**options)
  @options = options
end

Public Instance Methods

==(other) click to toggle source
# File lib/validate/constraint.rb, line 207
def ==(other)
  other.is_a?(Constraint) && other.name == name && other.options == options
end
inspect() click to toggle source
# File lib/validate/constraint.rb, line 203
def inspect
  "#<#{self.class.name} #{@options.map { |name, value| "#{name}: #{value.inspect}" }.join(', ')}>"
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/validate/constraint.rb, line 215
def method_missing(method, *args)
  return super unless args.empty? || respond_to_missing?(method)

  @options[method]
end
name() click to toggle source
# File lib/validate/constraint.rb, line 191
def name
  raise ::NotImplementedError
end
respond_to_missing?(method, _ = false) click to toggle source
# File lib/validate/constraint.rb, line 211
def respond_to_missing?(method, _ = false)
  @options.include?(method)
end
to_s() click to toggle source
# File lib/validate/constraint.rb, line 199
def to_s
  name.to_s.gsub('_', ' ')
end
valid?(value, ctx = Constraints::ValidationContext.none) click to toggle source
# File lib/validate/constraint.rb, line 195
def valid?(value, ctx = Constraints::ValidationContext.none)
  raise ::NotImplementedError
end

Private Instance Methods

fail() click to toggle source
# File lib/validate/constraint.rb, line 223
def fail
  throw(:result, :fail)
end
pass() click to toggle source
# File lib/validate/constraint.rb, line 227
def pass
  throw(:result, :pass)
end