Module: Card::Format::Render

Included in:
Card::Format
Defined in:
lib/card/format/render.rb

Overview

View rendering methods.

Instance Method Summary collapse

Instance Method Details

#add_debug_info(view, method, rendered) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/card/format/render.rb', line 47

def add_debug_info view, method, rendered
  return rendered unless show_debug_info?
  <<-HTML
    <view-debug view='#{safe_name}##{view}' src='#{pretty_path method.source_location}' module='#{method.owner}'/>
    #{rendered}
  HTML
end

#before_view(view) ⇒ Object



25
26
27
# File 'lib/card/format/render.rb', line 25

def before_view view
  try "_before_#{view}"
end

#current_view(view) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/card/format/render.rb', line 127

def current_view view
  old_view = @current_view
  @current_view = view
  yield
ensure
  @current_view = old_view
end

#expand_stubs(cached_content) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/card/format/render.rb', line 95

def expand_stubs cached_content
  return cached_content unless cached_content.is_a? String

  conto = Card::Content.new cached_content, self, chunk_list: :stub
  conto.process_each_chunk do |stub_hash|
    yield(stub_hash)
  end

  if conto.pieces.size == 1
    # for stubs in json format this converts a single stub back
    # to it's original type (e.g. a hash)
    conto.pieces.first.to_s
  else
    conto.to_s
  end
end

#final_render(view) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/card/format/render.rb', line 39

def final_render view
  current_view(view) do
    method = view_method view
    rendered = method.call
    add_debug_info view, method, rendered
  end
end

#prepare_stub_nest(stub_hash) {|stub_card, view_opts| ... } ⇒ Object

Yields:

  • (stub_card, view_opts)


86
87
88
89
90
91
92
93
# File 'lib/card/format/render.rb', line 86

def prepare_stub_nest stub_hash
  stub_card = Card.fetch_from_cast stub_hash[:cast]
  view_opts = stub_hash[:view_opts]
  if stub_card&.key.present? && stub_card.key == card.key
    view_opts[:nest_name] ||= "_self"
  end
  yield stub_card, view_opts
end

#pretty_path(source_location) ⇒ Object



59
60
61
62
# File 'lib/card/format/render.rb', line 59

def pretty_path source_location
  source_location.first.gsub(%r{^.+mod\d+-([^/]+)}, '\1: ') + ':' +
    source_location.second.to_s
end

#render!(view, args = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/card/format/render.rb', line 6

def render! view, args={}
  voo = View.new self, view, args, @voo
  with_voo voo do
    voo.process do |final_view|
      final_render final_view
    end
  end
rescue => e
  rescue_view e, view
end

#show_debug_info?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/card/format/render.rb', line 55

def show_debug_info?
  Env.params[:debug] == "view"
end

#show_view?(view, default_viz = :show) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
# File 'lib/card/format/render.rb', line 33

def show_view? view, default_viz=:show
  voo.process_visibility_options # trigger viz processing
  visibility = voo.viz_hash[view] || default_viz
  visibility == :show
end

#stub_render(cached_content) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/card/format/render.rb', line 73

def stub_render cached_content
  result = expand_stubs cached_content do |stub_hash|
    prepare_stub_nest(stub_hash) do |stub_card, view_opts|
      nest stub_card, view_opts, stub_hash[:format_opts]
    end
  end
  if result =~ /stub/
    Rails.logger.info "STUB IN RENDERED VIEW: #{card.name}: " \
                      "#{voo.ok_view}\n#{result}"
  end
  result
end

#supports_view?(view) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/card/format/render.rb', line 119

def supports_view? view
  respond_to? view_method_name(view)
end

#view_cache_setting(view) ⇒ Object

setting (:alway, :never, :nested) designated in view definition



65
66
67
68
69
70
71
# File 'lib/card/format/render.rb', line 65

def view_cache_setting view
  method = self.class.view_cache_setting_method view
  coded_setting = respond_to?(method) ? send(method) : :standard
  return :never if coded_setting == :never
  # seems unwise to override a hard-coded "never"
  (voo && voo.cache) || coded_setting
end

#view_method(view) ⇒ Object



112
113
114
115
116
117
# File 'lib/card/format/render.rb', line 112

def view_method view
  unless supports_view? view
    raise Card::Error::UserError, unsupported_view_error_message(view)
  end
  method view_method_name(view)
end

#view_method_name(view) ⇒ Object



123
124
125
# File 'lib/card/format/render.rb', line 123

def view_method_name view
  "_view_#{view}"
end

#vooObject



29
30
31
# File 'lib/card/format/render.rb', line 29

def voo
  @voo
end

#with_voo(voo) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/card/format/render.rb', line 17

def with_voo voo
  old_voo = @voo
  @voo = voo
  yield
ensure
  @voo = old_voo
end