class HTMLAttributes
Represents standard HTML attributes, such as class=“myclass”
Attributes
content[R]
Public Class Methods
new(new = nil)
click to toggle source
# File lib/objective_elements/html_attributes.rb, line 4 def initialize(new = nil) @content = {} self << new end
Public Instance Methods
<<(new)
click to toggle source
This is the only way we add new attributes. Flexible about what you give it– accepts both strings and symbols for the keys, and both strings and arrays for the values.
# File lib/objective_elements/html_attributes.rb, line 29 def <<(new) # Don't break everything if this is passed an empty value: return self unless new if new.is_a? Hash add_hash(new) else add_string(new) end self end
[](key)
click to toggle source
# File lib/objective_elements/html_attributes.rb, line 9 def [](key) @content[key.to_sym] end
delete(trash)
click to toggle source
# File lib/objective_elements/html_attributes.rb, line 42 def delete(trash) # accepts an array or a single element [trash].flatten .map(&:to_sym) .each { |k| @content.delete k } self end
empty?()
click to toggle source
# File lib/objective_elements/html_attributes.rb, line 65 def empty? @content.empty? end
method_missing(method, arg = nil)
click to toggle source
Calls superclass method
# File lib/objective_elements/html_attributes.rb, line 69 def method_missing(method, arg = nil) if method[-1] == '=' raise 'must supply new value' unless arg replace(method[0..-2] => arg) elsif @content.key? method @content[method].join(' ') else super end end
replace(new)
click to toggle source
# File lib/objective_elements/html_attributes.rb, line 51 def replace(new) formatted_new = if new.is_a? String hashify_input(new) else new.transform_keys(&:to_sym) end delete formatted_new.keys add_hash formatted_new self end
respond_to_missing?(method, include_private = false)
click to toggle source
Calls superclass method
# File lib/objective_elements/html_attributes.rb, line 81 def respond_to_missing?(method, include_private = false) (method[-1] == '=') || (@content.key? method) || super end
to_s()
click to toggle source
# File lib/objective_elements/html_attributes.rb, line 13 def to_s return_string = '' @content.each_pair do |k, v| # If an attribute has no values, we need to introduce an empty string to # the array in order to get the correct format (alt="", for example): v << '' if v.empty? return_string << "#{k}=\"#{v.join ' '}\" " end return_string.strip end
Private Instance Methods
add_hash(new_hash)
click to toggle source
Input: Keys are attribute names (either strings or symbols), values are attribute values (either a string or an array of strings)
# File lib/objective_elements/html_attributes.rb, line 95 def add_hash(new_hash) formatted_new = {} new_hash.each_pair do |k, v| v = v.split(' ') if v.is_a? String formatted_new[k.to_sym] = v end @content.merge!(formatted_new) do |_key, oldval, newval| oldval.concat(newval) end self end
add_string(new_string)
click to toggle source
# File lib/objective_elements/html_attributes.rb, line 89 def add_string(new_string) add_hash hashify_input new_string end
hashify_input(new_string)
click to toggle source
# File lib/objective_elements/html_attributes.rb, line 111 def hashify_input(new_string) # looking for something like: # 'class="something something-else" id="my-id" alt=""' new_hash = {} new_string.scan(/ ?([^="]+)="([^"]*)"/).each do |match| # Returns something like: # [['class','something something-else'],['id','my-id'],['alt', '']] key, val = *match if new_hash[key] new_hash[key] << ' ' + val else new_hash[key] = val end end new_hash end