Class: Eskimo::ASCII::Squeeze

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



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

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



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

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