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.



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

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/objective_elements/html_attributes.rb', line 65

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.



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

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.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/objective_elements/html_attributes.rb', line 25

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



12
13
14
# File 'lib/objective_elements/html_attributes.rb', line 12

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

#delete(trash) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/objective_elements/html_attributes.rb', line 38

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)


61
62
63
# File 'lib/objective_elements/html_attributes.rb', line 61

def empty?
  @content.empty?
end

#replace(new) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/objective_elements/html_attributes.rb', line 47

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)


77
78
79
80
81
# File 'lib/objective_elements/html_attributes.rb', line 77

def respond_to_missing?(method, include_private = false)
  (method[-1] == '=') ||
    (@content.key? method) ||
    super
end

#to_sObject



16
17
18
19
20
# File 'lib/objective_elements/html_attributes.rb', line 16

def to_s
  # An attribute with no values joins to an empty string, which is the
  # correct format (alt="", for example).
  @content.map { |k, v| "#{k}=\"#{v.join ' '}\"" }.join(' ')
end