Class: HTMLAttributes
- Inherits:
-
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
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
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/objective_elements/html_attributes.rb', line 61
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
#content ⇒ Object
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.
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/objective_elements/html_attributes.rb', line 21
def <<(new)
return self unless new
if new.is_a? Hash
add_hash(new)
else
add_string(new)
end
self
end
|
#delete(trash) ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/objective_elements/html_attributes.rb', line 34
def delete(trash)
[trash].flatten
.map(&:to_sym)
.each { |k| @content.delete k }
self
end
|
#empty? ⇒ Boolean
57
58
59
|
# File 'lib/objective_elements/html_attributes.rb', line 57
def empty?
@content.empty?
end
|
#replace(new) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/objective_elements/html_attributes.rb', line 43
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
73
74
75
76
77
|
# File 'lib/objective_elements/html_attributes.rb', line 73
def respond_to_missing?(method, include_private = false)
(method[-1] == '=') ||
(@content.key? method) ||
super
end
|
#to_s ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/objective_elements/html_attributes.rb', line 9
def to_s
return_string = ''
@content.each_pair do |k, v|
return_string << "#{k}=\"#{v.join ' '}\" "
end
return_string.strip
end
|