Class: Tailmix::HTML::ClassList
- Inherits:
-
Set
- Object
- Set
- Tailmix::HTML::ClassList
- 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
-
#add(class_names) ⇒ self
(also: #<<)
Adds one or more classes.
-
#added(class_names) ⇒ Object
Returns a new ClassList with the given classes added.
-
#initialize(initial_classes = nil) ⇒ ClassList
constructor
Initializes a new ClassList.
-
#remove(class_names) ⇒ self
Removes one or more classes.
-
#removed(class_names) ⇒ Object
Returns a new ClassList with the given classes removed.
-
#to_s ⇒ String
Renders the set of classes to a space-separated string for HTML.
-
#toggle(class_names) ⇒ self
Toggles one or more classes.
-
#toggled(class_names) ⇒ Object
Returns a new ClassList with the given classes toggled.
Constructor Details
#initialize(initial_classes = nil) ⇒ ClassList
Initializes a new ClassList.
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.
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.
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_s ⇒ String
Renders the set of classes to a space-separated string for HTML.
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.
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 |