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.



80
81
82
83
# File 'mod/standard/lib/card/layout.rb', line 80

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

Class Method Details

.built_in_layoutsObject



55
56
57
# File 'mod/standard/lib/card/layout.rb', line 55

def built_in_layouts
  built_in_layouts_hash.keys
end

.built_in_layouts_hashObject



51
52
53
# File 'mod/standard/lib/card/layout.rb', line 51

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



63
64
65
# File 'mod/standard/lib/card/layout.rb', line 63

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

.deregister_layout(layout_name) ⇒ Object



37
38
39
# File 'mod/standard/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 '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

.layout_key(name) ⇒ Object



41
42
43
44
# File 'mod/standard/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



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

def layouts
  @layouts ||= {}
end

.main_nest_opts(layout_name, format) ⇒ Object



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

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



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

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 'mod/standard/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



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

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 '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



85
86
87
# File 'mod/standard/lib/card/layout.rb', line 85

def fetch_main_nest_opts
  {}
end

#main_nest_optsObject



89
90
91
# File 'mod/standard/lib/card/layout.rb', line 89

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