Class: WrapIt::HTMLClass

Inherits:
Array
  • Object
show all
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

Author:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = []) ⇒ HTMLClass

Returns a new instance of HTMLClass.



47
48
49
# File 'lib/wrap_it/html_class.rb', line 47

def initialize(value = [])
  super(HTMLClass.sanitize(value))
end

Class Method Details

.sanitize(*values) ⇒ Array<String>

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

Parameters:

  • values (Object)

    can be a symbol, string, array of symbols and strings, array of strings, strings can contains spaces.

Returns:

  • (Array<String>)

    sanitized list of HTML classes



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

Parameters:

  • cond (Symbol, String, Array<String>, Regexp)

    [description]

  • &block (Proc)

    searching block

Returns:

  • (self)


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

Parameters:

  • cond (Symbol, String, Array<String>, Regexp)

    [description]

Returns:

  • (Boolean)

    whether HTML classes include specified class



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.

Parameters:

  • value (nil, Symbol, String, Array<String>, Regexp) (defaults to: nil)

    condition

  • block (Proc)

    searching block

Returns:

  • (nil, Number)

    index of finded item or nil



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_htmlString

Combines all classes, ready to insert in HTML.

Actually just join all values with spaces

Returns:

  • (String)

    html string



180
181
182
# File 'lib/wrap_it/html_class.rb', line 180

def to_html
  __getobj__.join(' ')
end