Class: Primalize::Single::Optional
- Inherits:
-
Object
- Object
- Primalize::Single::Optional
- Includes:
- Type
- Defined in:
- lib/primalize/single.rb
Constant Summary
Constants included from Type
Instance Method Summary collapse
- #===(value) ⇒ Object
- #coerce(value) ⇒ Object
-
#initialize(types, &coercion) ⇒ Optional
constructor
A new instance of Optional.
- #inspect ⇒ Object
Constructor Details
#initialize(types, &coercion) ⇒ Optional
Returns a new instance of Optional.
369 370 371 372 |
# File 'lib/primalize/single.rb', line 369 def initialize types, &coercion @types = types @coercion = coercion end |
Instance Method Details
#===(value) ⇒ Object
374 375 376 |
# File 'lib/primalize/single.rb', line 374 def === value value.nil? || @types.any? { |type| type === value } end |
#coerce(value) ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
# File 'lib/primalize/single.rb', line 382 def coerce value if value.nil? super else type = @types.find { |type| type === value } if type.respond_to? :coerce if @coercion @coercion.call(value) else type.coerce(value) end else value end end end |
#inspect ⇒ Object
378 379 380 |
# File 'lib/primalize/single.rb', line 378 def inspect "optional(#{@types.map(&:inspect).join(', ')})" end |