Module: WrapIt::HTMLClass

Extended by:
DerivedAttributes
Included in:
Base
Defined in:
lib/wrap_it/html_class.rb

Overview

Methods for manipulationg with HTML class. For internal usage. You should not include this class directly - subclass from ‘WrapIt::Base` instead.

Author:

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/wrap_it/html_class.rb', line 12

def self.included(base)
  base == Base || fail(
    TypeError,
    "#{self.class.name} can be included only into WrapIt::Base"
  )
  base.extend ClassMethods
end

.sanitize(*args) ⇒ Array<String>

Sanitize HTML class list. Arguments list flatten and filtered for only Strings and Symbols. Duplicates are removed.

Parameters:

  • html_class (Object)

    HTML class

Returns:

  • (Array<String>)

    sanitized HTML classes list



28
29
30
31
32
33
34
# File 'lib/wrap_it/html_class.rb', line 28

def self.sanitize(*args)
  args
    .flatten
    .map { |a| a.is_a?(String) || a.is_a?(Symbol) ? a.to_s : nil }
    .compact
    .uniq
end

Instance Method Details

#add_html_class(*args) ⇒ self

Adds html class(es) to element. Chaining allowed. All classes will be converted to Strings, duplicates are removed.

Examples:

element.html_class = 'a'
element.add_html_class :b, :c, ['d', :c, :e, 'a']
element.html_class #=> ['a', 'b', 'c', 'd', 'e']

Parameters:

  • html_class (Symbol, String, Array<Symbol, String>)

    HTML class or list of HTML classes.

Returns:

  • (self)


79
80
81
82
83
84
85
86
# File 'lib/wrap_it/html_class.rb', line 79

def add_html_class(*args)
  if @options.key?(:class)
    @options[:class].is_a?(Array) || options[:class] = [options[:class]]
    args += @options[:class]
  end
  @options[:class] = HTMLClass.sanitize(*args)
  self # allow chaining
end

#html_classArray<String>

html class getter.

Returns:

  • (Array<String>)

    array of html classes of element.



40
41
42
# File 'lib/wrap_it/html_class.rb', line 40

def html_class
  @options[:class]
end

#html_class=(value) ⇒ void

This method returns an undefined value.

Sets html class(es) for element.

Examples:

element.html_class = [:a, 'b', ['c', :d, 'a']]
element.html_class #=> ['a', 'b', 'c', 'd']

Parameters:

  • value (Symbol, String, Array<Symbol, String>)

    HTML class or list of classes. All classes will be converted to Strings, duplicates are removed.



62
63
64
65
# File 'lib/wrap_it/html_class.rb', line 62

def html_class=(value)
  @options[:class] = []
  add_html_class(value)
end

#html_class([condition, ...]) ⇒ Boolean #html_class(&block) ⇒ Boolean

Determines whether element contains class, satisfied by conditions, specified in method arguments.

There are two forms of method call: with list of conditions as arguments and with block for comparing. Method makes comparison with html class untill first ‘true` return value or end of list. All conditions should be satisfied for `true` return of this method.

In first form, each argument treated as condition. Condition can be a ‘Regexp`, so html classes of element tested for matching to that regular expression. If condition is an `Array` then every class will be tested for presence in this array. If condition is `Symbol` or `String` classes will be compared with it via equality operator `==`.

In second form all arguments are ignored and for each comparison given block called with html class as argument. Block return value then used.

Examples:

with ‘Symbol` or `String` conditions

element.html_class = [:a, :b, :c]
element.html_class?(:a)       #=> true
element.html_class?(:d)       #=> false
element.html_class?(:a, 'b')  #=> true
element.html_class?(:a, :d)   #=> false

with ‘Regexp` conditions

element.html_class = [:some, :test]
element.html_class?(/some/)         #=> true
element.html_class?(/some/, /bad/)  #=> false
element.html_class?(/some/, :test)  #=> true

with ‘Array` conditions

element.html_class = [:a, :b, :c]
element.html_class?(%w(a d)) #=> true
element.html_class?(%w(e d)) #=> false

with block

element.html_class = [:a, :b, :c]
element.html_class? { |x| x == 'a' } #=> true

Parameters:

  • condition (<Regexp, Symbol, String, Array<String>] condition for comparison.)

    ondition [<Regexp, Symbol, String, Array<String>] condition for comparison.

Yields:

  • (html_class)

    Gives each html class to block. You should return ‘true` if element contains this html class.

Yield Parameters:

  • html_class (String)

    html class to inspect.

Yield Returns:

  • (Boolean)

    whether element has html class.

Returns:

  • (Boolean)

    whether element has class with specified conditions.



162
163
164
# File 'lib/wrap_it/html_class.rb', line 162

def html_class?(*args, &block)
  args.all? { |c| inspect_class(:any?, c, &block) }
end

#html_class_prefixString

HTML class prefix getter.

Returns:

  • (String)

    HTML class prefix.



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

def html_class_prefix
  @html_class_prefix ||= self.class.html_class_prefix
end

#no_html_class?(*args, &block) ⇒ Boolean

Determines whether element doesn’t contains class, satisfied by conditions, specified in method arguments.

Returns:

  • (Boolean)

See Also:



171
172
173
# File 'lib/wrap_it/html_class.rb', line 171

def no_html_class?(*args, &block)
  args.all? { |c| inspect_class(:none?, c, &block) }
end

#remove_html_class(*args) ⇒ self

Removes html class(es) from element. Chaining allowed.

Examples:

element.add_html_class %w(a b c d e)
element.remove_html_class :b, ['c', :e]
element.html_class #=> ['a', 'd']

Parameters:

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

    HTML class or list of HTML classes.

Returns:

  • (self)


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/wrap_it/html_class.rb', line 99

def remove_html_class(*args)
  args.flatten!
  re = []
  args.reject! { |c| c.is_a?(Regexp) && re << c && true }
  args = args.uniq.map { |c| c.to_s }
  args.size > 0 && @options[:class].reject! { |c| args.include?(c) }
  re.is_a?(Array) && re.each do |r|
    @options[:class].reject! { |c| r.match(c) }
  end
  self # allow chaining
end