-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.rb
160 lines (131 loc) · 3.66 KB
/
logging.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# frozen_string_literal: true
require 'callable_tree'
require 'json'
require 'rexml/document'
module Node
class Identity
attr_reader :klass, :type
def initialize(klass:, type:)
@klass = klass
@type = type
end
def to_s
"#{klass}(#{type})"
end
end
module JSON
class Parser
include CallableTree::Node::Internal
prepend CallableTree::Node::Hooks::Matcher
def match?(input, **_options)
File.extname(input) == '.json'
end
def call(input, **options)
File.open(input) do |file|
json = ::JSON.load(file)
super(json, **options)
end
end
def terminate?(_output, *_inputs, **_options)
true
end
end
class Scraper
include CallableTree::Node::External
prepend CallableTree::Node::Hooks::Matcher
prepend CallableTree::Node::Hooks::Caller
def initialize(type:)
@type = type
end
def identity
Identity.new(klass: super, type: @type)
end
def match?(input, **_options)
!!input[@type.to_s]
end
def call(input, **_options)
input[@type.to_s]
.to_h { |element| [element['name'], element['emoji']] }
end
end
end
module XML
class Parser
include CallableTree::Node::Internal
prepend CallableTree::Node::Hooks::Matcher
def match?(input, **_options)
File.extname(input) == '.xml'
end
def call(input, **options)
File.open(input) do |file|
super(REXML::Document.new(file), **options)
end
end
def terminate?(_output, *_inputs, **_options)
true
end
end
class Scraper
include CallableTree::Node::External
prepend CallableTree::Node::Hooks::Matcher
prepend CallableTree::Node::Hooks::Caller
def initialize(type:)
@type = type
end
def identity
Identity.new(klass: super, type: @type)
end
def match?(input, **_options)
!input.get_elements("//#{@type}").empty?
end
def call(input, **_options)
input
.get_elements("//#{@type}")
.first
.to_h { |element| [element['name'], element['emoji']] }
end
end
end
end
module Logging
INDENT_SIZE = 2
BLANK = ' '
LIST_STYLE = '*'
INPUT_LABEL = 'Input :'
OUTPUT_LABEL = 'Output:'
def self.loggable(node)
node.after_matcher! do |matched, _node_:, **|
prefix = LIST_STYLE.rjust((_node_.depth * INDENT_SIZE) - INDENT_SIZE + LIST_STYLE.length, BLANK)
puts "#{prefix} #{_node_.identity}: [matched: #{matched}]"
matched
end
return unless node.external?
node
.before_caller! do |input, *, _node_:, **|
input_prefix = INPUT_LABEL.rjust((_node_.depth * INDENT_SIZE) + INPUT_LABEL.length, BLANK)
puts "#{input_prefix} #{input}"
input
end
.after_caller! do |output, _node_:, **|
output_prefix = OUTPUT_LABEL.rjust((_node_.depth * INDENT_SIZE) + OUTPUT_LABEL.length, BLANK)
puts "#{output_prefix} #{output}"
output
end
end
end
loggable = Logging.method(:loggable)
tree = CallableTree::Node::Root.new.append(
Node::JSON::Parser.new.tap(&loggable).append(
Node::JSON::Scraper.new(type: :animals).tap(&loggable).verbosify,
Node::JSON::Scraper.new(type: :fruits).tap(&loggable).verbosify
),
Node::XML::Parser.new.tap(&loggable).append(
Node::XML::Scraper.new(type: :animals).tap(&loggable).verbosify,
Node::XML::Scraper.new(type: :fruits).tap(&loggable).verbosify
)
)
Dir.glob("#{__dir__}/../docs/*") do |file|
options = { foo: :bar }
pp tree.call(file, **options)
puts '---'
end