lib/hexp/node/attributes.rb in hexp-0.2.0 vs lib/hexp/node/attributes.rb in hexp-0.3.0

- old
+ new

@@ -12,13 +12,14 @@ # @example # H[:p, class: 'hello'].attr('class') # => "hello" # H[:p, class: 'hello'].attr('id', 'para1') # => H[:p, {"class"=>"hello", "id"=>"para1"}] # H[:p, class: 'hello'].attr('class', nil) # => H[:p] # + # @param [Array<#to_s>] args # @return [String|Hexp::Node] - # @api private # + # @api private def attr(*args) arity = args.count attr_name = args[0].to_s case arity @@ -36,51 +37,58 @@ # This will also return true if the attribute is present but empty. # # @example # H[:option].has_attr?('selected') #=> false # - # @param name [String|Symbol] the name of the attribute - # @return [Boolean] - # @api public + # @param [String|Symbol] name + # The name of the attribute # + # @return [true,false] + # + # @api public def has_attr?(name) attributes.has_key? name.to_s end # Check for the presence of a class # # @example # H[:span, class: "banner strong"].class?("strong") #=> true # - # @param klass [String] the name of the class to check for - # @return [Boolean] true if the class is present, false otherwise - # @api public + # @param [String] klass + # The name of the class to check for # + # @return [Boolean] + # True if the class is present, false otherwise + # + # @api public def class?(klass) attr('class') && attr('class').split(' ').include?(klass.to_s) end # Add a CSS class to the element # # @example # H[:div].add_class('foo') #=> H[:div, class: 'foo'] # - # @param klass [#to_s] The class to add + # @param [#to_s] klass + # The class to add + # # @return [Hexp::Node] - # @api public # + # @api public def add_class(klass) attr('class', [attr('class'), klass].compact.join(' ')) end # The CSS classes of this element as an array # # Convenience method so you don't have to split the class list yourself. # # @return [Array<String>] - # @api public # + # @api public def class_list @class_list ||= (attr('class') || '').split(' ').freeze end # Remove a CSS class from this element @@ -91,28 +99,30 @@ # 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. # - # @param klass [#to_s] The class to be removed - # @return [Hexp::Node] A node that is identical to this one, but with - # the given class removed - # @api public + # @param [#to_s] klass + # The class to be removed + # @return [Hexp::Node] + # A node that is identical to this one, but with the given class removed # + # @api public 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 or override multiple attributes using a hash syntax # - # @param attrs[Hash] + # @param [Hash<#to_s,#to_s>] attrs + # # @return [Hexp::Node] - # @api public # + # @api public def set_attrs(attrs) H[ self.tag, self.attributes.merge(Hash[*attrs.flat_map{|k,v| [k.to_s, v]}]), self.children @@ -121,44 +131,51 @@ alias :% :set_attrs alias :add_attributes :set_attrs # Remove an attribute by name # - # @param name [#to_s] The attribute to be removed - # @return [Hexp::Node] a new node with the attribute removed - # @api public + # @param [#to_s] name + # The attribute to be removed # + # @return [Hexp::Node] + # A new node with the attribute removed + # + # @api public def remove_attr(name) H[ self.tag, self.attributes.reject {|key,_| key == name.to_s}, self.children ] end # Attribute accessor # - # @param attribute_name [#to_s] The name of the attribute - # @return [String] The value of the attribute - # @api public + # @param_name [#to_s] attr + # The name of the attribute # + # @return [String] + # The value of the attribute + # + # @api public def [](attr_name) self.attributes[attr_name.to_s] end # 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. + # than being overwritten. See {#set_attrs} for a more basic version. # - # This method is analoguous with {Hash#merge}. As argument it can take a + # 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. # - # @param node_or_hash [#to_hexp|Hash] + # @param_or_hash [#to_hexp|Hash] node + # # @return [Hexp::Node] - # @api public # + # @api public 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|