Class: Card::Layout::CardLayout

Inherits:
Card::Layout show all
Defined in:
lib/card/layout/card_layout.rb

Overview

Layout based on a card’s content

Constant Summary collapse

MAIN_NESTING_LIMIT =
5

Instance Method Summary collapse

Methods inherited from Card::Layout

built_in_layouts, built_in_layouts_hash, card_layout?, clear_cache, code_layout?, deregister_layout, #initialize, layout_class, layout_key, layouts, #main_nest_opts, main_nest_opts, register_built_in_layout, register_layout, register_layout_with_nest, render

Constructor Details

This class inherits a constructor from Card::Layout

Instance Method Details

#fetch_main_nest_optsObject



13
14
15
16
# File 'lib/card/layout/card_layout.rb', line 13

def fetch_main_nest_opts
  find_main_nest_chunk&.options ||
    raise(Card::Error, "no main nest found in layout \"#{@layout}\"")
end

#find_main_nest_chunk(card = layout_card, depth = 0) ⇒ Object



20
21
22
23
24
25
# File 'lib/card/layout/card_layout.rb', line 20

def find_main_nest_chunk card=layout_card, depth=0
  content = Card::Content.new(card.content, @format, chunk_list: :nest_only)
  return unless content.each_chunk.count.positive?

  main_chunk(content) || go_deeper(content, depth)
end

#go_deeper(content, depth) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/card/layout/card_layout.rb', line 27

def go_deeper content, depth
  return if depth > MAIN_NESTING_LIMIT

  content.each_chunk do |chunk|
    main_chunk = find_main_nest_chunk chunk.referee_card, depth + 1
    return main_chunk if main_chunk
  end
  nil
end

#layout_cardObject



5
6
7
# File 'lib/card/layout/card_layout.rb', line 5

def layout_card
  @layout_card ||= Card.quick_fetch @layout
end

#main_chunk(content) ⇒ Object



37
38
39
# File 'lib/card/layout/card_layout.rb', line 37

def main_chunk content
  content.each_chunk.find(&:main?)
end

#renderObject



9
10
11
# File 'lib/card/layout/card_layout.rb', line 9

def render
  @format.process_content layout_card.content, chunk_list: :references
end