Module: Slotify::Extensions::Template

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#initializeObject



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

def initialize(...)
  super
  @strict_slots = STRICT_SLOTS_NONE
end

#locals_codeObject



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

def locals_code
  return super unless strict_slots?

  strict_slots_keys.each_with_object(+super) do |key, code|
    code << "partial.set_slot_default(:#{key}, binding.local_variable_get(:#{key})); #{key} = partial.public_send(:#{key});"
  end
end

#strict_locals!Object



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

def strict_locals!
  return super unless strict_slots?

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

#strict_slots!Object



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

def strict_slots!
  if @strict_slots == STRICT_SLOTS_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)


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

def strict_slots?
  strict_slots!.present?
end

#strict_slots_keysObject



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

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