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, 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

#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



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_elementObject



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

#renderObject



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