beid 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/beid/node.rb ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Beid
4
+ Node = Struct.new(:type, :attributes, :children, :range, :marker, keyword_init: true) do
5
+ def initialize(type:, attributes: {}, children: [], range:, marker: nil)
6
+ super(type: type, attributes: freeze_value(attributes), children: children.freeze,
7
+ range: range, marker: marker&.freeze)
8
+ freeze
9
+ end
10
+
11
+ def text
12
+ attributes[:text]
13
+ end
14
+
15
+ private
16
+
17
+ def freeze_value(value)
18
+ case value
19
+ when Hash
20
+ value.each { |key, item| freeze_value(key); freeze_value(item) }
21
+ when Array
22
+ value.each { |item| freeze_value(item) }
23
+ end
24
+ value.freeze
25
+ end
26
+ end
27
+ end