Class: Fdoc::SchemaPresenter

Inherits:
HtmlPresenter show all
Defined in:
lib/fdoc/presenters/schema_presenter.rb

Overview

An HtmlPresenter 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

Attributes inherited from HtmlPresenter

#options

Instance Method Summary collapse

Methods inherited from HtmlPresenter

#css_path, #html_directory, #index_path, #render_erb, #render_json, #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

Returns:

  • (Boolean)


88
89
90
# File 'lib/fdoc/presenters/schema_presenter.rb', line 88

def deprecated?
  @schema["deprecated"]
end

#enum_htmlObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fdoc/presenters/schema_presenter.rb', line 96

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

#exampleObject



82
83
84
85
86
# File 'lib/fdoc/presenters/schema_presenter.rb', line 82

def example
  return unless e = @schema["example"]

  render_json(e)
end

#formatObject



78
79
80
# File 'lib/fdoc/presenters/schema_presenter.rb', line 78

def format
  @schema["format"]
end

#items_htmlObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fdoc/presenters/schema_presenter.rb', line 111

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

Returns:

  • (Boolean)


26
27
28
# File 'lib/fdoc/presenters/schema_presenter.rb', line 26

def nested?
  options[:nested]
end

#properties_htmlObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/fdoc/presenters/schema_presenter.rb', line 131

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/fdoc/presenters/schema_presenter.rb', line 22

def request?
  options[:request]
end

#required?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/fdoc/presenters/schema_presenter.rb', line 92

def required?
  @schema["required"] ? "yes" : "no"
end

#schema_slug(key, property) ⇒ Object



151
152
153
# File 'lib/fdoc/presenters/schema_presenter.rb', line 151

def schema_slug(key, property)
  "#{key}-#{property.hash}"
end

#to_htmlObject



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 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

#typeObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fdoc/presenters/schema_presenter.rb', line 61

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