Class: Validate::Constraint::Option
- Inherits:
-
Object
- Object
- Validate::Constraint::Option
- Defined in:
- lib/validate/constraint.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #get_or_default(options) ⇒ Object
-
#initialize(name, default:, assertion: nil, &assert_block) ⇒ Option
constructor
A new instance of Option.
- #replace_default(default) ⇒ Object
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
#name ⇒ Object (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() value = .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 |