forked from foobara/http-command-connector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresenter.rb
More file actions
187 lines (171 loc) · 6.01 KB
/
presenter.rb
File metadata and controls
187 lines (171 loc) · 6.01 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
require "pathname"
require_relative "presenter/command"
require_relative "presenter/entity"
require_relative "presenter/error"
require_relative "presenter/model"
require_relative "presenter/organization"
require_relative "presenter/domain"
require_relative "presenter/processor"
require_relative "presenter/processor_class"
require_relative "presenter/root"
require_relative "presenter/type"
module Foobara
module CommandConnectors
class Http < CommandConnector
module Commands
class Help < Command
class Presenter
class << self
def for(manifest)
case manifest
when Manifest::RootManifest
Presenter::Root
when Manifest::Command
Presenter::Command
# TODO: do we need a dedicated presenter for this??
when Manifest::Entity, Manifest::DetachedEntity
Presenter::Entity
when Manifest::Model
Presenter::Model
when Manifest::Type
Presenter::Type
when Manifest::Error
Presenter::Error
when Manifest::Domain
Presenter::Domain
when Manifest::Organization
Presenter::Organization
when Manifest::Processor
Presenter::Processor
when Manifest::ProcessorClass
Presenter::ProcessorClass
when ErrorCollection
Presenter::RequestFailed
else
# :nocov:
raise "No presenter found for #{manifest.path}"
# :nocov:
end.new(manifest)
end
def template_symbol
Util.underscore(Util.non_full_name(self))
end
def template_path
template_path = File.join(
__dir__,
"templates",
"#{template_symbol}.html.erb"
)
Pathname.new(template_path).cleanpath.to_s
end
end
attr_accessor :manifest
def initialize(manifest)
self.manifest = manifest
end
def render_html_list(data)
html = ""
case data
when ::Hash
html << "<ul>"
data.each do |key, value|
html << "<li>#{key}"
html << render_html_list(value)
html << "</li>"
end
html << "</ul>"
when ::Array
data.each do |item|
html << render_html_list(item)
end
when Manifest::Attributes
html << "<ul>"
data.relevant_manifest.each_pair do |key, value|
if key.to_s == "type"
next
end
if key.to_s == "element_type_declarations"
key = :attributes
value = data.attribute_declarations
end
html << "<li>#{key}"
html << render_html_list(value)
html << "</li>"
end
html << "</ul>"
when Manifest::Array
html << "<ul>"
data.relevant_manifest.each_pair do |key, value|
next if key == :element_type_declaration
if key.to_s == "type"
value = root_manifest.lookup_path(key, value)
end
html << "<li>#{key}"
html << render_html_list(value)
html << "</li>"
end
html << render_html_list({ element_type: data.element_type })
html << "</ul>"
when Manifest::TypeDeclaration
manifest = data.relevant_manifest
html << "<ul>"
if manifest.is_a?(::Symbol)
html << "<li>"
html << foobara_reference_link(data.to_type)
html << "</li>"
else
data.relevant_manifest.each_pair do |key, value|
if key.to_s == "type"
value = root_manifest.lookup_path(key, value)
end
html << "<li>#{key}"
html << render_html_list(value)
html << "</li>"
end
end
html << "</ul>"
when Manifest::Type, Manifest::Command, Manifest::Error
html << "<ul>"
html << "<li>"
html << foobara_reference_link(data)
html << "</li>"
html << "</ul>"
when Manifest::PossibleError
html << render_html_list(data.error)
else
html << "<ul>"
html << "<li>#{data}</li>"
html << "</ul>"
end
html
end
def foobara_reference_link(manifest)
path = "/help/#{manifest.reference}"
"<a href=\"#{path}\">#{manifest.reference.split("::").last}</a>"
end
def root_manifest
@root_manifest ||= Manifest::RootManifest.new(manifest.root_manifest)
end
def template_path
self.class.template_path
end
def method_missing(method_name, *, &)
if manifest.respond_to?(method_name)
manifest.send(method_name, *, &)
else
# :nocov:
super
# :nocov:
end
end
def respond_to_missing?(method_name, include_private = false)
# :nocov:
manifest.respond_to?(method_name, include_private)
# :nocov:
end
end
end
end
end
end
end