class WrapIt::HTMLClass

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

@author Alexey Ovchinnikov <alexiss@cybernetlab.ru>

Public Class Methods

new(value = []) click to toggle source
Calls superclass method
# File lib/wrap_it/html_class.rb, line 47
def initialize(value = [])
  super(HTMLClass.sanitize(value))
end
sanitize(*values) click to toggle source

Sanitizes and normalizes HTML class. Makes array of classes flatten, removes all duplicates, splits spaced strings.

@param values [Object] can be a symbol, string, array of symbols and

strings, array of strings, strings can contains spaces.

@return [Array<String>] sanitized list of HTML classes

# 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

Public Instance Methods

clear() click to toggle source

@private

# File lib/wrap_it/html_class.rb, line 104
def clear
  __getobj__.clear
  self
end
delete(*args, &block) click to toggle source

Removes elements from list by some conditions.

See {#index} for condition details

@overload delete([cond, …], &block)

@param  cond [Symbol, String, Array<String>, Regexp] [description]
@param  &block [Proc] searching block

@return [self]

# 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?(*args) click to toggle source

Determines whether HTML classes have class, matching conditions

@overload include?([cond, …])

@param  cond [Symbol, String, Array<String>, Regexp] [description]

@return [Boolean] whether HTML classes include specified class

# 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) click to toggle source

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.

@param value [nil, Symbol, String, Array<String>, Regexp] condition @param block [Proc] searching block

@return [nil, Number] index of finded item or nil

# 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
Also aliased as: rindex
rindex(value = nil, &block)
Alias for: index
to_html() click to toggle source

Combines all classes, ready to insert in HTML.

Actually just join all values with spaces

@return [String] html string

# File lib/wrap_it/html_class.rb, line 180
def to_html
  __getobj__.join(' ')
end