Module: Hexp::Node::Attributes
- Included in:
- Hexp::Node
- Defined in:
- lib/hexp/node/attributes.rb
Overview
Node API methods that deal with attributes
Instance Method Summary collapse
-
#[](attr_name) ⇒ String
Attribute accessor.
-
#add_class(klass) ⇒ Hexp::Node
Add a CSS class to the element.
-
#attr(*args) ⇒ String|Hexp::Node
private
Attribute getter/setter.
-
#class?(klass) ⇒ Boolean
Check for the presence of a class.
-
#class_list ⇒ Array<String>
The CSS classes of this element as an array.
-
#has_attr?(name) ⇒ true, false
Is an attribute present.
-
#merge_attrs(node_or_hash) ⇒ Hexp::Node
(also: #%)
Merge attributes into this Hexp.
-
#remove_attr(name) ⇒ Hexp::Node
Remove an attribute by name.
-
#remove_class(klass) ⇒ Hexp::Node
Remove a CSS class from this element.
-
#set_attrs(attrs) ⇒ Hexp::Node
Set or override multiple attributes using a hash syntax.
Instance Method Details
#[](attr_name) ⇒ String
Attribute accessor
158 159 160 |
# File 'lib/hexp/node/attributes.rb', line 158 def [](attr_name) self.attributes[attr_name.to_s] end |
#add_class(klass) ⇒ Hexp::Node
Add a CSS class to the element
79 80 81 |
# File 'lib/hexp/node/attributes.rb', line 79 def add_class(klass) attr('class', [attr('class'), klass].compact.join(' ')) end |
#attr(*args) ⇒ String|Hexp::Node
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Attribute getter/setter
When called with one argument : return the attribute value with that name. When called with two arguments : return a new Node with the attribute set. When the second argument is nil : return a new Node with the attribute unset.
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hexp/node/attributes.rb', line 21 def attr(*args) arity = args.count attr_name = args[0].to_s case arity when 1 attributes[attr_name] when 2 set_attr(*args) else raise ArgumentError, "wrong number of arguments(#{arity} for 1..2)" end end |
#class?(klass) ⇒ Boolean
Check for the presence of a class
64 65 66 |
# File 'lib/hexp/node/attributes.rb', line 64 def class?(klass) attr('class') && attr('class').split(' ').include?(klass.to_s) end |
#class_list ⇒ Array<String>
The CSS classes of this element as an array
Convenience method so you don’t have to split the class list yourself.
90 91 92 |
# File 'lib/hexp/node/attributes.rb', line 90 def class_list @class_list ||= (attr('class') || '').split(' ') end |
#has_attr?(name) ⇒ true, false
Is an attribute present
This will also return true if the attribute is present but empty.
48 49 50 |
# File 'lib/hexp/node/attributes.rb', line 48 def has_attr?(name) attributes.has_key? name.to_s end |
#merge_attrs(node_or_hash) ⇒ Hexp::Node Also known as: %
Merge attributes into this Hexp
Class attributes are treated special : the class lists are merged, rather than being overwritten. See #set_attrs for a more basic version.
This method is analoguous with ‘Hash#merge`. As argument it can take a Hash, or another Hexp element, in which case that element’s attributes are used.
176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/hexp/node/attributes.rb', line 176 def merge_attrs(node_or_hash) hash = node_or_hash.respond_to?(:to_hexp) ? node_or_hash.to_hexp.attributes : node_or_hash result = self hash.each do |key,value| result = if key.to_s == 'class' result.add_class(value) else result.attr(key, value) end end result end |
#remove_attr(name) ⇒ Hexp::Node
Remove an attribute by name
141 142 143 144 145 146 147 |
# File 'lib/hexp/node/attributes.rb', line 141 def remove_attr(name) H[ self.tag, self.attributes.reject {|key,_| key == name.to_s}, self.children ] end |
#remove_class(klass) ⇒ Hexp::Node
Remove a CSS class from this element
If the resulting class list is empty, the class attribute will be removed. If the class is present several times all instances will be removed. If it’s not present at all, the class list will be unmodified.
Calling this on a node with a class attribute that is equal to an empty string will result in the class attribute being removed.
110 111 112 113 114 115 |
# File 'lib/hexp/node/attributes.rb', line 110 def remove_class(klass) return self unless has_attr?('class') new_list = class_list - [klass.to_s] return remove_attr('class') if new_list.empty? attr('class', new_list.join(' ')) end |
#set_attrs(attrs) ⇒ Hexp::Node
Set or override multiple attributes using a hash syntax
124 125 126 127 128 129 130 |
# File 'lib/hexp/node/attributes.rb', line 124 def set_attrs(attrs) H[ self.tag, Hash[*attrs.flat_map{|k,v| [k.to_s, v]}], self.children ] end |