Class: Ukiryu::Validation::DependencyConstraint

Inherits:
Constraint
  • Object
show all
Defined in:
lib/ukiryu/validation/constraints.rb

Overview

Validates dependency constraints between options

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Constraint

#applies_to?

Constructor Details

#initialize(option_name, requires: nil, conflicts: nil, implies: nil) ⇒ DependencyConstraint

Returns a new instance of DependencyConstraint.

Parameters:

  • option_name (String, Symbol)

    the option name

  • requires (Array) (defaults to: nil)

    options that must be present

  • conflicts (Array) (defaults to: nil)

    options that cannot be present

  • implies (Hash) (defaults to: nil)

    options that imply certain values



190
191
192
193
194
195
# File 'lib/ukiryu/validation/constraints.rb', line 190

def initialize(option_name, requires: nil, conflicts: nil, implies: nil)
  @option_name = option_name
  @requires = requires
  @conflicts = conflicts
  @implies = implies
end

Instance Attribute Details

#conflictsObject (readonly)

Returns the value of attribute conflicts.



184
185
186
# File 'lib/ukiryu/validation/constraints.rb', line 184

def conflicts
  @conflicts
end

#impliesObject (readonly)

Returns the value of attribute implies.



184
185
186
# File 'lib/ukiryu/validation/constraints.rb', line 184

def implies
  @implies
end

#option_nameObject (readonly)

Returns the value of attribute option_name.



184
185
186
# File 'lib/ukiryu/validation/constraints.rb', line 184

def option_name
  @option_name
end

#requiresObject (readonly)

Returns the value of attribute requires.



184
185
186
# File 'lib/ukiryu/validation/constraints.rb', line 184

def requires
  @requires
end

Instance Method Details

#validate(_value, context = {}) ⇒ Object

Parameters:

  • value (Object)

    the value of the dependent option

  • context (Hash) (defaults to: {})

    must contain :options_accessor to get other values

Raises:

  • (ValidationError)

    if dependency constraints are violated



200
201
202
203
204
205
206
207
# File 'lib/ukiryu/validation/constraints.rb', line 200

def validate(_value, context = {})
  accessor = context[:options_accessor]
  raise ArgumentError, 'Dependency validation requires :options_accessor' unless accessor

  validate_requires(accessor)
  validate_conflicts(accessor)
  validate_implies(accessor)
end