-
Notifications
You must be signed in to change notification settings - Fork 137
/
json.rb
53 lines (46 loc) · 1 KB
/
json.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true
module Stupidedi
using Refinements
module Writer
class Json
def initialize(node)
@node = node
end
# @return [Hash]
def write(out = Hash.new { |k, v| self[k] = v })
build(@node, out)
out
end
private
def resolve_traverser(node)
case
when node.transmission?
Transmission
when node.interchange?
Interchange
when node.segment?
Segment
when node.loop?
Loop
when node.element?
Element
when node.functional_group?
FunctionalGroup
when node.transaction_set?
TransactionSet
when node.table?
Table
else
NullNode
end.new(node)
end
def build(node, out)
traverser = resolve_traverser(node)
traverser.reduce(out) do |children, memo = {}|
build(children, memo)
end
out
end
end
end
end