Module: BulmaX::Dsl::Slots::ClassMethods

Defined in:
lib/bulma_x/dsl/slots.rb

Instance Method Summary collapse

Instance Method Details

#define_attributes_helper(slot_name, attributes) ⇒ Object



116
# File 'lib/bulma_x/dsl/slots.rb', line 116

def define_attributes_helper(slot_name, attributes) = define_method(:"#{slot_name}_attributes") { attributes }

#define_classes_helper(slot_name, classes) ⇒ Object



115
# File 'lib/bulma_x/dsl/slots.rb', line 115

def define_classes_helper(slot_name, classes) = define_method(:"#{slot_name}_classes") { classes }

#define_tag_helper(slot_name, tag) ⇒ Object



114
# File 'lib/bulma_x/dsl/slots.rb', line 114

def define_tag_helper(slot_name, tag) = define_method(:"#{slot_name}_tag") { tag }

#root_slot(**kwargs) ⇒ Object

root_slot allows to redefine the root tag, classes and attributes in one go



103
104
105
106
107
108
109
110
111
112
# File 'lib/bulma_x/dsl/slots.rb', line 103

def root_slot(**kwargs)
  tag = kwargs[:tag] || :div
  define_method(:root_tag) { tag }

  classes = kwargs[:classes] || []
  define_method(:root_classes) { base_classes + classes }

  attributes = kwargs[:attributes] || {}
  define_method(:root_attributes) { base_attributes.merge(attributes) }
end

#slot(slot_name, tag: :div, classes: [], attributes: {}, component: Slot) ⇒ Object

slot defines a slot that will be accessible on the instance via slot rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bulma_x/dsl/slots.rb', line 29

def slot(slot_name, tag: :div, classes: [], attributes: {}, component: Slot)
  include DeferredRender

  if component != Slot && (tag != :div || classes.any? || attributes.any?)
    raise ArgumentError, 'When using a custom component, tag, classes and attributes are ignored'
  end

  define_tag_helper(slot_name, tag)
  define_classes_helper(slot_name, classes)
  define_attributes_helper(slot_name, attributes)

  # TODO: Replace method usage by class variable for validation
  # @enabled_slots ||= {}
  # @enabled_slots[slot_name] = { collection: false, ...}

  define_method(:"with_#{slot_name}") do |**options, &block|
    @slots[slot_name] = { component:, options:, block: }

    nil
  end
  private :"with_#{slot_name}"

  define_method(:"#{slot_name}_slot_component") do
    if component.is_a?(Class)
      component
    elsif component.is_a?(String)
      if component == 'self'
        self.class
      else
        self.class.const_get(component)
      end
    end
  end
end

#slots(slot_name, tag: :div, classes: [], attributes: {}, component: Slot) ⇒ Object

rubocop:disable Metrics



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/bulma_x/dsl/slots.rb', line 68

def slots(slot_name, tag: :div, classes: [], attributes: {}, component: Slot)
  include DeferredRender

  if component != Slot && (tag != :div || classes.any? || attributes.any?)
    raise ArgumentError, 'When using a custom component, tag, classes and attributes are ignored'
  end

  define_tag_helper(slot_name, tag)
  define_classes_helper(slot_name, classes)
  define_attributes_helper(slot_name, attributes)

  define_method(:"with_#{slot_name}") do |**options, &block|
    @slots[slot_name] ||= []

    @slots[slot_name] << { component:, options:, block: }

    nil
  end
  private :"with_#{slot_name}"

  define_method(:"#{slot_name}_slot_component") do
    if component.is_a?(Class)
      component
    elsif component.is_a?(String)
      if component == 'self'
        self.class
      else
        self.class.const_get(component)
      end
    end
  end
end