Module: WrapIt::HTMLClass
Overview
Methods for manipulationg with HTML class. For internal usage. You should not include this class directly - subclass from WrapIt::Base instead.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .included(base) ⇒ Object
-
.sanitize(*args) ⇒ Array<String>
Sanitize HTML class list.
Instance Method Summary collapse
-
#add_html_class(*args) ⇒ self
Adds html class(es) to element.
-
#html_class ⇒ Array<String>
html class getter.
-
#html_class=(value) ⇒ void
Sets html class(es) for element.
-
#html_class?(*args) {|html_class| ... } ⇒ Boolean
Determines whether element contains class, satisfied by conditions, specified in method arguments.
-
#html_class_prefix ⇒ String
HTML class prefix getter.
-
#no_html_class?(*args, &block) ⇒ Boolean
Determines whether element doesn’t contains class, satisfied by conditions, specified in method arguments.
-
#remove_html_class(*args) ⇒ self
Removes html class(es) from element.
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.
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.
79 80 81 82 83 84 85 86 |
# File 'lib/wrap_it/html_class.rb', line 79 def add_html_class(*args) if .key?(:class) [:class].is_a?(Array) || [:class] = [[:class]] args += [:class] end [:class] = HTMLClass.sanitize(*args) self # allow chaining end |
#html_class ⇒ Array<String>
html class getter.
40 41 42 |
# File 'lib/wrap_it/html_class.rb', line 40 def html_class [:class] end |
#html_class=(value) ⇒ void
This method returns an undefined value.
Sets html class(es) for element.
62 63 64 65 |
# File 'lib/wrap_it/html_class.rb', line 62 def html_class=(value) [: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.
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_prefix ⇒ String
HTML class prefix getter.
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.
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.
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 && [:class].reject! { |c| args.include?(c) } re.is_a?(Array) && re.each do |r| [:class].reject! { |c| r.match(c) } end self # allow chaining end |