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

#invalid_stubObject

def reject_illegal_stub_values! normalized_options.each do |key, value| next unless value =~ /</ raise invalid_stub + " has illegal value for #key: #value" end end



64
65
66
# File 'lib/card/view/stub.rb', line 64

def invalid_stub
  "INVALID STUB: #{card.name}/#{ok_view}"
end

#reject_foreign_options_in_stub!Object



69
70
71
72
# File 'lib/card/view/stub.rb', line 69

def reject_foreign_options_in_stub!
  return if foreign_normalized_options.empty?
  raise invalid_stub + " has foreign options: #{foreign_normalized_options}"
end

#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
# File 'lib/card/view/stub.rb', line 34

def stub_hash
  with_override mode: format.nest_mode,
                cast: card.cast,
                options: normalized_options,
                override: root?
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

#validate_stub!Object



52
53
54
55
# File 'lib/card/view/stub.rb', line 52

def validate_stub!
  reject_foreign_options_in_stub!
  #reject_illegal_stub_values!
end

#with_override(hash) ⇒ Hash

handle nest_mode in stub

Returns:

  • (Hash)


43
44
45
46
47
48
49
50
# File 'lib/card/view/stub.rb', line 43

def with_override hash

  # 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
  hash
end