Class: Validate::Constraint

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

Defined Under Namespace

Modules: DSL Classes: Option, Violation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Constraint

Returns a new instance of Constraint.



187
188
189
# File 'lib/validate/constraint.rb', line 187

def initialize(**options)
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



215
216
217
218
219
# File 'lib/validate/constraint.rb', line 215

def method_missing(method, *args)
  return super unless args.empty? || respond_to_missing?(method)

  @options[method]
end

Class Method Details

.create_class(name, **defaults, &constraint_block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



50
51
52
# File 'lib/validate/constraint.rb', line 50

def self.inherited(child)
  child.extend DSL
end

Instance Method Details

#==(other) ⇒ Object



207
208
209
# File 'lib/validate/constraint.rb', line 207

def ==(other)
  other.is_a?(Constraint) && other.name == name && other.options == options
end

#inspectObject



203
204
205
# File 'lib/validate/constraint.rb', line 203

def inspect
  "#<#{self.class.name} #{@options.map { |name, value| "#{name}: #{value.inspect}" }.join(', ')}>"
end

#nameObject

Raises:

  • (::NotImplementedError)


191
192
193
# File 'lib/validate/constraint.rb', line 191

def name
  raise ::NotImplementedError
end

#respond_to_missing?(method, _ = false) ⇒ Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/validate/constraint.rb', line 211

def respond_to_missing?(method, _ = false)
  @options.include?(method)
end

#to_sObject



199
200
201
# File 'lib/validate/constraint.rb', line 199

def to_s
  name.to_s.gsub('_', ' ')
end

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

Returns:

  • (Boolean)

Raises:

  • (::NotImplementedError)


195
196
197
# File 'lib/validate/constraint.rb', line 195

def valid?(value, ctx = Constraints::ValidationContext.none)
  raise ::NotImplementedError
end