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

#api_render(match, opts) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/card/format/render.rb', line 82

def api_render match, opts
  view = match[3] ? match[4] : opts.shift
  args = opts[0] ? opts.shift.clone : {}
  optional_render_args(args, opts) if match[2]
  args[:skip_perms] = true if match[1]
  render view, args
end

#current_view(view) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/card/format/render.rb', line 101

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

#expand_stubs(cached_content) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/card/format/render.rb', line 74

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

#final_render(view, args) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/card/format/render.rb', line 41

def final_render view, args
  current_view(view) do
    with_nest_mode view do
      method = view_method view
      method.arity.zero? ? method.call : method.call(args)
    end
  end
end

#optional_render_args(args, opts) ⇒ Object



90
91
92
# File 'lib/card/format/render.rb', line 90

def optional_render_args args, opts
  args[:optional] = opts.shift || :show
end

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

Yields:

  • (stub_card, , stub_options)


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

def prepare_stub_nest stub_hash
  stub_card = Card.fetch_from_cast stub_hash[:cast]
  stub_options = stub_hash[:options]
  if stub_card.key.present? && stub_card.key == card.key
    stub_options[:nest_name] ||= "_self"
  end
  yield stub_card, stub_hash[:mode], stub_options
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, options|
      final_render final_view, options
    end
  end
rescue => e
  rescue_view e, view
end

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

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/card/format/render.rb', line 35

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



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

def stub_render cached_content
  return cached_content unless cached_content.is_a? String
  expand_stubs cached_content do |stub_hash|
    prepare_stub_nest(stub_hash) do |stub_card, mode, options|
      with_nest_mode(mode) { nest stub_card, options }
    end
  end
end

#view_cache_setting(view) ⇒ Object

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



51
52
53
54
# File 'lib/card/format/render.rb', line 51

def view_cache_setting view
  setting_method = self.class.view_cache_setting_method view
  respond_to?(setting_method) ? send(setting_method) : :standard
end

#view_method(view) ⇒ Object



94
95
96
97
98
99
# File 'lib/card/format/render.rb', line 94

def view_method view
  method "_view_#{view}"
rescue
  voo.unsupported_view = view
  method "_view_unsupported_view"
end

#view_options_with_defaults(view, options) ⇒ Object



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

def view_options_with_defaults view, options
  default_method = "default_#{view}_args"
  send default_method, options if respond_to? default_method
  options
end

#vooObject



31
32
33
# File 'lib/card/format/render.rb', line 31

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
  result = yield
  @voo = old_voo
  result
end