Class: RiceBubble::Attributes::Optional

Inherits:
Base
  • Object
show all
Defined in:
lib/rice_bubble/attributes/optional.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#fetch, #optional, #valid_types

Constructor Details

#initialize(child) ⇒ Optional

Returns a new instance of Optional.



6
7
8
9
# File 'lib/rice_bubble/attributes/optional.rb', line 6

def initialize(child, &)
  super(&)
  @child = child
end

Instance Attribute Details

#childObject (readonly)

Returns the value of attribute child.



4
5
6
# File 'lib/rice_bubble/attributes/optional.rb', line 4

def child
  @child
end

Instance Method Details

#call(value, path: '') ⇒ Object



15
16
17
# File 'lib/rice_bubble/attributes/optional.rb', line 15

def call(value, path: '')
  value && child.call(value, path:)
end

#descriptionObject



19
20
21
# File 'lib/rice_bubble/attributes/optional.rb', line 19

def description
  "#{child.description} (optional)"
end

#valid?(value) ⇒ Boolean

Returns:



11
12
13
# File 'lib/rice_bubble/attributes/optional.rb', line 11

def valid?(value)
  value.nil? || child.valid?(value)
end