Class: Exclaim::Implementations::Each

Inherits:
Object
  • Object
show all
Defined in:
lib/exclaim/implementations/each_component.rb

Instance Method Summary collapse

Instance Method Details

#call(config, env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/exclaim/implementations/each_component.rb', line 10

def call(config, env)
  items = (config['items'] || config['$each']).to_a
  bind_reference = config['yield']

  # This implementation mutates the env Hash before passing it to each child element,
  # and then restores the env to its original state at the end.
  original_env_includes_bind_reference = env.key?(bind_reference)
  original_bind_value = env[bind_reference] if original_env_includes_bind_reference
  resolved_items = items.map do |item|
    env[bind_reference] = item
    yield config['do'], env # yields to render_child block
  end

  resolved_items.map { |line| line.end_with?("\n") ? line : "#{line}\n" }.join
ensure
  env.delete(bind_reference)
  env[bind_reference] = original_bind_value if original_env_includes_bind_reference
end

#component?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/exclaim/implementations/each_component.rb', line 6

def component?
  true
end