Class: Tailmix::HTML::ClassList

Inherits:
Set
  • Object
show all
Defined in:
lib/tailmix/html/class_list.rb

Overview

Manages a set of CSS classes with a fluent, chainable API. Inherits from Set to ensure uniqueness and leverage its performance.

Instance Method Summary collapse

Constructor Details

#initialize(initial_classes = nil) ⇒ ClassList

Initializes a new ClassList.

Parameters:

  • initial_classes (String, Array, Set, nil) (defaults to: nil)

    The initial classes to add.



12
13
14
15
# File 'lib/tailmix/html/class_list.rb', line 12

def initialize(initial_classes = nil)
  super()
  add(initial_classes) if initial_classes
end

Instance Method Details

#add(class_names) ⇒ self Also known as: <<

Adds one or more classes. Handles strings, arrays, or other sets. This method is MUTABLE and chainable.

Parameters:

  • class_names (String, Array, Set, nil)

Returns:

  • (self)


21
22
23
24
# File 'lib/tailmix/html/class_list.rb', line 21

def add(class_names)
  each_token(class_names) { |token| super(token) }
  self
end

#added(class_names) ⇒ Object

Returns a new ClassList with the given classes added. IMMUTABLE.



46
47
48
# File 'lib/tailmix/html/class_list.rb', line 46

def added(class_names)
  dup.add(class_names)
end

#remove(class_names) ⇒ self

Removes one or more classes. This method is MUTABLE and chainable.

Parameters:

  • class_names (String, Array, Set, nil)

Returns:

  • (self)


31
32
33
34
# File 'lib/tailmix/html/class_list.rb', line 31

def remove(class_names)
  each_token(class_names) { |token| delete(token) }
  self
end

#removed(class_names) ⇒ Object

Returns a new ClassList with the given classes removed. IMMUTABLE.



51
52
53
# File 'lib/tailmix/html/class_list.rb', line 51

def removed(class_names)
  dup.remove(class_names)
end

#to_sString

Renders the set of classes to a space-separated string for HTML.

Returns:

  • (String)


62
63
64
# File 'lib/tailmix/html/class_list.rb', line 62

def to_s
  to_a.join(" ")
end

#toggle(class_names) ⇒ self

Toggles one or more classes. This method is MUTABLE and chainable.

Parameters:

  • class_names (String, Array, Set, nil)

Returns:

  • (self)


40
41
42
43
# File 'lib/tailmix/html/class_list.rb', line 40

def toggle(class_names)
  each_token(class_names) { |token| include?(token) ? delete(token) : add(token) }
  self
end

#toggled(class_names) ⇒ Object

Returns a new ClassList with the given classes toggled. IMMUTABLE.



56
57
58
# File 'lib/tailmix/html/class_list.rb', line 56

def toggled(class_names)
  dup.toggle(class_names)
end