Class: El::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/element_component/core/element.rb

Overview

represent the element base

Direct Known Subclasses

Bulma::Datagrid, A, Article, Aside, Form, Input, Li, Nav, Span, Table, Tbody, Td, Tfoot, Th, Thead, Tr, Ul

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, contents: [], attributes: {}, closing_tag: true) ⇒ Element

Returns a new instance of Element.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/element_component/core/element.rb', line 8

def initialize(element, contents: [], attributes: {}, closing_tag: true)
  @element = element
  @closing_tag = closing_tag

  reset_attributes!
  reset_contents!
  reset_components!

  contents.each do |content|
    add_content content
  end

  attributes.each_key do |attribute_key|
    add_attribute attribute_key, attributes[attribute_key]
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/element_component/core/element.rb', line 6

def attributes
  @attributes
end

#componentsObject (readonly)

Returns the value of attribute components.



6
7
8
# File 'lib/element_component/core/element.rb', line 6

def components
  @components
end

#elementObject (readonly)

Returns the value of attribute element.



6
7
8
# File 'lib/element_component/core/element.rb', line 6

def element
  @element
end

Instance Method Details

#add_attribute(attribute, value, reset: false) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/element_component/core/element.rb', line 42

def add_attribute(attribute, value, reset: false)
  attribute = attribute.to_sym

  @attributes.delete attribute if reset

  return @attributes[attribute].push(value) if @attributes.key?(attribute)

  @attributes[attribute] = [value]
end

#add_component(identifier, component) ⇒ Object



32
33
34
35
# File 'lib/element_component/core/element.rb', line 32

def add_component(identifier, component)
  identifier = identifier.to_sym
  @components.store identifier, component
end

#add_content(content, reset: false) ⇒ Object



25
26
27
28
29
30
# File 'lib/element_component/core/element.rb', line 25

def add_content(content, reset: false)
  reset_contents! if reset
  @contents.push(content)

  content
end

#buildObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/element_component/core/element.rb', line 74

def build
  partial = open_tag

  @contents.each do |content|
    partial << (content.is_a?(Element) ? content.build : content.to_s)
  end

  partial << close_tag if @closing_tag
  partial
end

#get_component(identifier) ⇒ Object



37
38
39
40
# File 'lib/element_component/core/element.rb', line 37

def get_component(identifier)
  identifier = identifier.to_sym
  @components.fetch identifier
end

#remove_attribute!(attribute) ⇒ Object



52
53
54
55
# File 'lib/element_component/core/element.rb', line 52

def remove_attribute!(attribute)
  attribute = attribute.to_sym
  @attributes = @attributes.except(attribute)
end

#remove_attribute_value(attribute, value) ⇒ Object



57
58
59
60
# File 'lib/element_component/core/element.rb', line 57

def remove_attribute_value(attribute, value)
  attribute = attribute.to_sym
  attributes[attribute].delete(value)
end

#reset_attributes!Object



66
67
68
# File 'lib/element_component/core/element.rb', line 66

def reset_attributes!
  @attributes = {}
end

#reset_components!Object



70
71
72
# File 'lib/element_component/core/element.rb', line 70

def reset_components!
  @components = {}
end

#reset_contents!Object



62
63
64
# File 'lib/element_component/core/element.rb', line 62

def reset_contents!
  @contents = []
end