Class: HTMLAttributes

Inherits:
Object
  • Object
show all
Defined in:
lib/objective_elements/html_attributes.rb

Overview

Represents standard HTML attributes, such as class=“myclass”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new = nil) ⇒ HTMLAttributes

Returns a new instance of HTMLAttributes.



4
5
6
7
# File 'lib/objective_elements/html_attributes.rb', line 4

def initialize(new = nil)
  @content = {}
  self << new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, arg = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# 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

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/objective_elements/html_attributes.rb', line 3

def content
  @content
end

Instance Method Details

#<<(new) ⇒ Object

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.



29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



9
10
11
# File 'lib/objective_elements/html_attributes.rb', line 9

def [](key)
  @content[key.to_sym]
end

#delete(trash) ⇒ Object



42
43
44
45
46
47
48
49
# 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?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/objective_elements/html_attributes.rb', line 65

def empty?
  @content.empty?
end

#replace(new) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# 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) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# 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_sObject



13
14
15
16
17
18
19
20
21
22
23
24
# 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