Class: Lurker::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
$ref
)
Instance Attribute Summary
#options
Instance Method Summary
collapse
#css_path, #get_binding, #html_directory, #index_path, #render, #render_erb, #tag_with_anchor, #url_base_path
Constructor Details
#initialize(schema, options) ⇒ SchemaPresenter
Returns a new instance of SchemaPresenter.
18
19
20
21
22
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 18
def initialize(schema, options)
options[:nested] ||= 0
super(options)
@schema = schema
end
|
Instance Method Details
#deprecated? ⇒ Boolean
90
91
92
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 90
def deprecated?
@schema["deprecated"]
end
|
#enum_html ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 98
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
84
85
86
87
88
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 84
def example
return unless e = @schema["example"]
Lurker::JsonPresenter.new(e)
end
|
80
81
82
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 80
def format
@schema["format"]
end
|
#items_html ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 113
def items_html
return unless items = @schema["items"]
html = ""
html << '<li>Items'
sub_options = options.merge(:nested => options[:nested] + 1)
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
28
29
30
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 28
def nested?
options[:nested] > 0
end
|
#properties_html ⇒ Object
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
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 133
def properties_html
properties = if (props = @schema["properties"]).present?
props
elsif (ref_path = @schema["$ref"]).present?
ref = Lurker::RefObject.new(ref_path, options[:root_path])
options[:root_path] = options[:root_path].merge(ref_path.sub(/#[^#]*?$/, ''))
ref.schema["properties"]
else
nil
end
return unless 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 => options[:nested] + 1)).to_html
html << '</li>'
end
html
end
|
#request? ⇒ Boolean
24
25
26
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 24
def request?
options[:request]
end
|
#required? ⇒ Boolean
94
95
96
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 94
def required?
@schema["required"] ? "yes" : "no"
end
|
#schema_slug(key, property) ⇒ Object
163
164
165
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 163
def schema_slug(key, property)
"#{key}-#{options[:nested]}-#{property.hash}"
end
|
#to_html ⇒ Object
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
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 32
def to_html
html = StringIO.new
html << '<span class="deprecated">Deprecated</span>' if deprecated?
html << '<div class="schema">'
html << @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
|
#type ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/lurker/presenters/schema_presenter.rb', line 63
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
|