Class: WrapIt::HTMLClass
- Inherits:
-
Array
- Object
- Array
- WrapIt::HTMLClass
- Defined in:
- lib/wrap_it/html_class.rb
Overview
Provides array-like access to HTML classes.
This class delegate allmost all methods to underlying array with some value checking and modification. Also it restrict a list of methods, exposed below becouse call to theese methods unusefull in context of HTML class list.
Some methods, thats described in this document have different manner. See each method description for details.
All other methods can be used as with standard array
Restricted methods: assoc, bsearch, combination, compact, compact!, fill, flatten, flatten!, insert, pack, permutation, product, rassoc, repeated_combination, rotate, repeated_permutation, reverse reverse!, reverse_each, sample, rotate!, shuffle, shuffle!, sort, sort!, sort_by!, transpose, uniq, uniq!, zip, flat_map, max, max_by, min, min_by, minmax, minmax_by
Class Method Summary collapse
-
.sanitize(*values) ⇒ Array<String>
Sanitizes and normalizes HTML class.
Instance Method Summary collapse
-
#delete([cond, ...], &block) ⇒ self
Removes elements from list by some conditions.
-
#include?([cond, ...]) ⇒ Boolean
Determines whether HTML classes have class, matching conditions.
-
#index(value = nil, &block) ⇒ nil, Number
(also: #rindex)
Searches HTML classes by conditions.
-
#initialize(value = []) ⇒ HTMLClass
constructor
A new instance of HTMLClass.
-
#to_html ⇒ String
Combines all classes, ready to insert in HTML.
Constructor Details
Class Method Details
.sanitize(*values) ⇒ Array<String>
Sanitizes and normalizes HTML class. Makes array of classes flatten, removes all duplicates, splits spaced strings.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wrap_it/html_class.rb', line 35 def self.sanitize(*values) values .flatten .each_with_object([]) do |i, a| a << i.to_s if i.is_a?(String) || i.is_a?(Symbol) end .join(' ') .strip .split(/\s+/) .uniq end |
Instance Method Details
#delete([cond, ...], &block) ⇒ self
Removes elements from list by some conditions.
See #index for condition details
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/wrap_it/html_class.rb', line 120 def delete(*args, &block) obj = __getobj__ args.each do |x| i = index(x) next if i.nil? || i.is_a?(Enumerator) obj.delete_at(i) end unless block.nil? i = index(&block) i.nil? || obj.delete_at(i) end self end |
#include?([cond, ...]) ⇒ Boolean
Determines whether HTML classes have class, matching conditions
168 169 170 171 172 |
# File 'lib/wrap_it/html_class.rb', line 168 def include?(*args) args.all? do |x| x.is_a?(Proc) ? !index(&x).nil? : !index(x).nil? end end |
#index(value = nil, &block) ⇒ nil, Number Also known as: rindex
Searches HTML classes by conditions
Conditions can be a Symbol, String, Array of strings or Regexp. Or you can provide block for searching.
For Strings and Symbols array-like search used (symbols converted to strings). For Array conditions, any value from this array will match. For Regexp - regular expression matcher will used.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/wrap_it/html_class.rb', line 148 def index(value = nil, &block) value.is_a?(Symbol) && value = value.to_s value.is_a?(Array) && value.map! { |x| x.to_s } case when value.is_a?(Regexp) then __getobj__.index { |x| value =~ x } when value.is_a?(Array) then __getobj__.index { |x| value.include?(x) } when block_given? then __getobj__.index(&block) when value.nil? then __getobj__.index else __getobj__.index(value) end end |
#to_html ⇒ String
Combines all classes, ready to insert in HTML.
Actually just join all values with spaces
180 181 182 |
# File 'lib/wrap_it/html_class.rb', line 180 def to_html __getobj__.join(' ') end |