https://github.com/lpogic/extree
Convenient creation of tree structures in Ruby
https://github.com/lpogic/extree
ruby tree-structure
Last synced: 10 months ago
JSON representation
Convenient creation of tree structures in Ruby
- Host: GitHub
- URL: https://github.com/lpogic/extree
- Owner: lpogic
- Created: 2024-02-23T09:08:06.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-29T20:43:24.000Z (about 2 years ago)
- Last Synced: 2025-08-19T11:30:15.681Z (11 months ago)
- Topics: ruby, tree-structure
- Language: Ruby
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
extree - DSL for building various data structures
===
XML-like data definition build in ruby code.
Installation
---
```
gem install extree
```
Usage
---
### 1. Simple tree building
```RUBY
require 'extree'
class Tree
include Extree
def initialize value = nil, &b
@value = value
@nodes = []
# sets self as current scope and call given block
scope! &b
end
# define special scoping method
def! :tr do |value = nil, &b|
@nodes << Tree.new(value, &b)
end
# print the tree in more human readable form
def inspect depth = 0
spaces = " " * (depth << 1)
nodes = @nodes.empty? ? "" : "\n#{@nodes.map{ _1.inspect(depth + 1)}.join("\n")}\n#{spaces}"
"#{spaces}<#{@value}#{nodes}>"
end
end
tree = Tree.new do
tr! 1
tr! 2 do
tr! 3
end
tr! 4 do
tr! 5 do
tr! 6
end
end
tr! 7
end
p tree
# Output:
# <
# <1>
# <2
# <3>
# >
# <4
# <5
# <6>
# >
# >
# <7>
# >
```
### 2. HTML in Ruby
```RUBY
require 'extree'
module Parent
include Extree
def! :div do |**na, &b|
element = Element.new "div"
scope! element, **na, &b
push_element element
end
def! :span do |**na, &b|
element = Element.new "span"
scope! element, **na, &b
push_element element
end
def push_element element
element
end
end
class Element
include Parent
def initialize tag
@attributes = {}
@children = []
@tag = tag
end
def push_element element
@children << element
end
def class=(classs)
@attributes[:class] = classs
end
def id=(id)
@attributes[:id] = id
end
def to_html depth = 0
spaces = " " * (depth << 1)
attributes = @attributes.map{|k, v| " #{k}='#{v}'" }.join
children = @children.map{|ch| "\n" + ch.to_html(depth + 1) }.join
"#{spaces}<#{@tag}#{attributes}>#{children}#{ "\n#{spaces}" if !@children.empty? }#{@tag}>"
end
end
include Parent
#######################################################
@first_div_id = "first-div-id"
html = div! class: "dividor" do
span! id: :spanner do
div! class: "first", id: @first_div_id
div! class: "second"
end
end
puts html.to_html
# Output:
#
#
#
#
#
#
```
Authors
---
- Łukasz Pomietło (oficjalnyadreslukasza@gmail.com)