Class: Eskimo::Components::Squeeze

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

Overview

Squeeze immediate consecutive soft breaks.

Squeeze.new do
  [
    SoftBreak.new,
    ConditionalComponent.new,

    SoftBreak.new,
    false && SomeOtherComponent.new,

    SoftBreak.new,
    'hello',
  ]
 end
 # => ""
 #    "hello"

The soft breaks for each conditional component will be preserved only if they do render some content.

Instance Method Summary collapse

Constructor Details

#initialize(children) ⇒ Squeeze

Returns a new instance of Squeeze



25
26
27
28
29
30
31
# File 'lib/eskimo/components/squeeze.rb', line 25

def initialize(children)
  if !children.is_a?(Array) || block_given?
    raise ArgumentError.new("Squeeze works only with an Array of components")
  end

  @children = children
end

Instance Method Details

#render(**props) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/eskimo/components/squeeze.rb', line 33

def render(**props)
  rendered = @children.map(&props[:render])

  without_blanks = rendered.reject(&:empty?)
  without_blanks.reject.with_index do |element, index|
    element == "\n" && without_blanks[index+1] == "\n"
  end
end