Module: Slotify::Extensions::Template

Defined in:
lib/slotify/extensions/template.rb

Constant Summary collapse

STRICT_SLOTS_REGEX =
/\#\s+slots:\s+\((.*)\)/
STRICT_SLOTS_KEYS_REGEX =
/(\w+):(?=(?:[^"\\]*(?:\\.|"(?:[^"\\]*\\.)*[^"\\]*"))*[^"]*$)/

Instance Method Summary collapse

Instance Method Details

#initializeObject



9
10
11
12
# File 'lib/slotify/extensions/template.rb', line 9

def initialize(...)
  super
  @strict_slots = ActionView::Template::NONE
end

#locals_codeObject



42
43
44
45
46
47
# File 'lib/slotify/extensions/template.rb', line 42

def locals_code
  return super unless strict_slots?
  strict_slots_keys.each_with_object(+super) do |key, code|
    code << "#{key} = partial.content_for(:#{key}, binding.local_variable_get(:#{key}));"
  end
end

#strict_locals!Object



36
37
38
39
40
# File 'lib/slotify/extensions/template.rb', line 36

def strict_locals!
  return super unless strict_slots?

  [strict_slots!, super].compact.join(", ")
end

#strict_slots!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/slotify/extensions/template.rb', line 14

def strict_slots!
  if @strict_slots == ActionView::Template::NONE
    source.sub!(STRICT_SLOTS_REGEX, "")
    strict_slots = $1
    @strict_slots = if strict_slots.nil?
      ""
    else
      strict_slots.sub("**", "").strip.delete_suffix(",")
    end
  end

  @strict_slots
end

#strict_slots?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/slotify/extensions/template.rb', line 28

def strict_slots?
  strict_slots!.present?
end

#strict_slots_keysObject



32
33
34
# File 'lib/slotify/extensions/template.rb', line 32

def strict_slots_keys
  strict_slots!.scan(STRICT_SLOTS_KEYS_REGEX).map(&:first)
end