Class: Slotify::Partial

Inherits:
Object
  • Object
show all
Includes:
InflectionHelper
Defined in:
lib/slotify/partial.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InflectionHelper

#plural?, #singular?, #singularize

Constructor Details

#initialize(view_context) ⇒ Partial

Returns a new instance of Partial.



7
8
9
10
11
12
# File 'lib/slotify/partial.rb', line 7

def initialize(view_context)
  @view_context = view_context
  @outer_partial = view_context.partial
  @values = []
  @strict_slots = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **options, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/slotify/partial.rb', line 67

def method_missing(name, *args, **options, &block)
  if name.start_with?("with_")
    slot_name = name.to_s.delete_prefix("with_")
    if singular?(slot_name)
      add_value(slot_name, args, options, block)
    else
      collection = args.first
      add_values(slot_name, collection, options, block)
    end
  end
end

Instance Attribute Details

#outer_partialObject (readonly)

Returns the value of attribute outer_partial.



5
6
7
# File 'lib/slotify/partial.rb', line 5

def outer_partial
  @outer_partial
end

Instance Method Details

#capture(*args, &block) ⇒ Object



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

def capture(*args, &block)
  @captured_buffer = @view_context.capture(*args, self, &block)
end

#content_for(slot_name, fallback_value = nil) ⇒ Object

Raises:



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

def content_for(slot_name, fallback_value = nil)
  raise SlotsAccessError, "slot values cannot be accessed from outside the partial" unless slots_defined?
  raise UnknownSlotError, "unknown slot :#{slot_name}" unless slot_defined?(slot_name)

  values = slot_values(slot_name)
  if values.none? && !fallback_value.nil?
    values = add_values(slot_name, Array(fallback_value))
  end

  singular?(slot_name) ? values.first : ValueCollection.new(values)
end

#content_for?(slot_name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



26
27
28
29
30
31
# File 'lib/slotify/partial.rb', line 26

def content_for?(slot_name)
  raise SlotsAccessError, "slot values cannot be accessed from outside the partial" unless slots_defined?
  raise UnknownSlotError, "unknown slot :#{slot_name}" unless slot_defined?(slot_name)

  slot_values(slot_name).any?
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/slotify/partial.rb', line 63

def respond_to_missing?(name, include_private = false)
  name.start_with?("with_")
end

#slot_localsObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/slotify/partial.rb', line 45

def slot_locals
  pairs = @strict_slots.map do |slot_name|
    values = slot_values(slot_name)
    values = singular?(slot_name) ? values&.first : values
    [slot_name, values]
  end
  pairs.filter do |key, value|
    # keep empty strings as local value but filter out empty arrays
    # and objects so they don't override any default values set via strict slots.
    value.is_a?(String) || value&.present?
  end.to_h
end

#with_strict_slots(strict_slot_names) ⇒ Object



58
59
60
61
# File 'lib/slotify/partial.rb', line 58

def with_strict_slots(strict_slot_names)
  @strict_slots = strict_slot_names.map(&:to_sym)
  validate_slots!
end

#yield(*args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/slotify/partial.rb', line 37

def yield(*args)
  if args.empty?
    @captured_buffer
  else
    content_for(args.first)
  end
end