Class: ElementComponent::Element
- Inherits:
-
Object
- Object
- ElementComponent::Element
show all
- Defined in:
- lib/element_component/element.rb
Direct Known Subclasses
Components::Alert, Components::AlertCloseButton, Components::AlertHeading, Components::AlertLink, Components::Badge, Components::Breadcrumb, Components::BreadcrumbItem, Components::Button, Components::ButtonGroup, Components::Card, Components::CardBody, Components::CardFooter, Components::CardHeader, Components::CardImage, Components::CardText, Components::CardTitle, Components::Carousel, Components::CarouselCaption, Components::CarouselItem, Components::CloseButton, Components::Dropdown, Components::DropdownDivider, Components::DropdownHeader, Components::DropdownItem, Components::DropdownMenu, Components::ListGroup, Components::ListGroupItem, Components::Modal, Components::ModalBody, Components::ModalContent, Components::ModalDialog, Components::ModalFooter, Components::ModalHeader, Components::ModalTitle, Components::Nav, Components::NavItem, Components::NavLink, Components::Navbar, Components::NavbarBrand, Components::NavbarCollapse, Components::NavbarNav, Components::NavbarToggler, Components::PageItem, Components::Pagination, Components::Progress, Components::ProgressBar, Components::Spinner, Components::Table
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(element, content = nil, closing_tag: true, **attribute, &block) ⇒ Element
Returns a new instance of Element.
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/element_component/element.rb', line 7
def initialize(element, content = nil, closing_tag: true, **attribute, &block)
@element = element
@closing_tag = closing_tag
@html = String.new
add_attribute!(attribute)
reset_contents!
add_content(content) if content
block&.call(self)
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
5
6
7
|
# File 'lib/element_component/element.rb', line 5
def attributes
@attributes
end
|
#contents ⇒ Object
Returns the value of attribute contents.
5
6
7
|
# File 'lib/element_component/element.rb', line 5
def contents
@contents
end
|
#element ⇒ Object
Returns the value of attribute element.
5
6
7
|
# File 'lib/element_component/element.rb', line 5
def element
@element
end
|
#html ⇒ Object
Returns the value of attribute html.
5
6
7
|
# File 'lib/element_component/element.rb', line 5
def html
@html
end
|
Instance Method Details
#add_attribute(hash_attr) ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/element_component/element.rb', line 42
def add_attribute(hash_attr)
hash_attr.each_key do |attr|
@attributes[attr] = [] unless @attributes.key?(attr)
hash_attr[attr]
.to_s
.split
.each do |value|
@attributes[attr].push(value)
end
end
self
end
|
#add_attribute!(hash_attr) ⇒ Object
36
37
38
39
40
|
# File 'lib/element_component/element.rb', line 36
def add_attribute!(hash_attr)
reset_attributes!
add_attribute(hash_attr)
end
|
#add_content(content = nil, &block) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/element_component/element.rb', line 24
def add_content(content = nil, &block)
if block_given?
@contents.push(block)
elsif content.is_a?(Array)
@contents.push(*content)
else
@contents.push(content)
end
self
end
|
#add_content!(content = nil) ⇒ Object
18
19
20
21
22
|
# File 'lib/element_component/element.rb', line 18
def add_content!(content = nil, &)
reset_contents!
add_content(content, &)
end
|
#new_element ⇒ Object
73
|
# File 'lib/element_component/element.rb', line 73
def new_element(...) = Element.new(...)
|
#remove_attribute(attribute) ⇒ Object
57
58
59
|
# File 'lib/element_component/element.rb', line 57
def remove_attribute(attribute)
@attributes = @attributes.except(attribute)
end
|
#remove_attribute_value(attribute, value) ⇒ Object
61
62
63
|
# File 'lib/element_component/element.rb', line 61
def remove_attribute_value(attribute, value)
attributes[attribute].delete(value)
end
|
#render ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/element_component/element.rb', line 75
def render
@html = String.new
before_render if respond_to? "before_render"
if respond_to? "around_render"
around_render { build }
else
build
end
after_render(@html) if respond_to? "after_render"
defined?(ActiveSupport::SafeBuffer) ? @html.html_safe : @html
end
|
#render_in(view_context, &block) ⇒ Object
91
|
# File 'lib/element_component/element.rb', line 91
def render_in(view_context, &block) = render
|
#reset_attributes! ⇒ Object
69
70
71
|
# File 'lib/element_component/element.rb', line 69
def reset_attributes!
@attributes = {}
end
|
#reset_contents! ⇒ Object
65
66
67
|
# File 'lib/element_component/element.rb', line 65
def reset_contents!
@contents = []
end
|