Class: Fdoc::SchemaPresenter
Overview
An BasePresenter for a JSON Schema fragment. Like most JSON schema things, has a tendency to recurse.
Constant Summary
collapse
- FORMATTED_KEYS =
%w(
description
type
required
example
deprecated
default
format
enum
items
properties
)
Instance Attribute Summary
#options
Instance Method Summary
collapse
#css_path, #get_binding, #html_directory, #index_path, #render_erb, #render_markdown, #tag_with_anchor
Constructor Details
#initialize(schema, options) ⇒ SchemaPresenter
Returns a new instance of SchemaPresenter.
17
18
19
20
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 17
def initialize(schema, options)
super(options)
@schema = schema
end
|
Instance Method Details
#deprecated? ⇒ Boolean
120
121
122
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 120
def deprecated?
@schema["deprecated"]
end
|
#enum_html ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 128
def enum_html
enum = @schema["enum"]
return unless enum
list = enum.map do |e|
'<tt>%s</tt>' % e
end.join(", ")
html = StringIO.new
html << '<li>Enum: '
html << list
html << '</li>'
html.string
end
|
#example ⇒ Object
114
115
116
117
118
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 114
def example
return unless e = @schema["example"]
Fdoc::JsonPresenter.new(e)
end
|
110
111
112
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 110
def format
@schema["format"]
end
|
#items_html ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 143
def items_html
return unless items = @schema["items"]
html = ""
html << '<li>Items'
sub_options = options.merge(:nested => true)
if items.kind_of? Array
item.compact.each do |item|
html << self.class.new(item, sub_options).to_html
end
else
html << self.class.new(items, sub_options).to_html
end
html << '</li>'
html
end
|
#nested? ⇒ Boolean
26
27
28
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 26
def nested?
options[:nested]
end
|
#properties_html ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 163
def properties_html
return unless properties = @schema["properties"]
html = ""
properties.each do |key, property|
next if property.nil?
html << '<li>'
html << tag_with_anchor(
'span',
'<tt>%s</tt>' % key,
schema_slug(key, property)
)
html << self.class.new(property, options.merge(:nested => true)).to_html
html << '</li>'
end
html
end
|
#request? ⇒ Boolean
22
23
24
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 22
def request?
options[:request]
end
|
#required? ⇒ Boolean
124
125
126
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 124
def required?
@schema["required"] ? "yes" : "no"
end
|
#schema_slug(key, property) ⇒ Object
183
184
185
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 183
def schema_slug(key, property)
"#{key}-#{property.hash}"
end
|
#to_html ⇒ Object
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
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 30
def to_html
html = StringIO.new
html << '<span class="deprecated">Deprecated</span>' if deprecated?
html << '<div class="schema">'
html << render_markdown(@schema["description"])
html << '<ul>'
begin
html << '<li>Required: %s</li>' % required? if nested?
html << '<li>Type: %s</li>' % type if type
html << '<li>Format: %s</li>' % format if format
html << '<li>Example: %s</li>' % example.to_html if example
html << enum_html
(@schema.keys - FORMATTED_KEYS).each do |key|
html << '<li>%s: %s</li>' % [ key, @schema[key] ]
end
html << items_html
html << properties_html
end
html << '</ul>'
html << '</div>'
html.string
end
|
#to_markdown(prefix = "") ⇒ Object
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
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 61
def to_markdown(prefix = "")
md = StringIO.new
md << 'Deprecated' if deprecated?
md << "\n#{@schema["description"]}"
md << "\n#{prefix}* __Required__: #{required?}" if nested?
md << "\n#{prefix}* __Type__: #{type}" if type
md << "\n#{prefix}* __Format__: #{format}" if format
md << "\n#{prefix}* __Example__: <tt>#{example.to_markdown}</tt>" if example
md << "\n#{@schema['enum']}"
(@schema.keys - Fdoc::SchemaPresenter::FORMATTED_KEYS).each do |key|
md << "\n#{prefix}* %{key} %{@schema[key]}"
end
if items = @schema["items"]
md << "\n#{prefix}* Items"
if items.kind_of? Array
item.compact.each do |item|
md << Fdoc::SchemaPresenter.new(item, options.merge(:nested => true)).to_markdown(prefix + "\t")
end
else
md << Fdoc::SchemaPresenter.new(@schema["items"], options.merge(:nested => true)).to_markdown(prefix + "\t")
end
end
if properties = @schema["properties"]
properties.each do |key, property|
next if property.nil?
md << "\n#{prefix}* __#{key}__:"
md << Fdoc::SchemaPresenter.new(property, options.merge(:nested => true)).to_markdown(prefix + "\t")
end
end
md.string
end
|
#type ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/fdoc/presenters/schema_presenter.rb', line 93
def type
t = @schema["type"]
if t.kind_of? Array
types = t.map do |type|
if type.kind_of? Hash
'<li>%s</li>' % self.class.new(type, options).to_html
else
'<li>%s</li>' % type
end
end.join('')
'<ul>%s</ul>' % types
elsif t != "object"
t
end
end
|