Class: Eskimo::ASCII::Either

Inherits:
Object
  • Object
show all
Defined in:
gems/eskimo-ascii/lib/eskimo/ascii/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



16
17
18
# File 'gems/eskimo-ascii/lib/eskimo/ascii/components/either.rb', line 16

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children



14
15
16
# File 'gems/eskimo-ascii/lib/eskimo/ascii/components/either.rb', line 14

def children
  @children
end

Instance Method Details

#render(render:) ⇒ Object



20
21
22
23
24
25
26
# File 'gems/eskimo-ascii/lib/eskimo/ascii/components/either.rb', line 20

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

    return rendered unless rendered.empty?
  end
end