Class: Slotify::Partial
Constant Summary
collapse
- RESERVED_SLOT_NAMES =
[
:content, :slot, :value, :content_for,
:capture, :yield, :partial
]
Instance Attribute Summary collapse
Instance Method Summary
collapse
#plural?, #singular?, #singularize
Constructor Details
#initialize(view_context) ⇒ Partial
Returns a new instance of Partial.
12
13
14
15
16
17
|
# File 'lib/slotify/partial.rb', line 12
def initialize(view_context)
@view_context = view_context
@outer_partial = view_context.partial
@values = ValueStore.new(@view_context)
@defined_slots = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **options, &block) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/slotify/partial.rb', line 82
def method_missing(name, *args, **options, &block)
if name.start_with?("with_")
values.add(name.to_s.delete_prefix("with_"), args, options, block)
elsif slot?(name)
content_for(name)
else
super
end
end
|
Instance Attribute Details
#outer_partial ⇒ Object
Returns the value of attribute outer_partial.
10
11
12
|
# File 'lib/slotify/partial.rb', line 10
def outer_partial
@outer_partial
end
|
Instance Method Details
#capture(*args, &block) ⇒ Object
32
33
34
|
# File 'lib/slotify/partial.rb', line 32
def capture(*args, &block)
@captured_buffer = @view_context.capture(*args, self, &block)
end
|
#content ⇒ Object
40
41
42
|
# File 'lib/slotify/partial.rb', line 40
def content
self.yield
end
|
#content_for(slot_name) ⇒ Object
19
20
21
22
23
24
|
# File 'lib/slotify/partial.rb', line 19
def content_for(slot_name)
raise UnknownSlotError, "unknown slot :#{slot_name}" unless slot?(slot_name)
slot_values = values.for(slot_name)
singular?(slot_name) ? slot_values.first : ValueCollection.new(slot_values)
end
|
#content_for?(slot_name) ⇒ Boolean
26
27
28
29
30
|
# File 'lib/slotify/partial.rb', line 26
def content_for?(slot_name)
raise UnknownSlotError, "unknown slot :#{slot_name}" unless slot?(slot_name)
values.for(slot_name).any?
end
|
#define_slots!(slot_names) ⇒ Object
68
69
70
71
72
73
74
75
76
|
# File 'lib/slotify/partial.rb', line 68
def define_slots!(slot_names)
raise SlotsDefinedError, "Slots cannot be redefined" unless @defined_slots.nil?
@defined_slots = slot_names.map(&:to_sym).each do |slot_name|
if RESERVED_SLOT_NAMES.include?(singularize(slot_name))
raise ReservedSlotNameError, ":#{slot_name} is a reserved word and cannot be used as a slot name"
end
end
end
|
#respond_to_missing?(name, include_private = false) ⇒ Boolean
78
79
80
|
# File 'lib/slotify/partial.rb', line 78
def respond_to_missing?(name, include_private = false)
name.start_with?("with_") || slot?(name)
end
|
#set_slot_default(slot_name, default_value) ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/slotify/partial.rb', line 44
def set_slot_default(slot_name, default_value)
raise UnknownSlotError, "unknown slot :#{slot_name}" unless slot?(slot_name)
if values.for(slot_name).none? && !default_value.nil?
values.add(slot_name, Array.wrap(default_value))
end
end
|
#slot_locals ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/slotify/partial.rb', line 52
def slot_locals
validate_slots!
pairs = @defined_slots.map do |slot_name|
slot_values = values.for(slot_name)
slot_values = singular?(slot_name) ? slot_values&.first : slot_values
[slot_name, slot_values]
end
pairs.filter do |key, value|
value.is_a?(String) || value&.present?
end.to_h
end
|
#yield(*args) ⇒ Object
36
37
38
|
# File 'lib/slotify/partial.rb', line 36
def yield(*args)
args.empty? ? @captured_buffer : content_for(args.first)
end
|