Class: Validate::Constraint::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default:, assertion: nil, &assert_block) ⇒ Option

Returns a new instance of Option.



33
34
35
36
37
# File 'lib/validate/constraint.rb', line 33

def initialize(name, default:, assertion: nil, &assert_block)
  @name = name
  @default = default.is_a?(Proc) ? default : -> { default }
  @assertion = assertion || assert_block && Assertions.create(&assert_block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/validate/constraint.rb', line 31

def name
  @name
end

Instance Method Details

#get_or_default(options) ⇒ Object



43
44
45
46
47
# File 'lib/validate/constraint.rb', line 43

def get_or_default(options)
  value = options.delete(@name) { return @default&.call }
  @assertion&.assert(value, message: "invalid option #{@name}")
  value
end

#replace_default(default) ⇒ Object



39
40
41
# File 'lib/validate/constraint.rb', line 39

def replace_default(default)
  Option.new(@name, default: default, assertion: @assertion)
end