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, &block) ⇒ Element

Returns a new instance of Element.



7
8
9
10
11
12
13
14
15
# File 'lib/element_component/element.rb', line 7

def initialize(element, closing_tag: true, **attribute, &block)
  @element = element
  @closing_tag = closing_tag
  @html = String.new

  add_attribute!(attribute)
  reset_contents!
  instance_eval(&block) if block
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#contentsObject (readonly)

Returns the value of attribute contents.



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

def contents
  @contents
end

#elementObject (readonly)

Returns the value of attribute element.



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

def element
  @element
end

#htmlObject (readonly)

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/element_component/element.rb', line 39

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



33
34
35
36
37
# File 'lib/element_component/element.rb', line 33

def add_attribute!(hash_attr)
  reset_attributes!

  add_attribute(hash_attr)
end

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



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

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

  self
end

#add_content!(content) ⇒ Object



17
18
19
20
21
# File 'lib/element_component/element.rb', line 17

def add_content!(content)
  reset_contents!

  add_content(content)
end

#new_elementObject



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

def new_element(...) = Element.new(...)

#remove_attribute(attribute) ⇒ Object



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

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

#remove_attribute_value(attribute, value) ⇒ Object



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

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

#renderObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/element_component/element.rb', line 72

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"

  @html
end

#reset_attributes!Object



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

def reset_attributes!
  @attributes = {}
end

#reset_contents!Object



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

def reset_contents!
  @contents = []
end