Class: Lutaml::Model::ComparableModel::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/model/comparable_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, color: nil) ⇒ Tree



112
113
114
115
116
# File 'lib/lutaml/model/comparable_model.rb', line 112

def initialize(content, color: nil)
  @content = content
  @children = []
  @color = color
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



110
111
112
# File 'lib/lutaml/model/comparable_model.rb', line 110

def children
  @children
end

#colorObject

Returns the value of attribute color.



110
111
112
# File 'lib/lutaml/model/comparable_model.rb', line 110

def color
  @color
end

#contentObject

Returns the value of attribute content.



110
111
112
# File 'lib/lutaml/model/comparable_model.rb', line 110

def content
  @content
end

Instance Method Details

#add_child(child) ⇒ Object



118
119
120
# File 'lib/lutaml/model/comparable_model.rb', line 118

def add_child(child)
  @children << child
end

#to_s(indent = "", is_last = true) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/lutaml/model/comparable_model.rb', line 122

def to_s(indent = "", is_last = true)
  prefix = is_last ? "└── " : "├── "
  result = "#{indent}#{colorize(prefix + @content, @color)}\n"
  @children.each_with_index do |child, index|
    is_last_child = index == @children.size - 1
    child_indent = indent + (if is_last
                               "    "
                             else
                               "#{colorize('│',
                                           @color)}   "
                             end)
    result += child.to_s(child_indent, is_last_child)
  end
  result
end