Class: Card::Layout

Inherits:
Object show all
Defined in:
card-mod-layout/lib/card/layout.rb,
card-mod-layout/lib/card/layout/card_layout.rb,
card-mod-layout/lib/card/layout/code_layout.rb,
card-mod-layout/lib/card/layout/proc_layout.rb,
card-mod-layout/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.



81
82
83
84
# File 'card-mod-layout/lib/card/layout.rb', line 81

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

Class Method Details

.built_in_layoutsObject



56
57
58
# File 'card-mod-layout/lib/card/layout.rb', line 56

def built_in_layouts
  built_in_layouts_hash.keys
end

.built_in_layouts_hashObject



52
53
54
# File 'card-mod-layout/lib/card/layout.rb', line 52

def built_in_layouts_hash
  @built_in_layouts ||= {}
end

.card_layout?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.clear_cacheObject



64
65
66
# File 'card-mod-layout/lib/card/layout.rb', line 64

def clear_cache
  @built_in_layouts = @layouts = nil
end

.code_layout?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.deregister_layout(layout_name) ⇒ Object



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

def deregister_layout layout_name
  layouts.delete layout_key(layout_name)
end

.layout_class(layout) ⇒ Object



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

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

.layout_key(name) ⇒ Object



41
42
43
44
45
# File 'card-mod-layout/lib/card/layout.rb', line 41

def layout_key name
  return name if name.is_a? Symbol

  name.to_name.key.to_sym
end

.layoutsObject



60
61
62
# File 'card-mod-layout/lib/card/layout.rb', line 60

def layouts
  @layouts ||= {}
end

.main_nest_opts(layout_name, format) ⇒ Object



68
69
70
71
72
# File 'card-mod-layout/lib/card/layout.rb', line 68

def main_nest_opts layout_name, format
  key = layout_key layout_name
  opts = layouts[key] || register_layout_with_nest(layout_name, format)
  opts.clone
end

.register_built_in_layout(new_layout, opts) ⇒ Object



47
48
49
50
# File 'card-mod-layout/lib/card/layout.rb', line 47

def register_built_in_layout new_layout, opts
  register_layout(new_layout) { opts.present? ? opts : nil }
  built_in_layouts_hash[new_layout] = true
end

.register_layout(new_layout) ⇒ Object



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

def register_layout new_layout
  key = layout_key new_layout
  return if layouts[key]

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

.register_layout_with_nest(layout_name, format) ⇒ Object



74
75
76
77
78
# File 'card-mod-layout/lib/card/layout.rb', line 74

def register_layout_with_nest layout_name, format
  register_layout(layout_name) do
    layout_class(layout_name).new(layout_name, format).fetch_main_nest_opts
  end
end

.render(layout, format) ⇒ Object



4
5
6
# File 'card-mod-layout/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



86
87
88
# File 'card-mod-layout/lib/card/layout.rb', line 86

def fetch_main_nest_opts
  {}
end

#main_nest_optsObject



90
91
92
# File 'card-mod-layout/lib/card/layout.rb', line 90

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