Class: Card::Layout

Inherits:
Object
  • Object
show all
Defined in:
mod/standard/lib/card/layout.rb,
mod/standard/lib/card/layout/card_layout.rb,
mod/standard/lib/card/layout/code_layout.rb,
mod/standard/lib/card/layout/proc_layout.rb,
mod/standard/lib/card/layout/unknown_layout.rb

Direct Known Subclasses

CardLayout, CodeLayout, ProcLayout, UnknownLayout

Defined Under Namespace

Classes: CardLayout, CodeLayout, ProcLayout, UnknownLayout

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(layout, format) ⇒ Layout

Returns a new instance of Layout.



66
67
68
69
# File 'mod/standard/lib/card/layout.rb', line 66

def initialize layout, format
  @layout = layout
  @format = format
end

Class Method Details

.built_in_layoutsObject



45
46
47
# File 'mod/standard/lib/card/layout.rb', line 45

def built_in_layouts
  built_in_layouts_hash.keys
end

.built_in_layouts_hashObject



41
42
43
# File 'mod/standard/lib/card/layout.rb', line 41

def built_in_layouts_hash
  @built_in_layouts ||= {}
end

.card_layout?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def card_layout? name
  Card.fetch_type_id(name).in? [Card::LayoutTypeID, Card::HtmlID, Card::BasicID]
rescue ArgumentError, Card::Error::CodenameNotFound => _e
  false
end

.clear_cacheObject



53
54
55
# File 'mod/standard/lib/card/layout.rb', line 53

def clear_cache
  @built_in_layouts = @layouts = nil
end

.code_layout?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'mod/standard/lib/card/layout.rb', line 26

def code_layout? name
  built_in_layouts_hash.key? name.to_sym
end

.layout_class(layout) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'mod/standard/lib/card/layout.rb', line 8

def layout_class layout
  if layout.respond_to?(:call)
    Card::Layout::ProcLayout
  elsif card_layout?(layout)
    Card::Layout::CardLayout
  elsif code_layout?(layout)
    Card::Layout::CodeLayout
  else
    Card::Layout::UnknownLayout
  end
end

.layoutsObject



49
50
51
# File 'mod/standard/lib/card/layout.rb', line 49

def layouts
  @layouts ||= {}
end

.main_nest_opts(layout_name, format) ⇒ Object



57
58
59
60
61
62
63
# File 'mod/standard/lib/card/layout.rb', line 57

def main_nest_opts layout_name, format
  opts = layouts[layout_name] ||
         register_layout(layout_name) do
           layout_class(layout_name).new(layout_name, format).fetch_main_nest_opts
         end
  opts.clone
end

.register_built_in_layout(new_layout) ⇒ Object



36
37
38
39
# File 'mod/standard/lib/card/layout.rb', line 36

def register_built_in_layout new_layout
  register_layout new_layout
  built_in_layouts_hash[new_layout] = true
end

.register_layout(new_layout) ⇒ Object



30
31
32
33
34
# File 'mod/standard/lib/card/layout.rb', line 30

def register_layout new_layout
  return if layouts[new_layout]

  layouts[new_layout] = block_given? ? yield : {}
end

.render(layout, format) ⇒ Object



4
5
6
# File 'mod/standard/lib/card/layout.rb', line 4

def render layout, format
  layout_class(layout).new(layout, format).render
end

Instance Method Details

#fetch_main_nest_optsObject



71
72
73
# File 'mod/standard/lib/card/layout.rb', line 71

def fetch_main_nest_opts
  {}
end

#main_nest_optsObject



75
76
77
# File 'mod/standard/lib/card/layout.rb', line 75

def main_nest_opts
  self.class.main_nest_opts @layout, @format
end