Module: Card::View::Stub

Included in:
Card::View
Defined in:
lib/card/view/stub.rb

Overview

A "stub" is a placeholder for a card view.

Cached views use stubs so that nesting content can remained cached even while nested content changes. The nested content's place is held by a stub.

A stub must contain all the information necessary to produce the view as intended.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape(stub_json) ⇒ Object



14
15
16
# File 'lib/card/view/stub.rb', line 14

def escape stub_json
  stub_json.gsub "(", "_OParEN_"
end

.unescape(stub_json) ⇒ Object



18
19
20
# File 'lib/card/view/stub.rb', line 18

def unescape stub_json
  stub_json.gsub "_OParEN_", "("
end

Instance Method Details

#stubString

Returns:

  • (String)


24
25
26
# File 'lib/card/view/stub.rb', line 24

def stub
  "(stub)#{Stub.escape stub_json}(/stub)".html_safe
end

#stub_hashHash

Returns:

  • (Hash)


34
35
36
37
38
39
40
41
42
43
# File 'lib/card/view/stub.rb', line 34

def stub_hash
  { cast: card.cast,
    view_opts: normalized_options,
    format_opts: { nest_mode: format.nest_mode, override: root? } }
  # nest mode handling:
  # typically modes only override views on nests, but stubs create non-standard nests
  # mode-based view overrides should NOT apply to standard render calls that have
  # been replaced with stubs - only to standard nest calls. therefore modes are not
  # stored in non-standard nest stubs
end

#stub_jsonString

Returns the stub_hash as JSON.

Returns:

  • (String)

    the stub_hash as JSON



29
30
31
# File 'lib/card/view/stub.rb', line 29

def stub_json
  JSON.generate stub_hash
end