Class: Eskimo::Components::Either

Inherits:
Object
  • Object
show all
Defined in:
lib/eskimo/components/either.rb

Overview

Render a fallback component in case the first one evaluates to an empty String.

Either.new(->(**) { false }, "Fallback here")
# => "Fallback here"

Either.new(->(**) { "Hi!" }, "Fallback here")
# => "Hi!"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(primary, fallback) ⇒ Either

Returns a new instance of Either



17
18
19
# File 'lib/eskimo/components/either.rb', line 17

def initialize(primary, fallback)
  @children = [ primary, fallback ]
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children



15
16
17
# File 'lib/eskimo/components/either.rb', line 15

def children
  @children
end

Instance Method Details

#render(render:) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/eskimo/components/either.rb', line 21

def render(render:, **)
  for child in children do
    rendered = render[child]

    return rendered unless rendered.empty?
  end
end