Class: ElementComponent::Element

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element, closing_tag: true, **attribute) ⇒ Element

Returns a new instance of Element.



5
6
7
8
9
10
11
12
# File 'lib/element_component/element.rb', line 5

def initialize(element, closing_tag: true, **attribute)
  @element = element
  @closing_tag = closing_tag
  @html = ""

  add_attribute!(attribute)
  reset_contents!
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/element_component/element.rb', line 3

def attributes
  @attributes
end

#contentsObject (readonly)

Returns the value of attribute contents.



3
4
5
# File 'lib/element_component/element.rb', line 3

def contents
  @contents
end

#elementObject (readonly)

Returns the value of attribute element.



3
4
5
# File 'lib/element_component/element.rb', line 3

def element
  @element
end

#htmlObject (readonly)

Returns the value of attribute html.



3
4
5
# File 'lib/element_component/element.rb', line 3

def html
  @html
end

Instance Method Details

#add_attribute(hash_attr) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/element_component/element.rb', line 36

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



30
31
32
33
34
# File 'lib/element_component/element.rb', line 30

def add_attribute!(hash_attr)
  reset_attributes!

  add_attribute(hash_attr)
end

#add_content(content = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/element_component/element.rb', line 20

def add_content(content = nil, &block)
  if block_given?
    @contents.push(block)
  else
    @contents.push(content)
  end

  self
end

#add_content!(content) ⇒ Object



14
15
16
17
18
# File 'lib/element_component/element.rb', line 14

def add_content!(content)
  reset_contents!

  add_content(content)
end

#new_element(*args, **kargs) ⇒ Object



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

def new_element(*args, **kargs) = Element.new(*args, **kargs)

#remove_attribute(attribute) ⇒ Object



51
52
53
# File 'lib/element_component/element.rb', line 51

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

#remove_attribute_value(attribute, value) ⇒ Object



55
56
57
# File 'lib/element_component/element.rb', line 55

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

#renderObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/element_component/element.rb', line 69

def render
  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'

  @html
end

#reset_attributes!Object



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

def reset_attributes!
  @attributes = {}
end

#reset_contents!Object



59
60
61
# File 'lib/element_component/element.rb', line 59

def reset_contents!
  @contents = []
end