Class: RubyUIConverter::Nodes::Element

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_ui_converter/nodes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, attributes: [], self_closing: false) ⇒ Element

Returns a new instance of Element.



25
26
27
28
29
30
# File 'lib/ruby_ui_converter/nodes.rb', line 25

def initialize(name:, attributes: [], self_closing: false)
  @name = name
  @attributes = attributes
  @children = []
  @self_closing = self_closing
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



22
23
24
# File 'lib/ruby_ui_converter/nodes.rb', line 22

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children.



22
23
24
# File 'lib/ruby_ui_converter/nodes.rb', line 22

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/ruby_ui_converter/nodes.rb', line 22

def name
  @name
end

#self_closingObject

Returns the value of attribute self_closing.



23
24
25
# File 'lib/ruby_ui_converter/nodes.rb', line 23

def self_closing
  @self_closing
end

Instance Method Details

#attr?(name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/ruby_ui_converter/nodes.rb', line 48

def attr?(name)
  attributes.any? { |attr_name, _| attr_name == name }
end

#static_attr(name) ⇒ Object

Returns the literal value of an attribute when it has no ERB parts, otherwise nil. Useful for ComponentMap matchers/emitters.



34
35
36
37
38
39
40
# File 'lib/ruby_ui_converter/nodes.rb', line 34

def static_attr(name)
  attr = attributes.find { |attr_name, _| attr_name == name }
  return nil unless attr && attr[1]
  return nil unless attr[1].all? { |kind, _| kind == :text }

  attr[1].map { |_, value| value }.join
end

#static_classesObject

Returns the static CSS classes when the ‘class` attribute has no ERB, otherwise an empty array. Useful for ComponentMap matchers.



44
45
46
# File 'lib/ruby_ui_converter/nodes.rb', line 44

def static_classes
  static_attr("class").to_s.split
end