Class: Brainstem::ApiDocs::Presenter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Concerns::Formattable, Concerns::Optional
Defined in:
lib/brainstem/api_docs/presenter.rb

Instance Attribute Summary collapse

Attributes included from Concerns::Formattable

#formatters

Instance Method Summary collapse

Methods included from Concerns::Formattable

#formatted_as, #formatter_type

Constructor Details

#initialize(atlas, options = {}) {|_self| ... } ⇒ Presenter

Returns a new instance of Presenter.

Yields:

  • (_self)

Yield Parameters:



41
42
43
44
45
46
47
48
# File 'lib/brainstem/api_docs/presenter.rb', line 41

def initialize(atlas, options = {})
  self.atlas = atlas
  self.document_empty_associations = Brainstem::ApiDocs.document_empty_presenter_associations
  self.document_empty_filters      = Brainstem::ApiDocs.document_empty_presenter_filters

  super options
  yield self if block_given?
end

Instance Attribute Details

#atlasObject

Returns the value of attribute atlas.



65
66
67
# File 'lib/brainstem/api_docs/presenter.rb', line 65

def atlas
  @atlas
end

#constObject

Returns the value of attribute const.



29
30
31
# File 'lib/brainstem/api_docs/presenter.rb', line 29

def const
  @const
end

#document_empty_associationsObject Also known as: document_empty_associations?

Returns the value of attribute document_empty_associations.



29
30
31
# File 'lib/brainstem/api_docs/presenter.rb', line 29

def document_empty_associations
  @document_empty_associations
end

#document_empty_filtersObject Also known as: document_empty_filters?

Returns the value of attribute document_empty_filters.



29
30
31
# File 'lib/brainstem/api_docs/presenter.rb', line 29

def document_empty_filters
  @document_empty_filters
end


78
79
80
# File 'lib/brainstem/api_docs/presenter.rb', line 78

def filename_link_pattern
  @filename_link_pattern ||= Brainstem::ApiDocs.presenter_filename_link_pattern
end

#filename_patternObject



73
74
75
# File 'lib/brainstem/api_docs/presenter.rb', line 73

def filename_pattern
  @filename_pattern ||= Brainstem::ApiDocs.presenter_filename_pattern
end

#target_classObject

Returns the value of attribute target_class.



29
30
31
# File 'lib/brainstem/api_docs/presenter.rb', line 29

def target_class
  @target_class
end

Instance Method Details

#brainstem_keysObject



97
98
99
# File 'lib/brainstem/api_docs/presenter.rb', line 97

def brainstem_keys
  const.possible_brainstem_keys.to_a.sort
end

#conditionalsObject



179
180
181
# File 'lib/brainstem/api_docs/presenter.rb', line 179

def conditionals
  configuration[:conditionals]
end

#contextual_documentation(key) ⇒ Object

Returns a key if it exists and is documentable.



202
203
204
205
206
# File 'lib/brainstem/api_docs/presenter.rb', line 202

def contextual_documentation(key)
  configuration.has_key?(key) &&
    !configuration[key][:nodoc] &&
    configuration[key][:info]
end

#default_sort_directionObject



194
195
196
# File 'lib/brainstem/api_docs/presenter.rb', line 194

def default_sort_direction
  @default_sort_direction ||= (default_sort_order.split(":")[1] || nil)
end

#default_sort_fieldObject



189
190
191
# File 'lib/brainstem/api_docs/presenter.rb', line 189

def default_sort_field
  @default_sort_field ||= (default_sort_order.split(":")[0] || nil)
end

#default_sort_orderObject



184
185
186
# File 'lib/brainstem/api_docs/presenter.rb', line 184

def default_sort_order
  configuration[:default_sort_order] || ""
end

#descriptionObject



102
103
104
# File 'lib/brainstem/api_docs/presenter.rb', line 102

def description
  contextual_documentation(:description) || ""
end

#documentable_association?(_, association) ⇒ Bool

Returns whether this association should be documented based on nodoc and empty description.

Returns:

  • (Bool)

    document this association?



170
171
172
173
174
175
176
# File 'lib/brainstem/api_docs/presenter.rb', line 170

def documentable_association?(_, association)
  !association.options[:nodoc] && # not marked nodoc and
    (
      document_empty_associations? || # document empty associations or
      !(association.description.nil? || association.description.empty?) # has description
    )
end

#documentable_filter?(_, filter) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
# File 'lib/brainstem/api_docs/presenter.rb', line 132

def documentable_filter?(_, filter)
  !filter[:nodoc] &&
    (
      document_empty_filters? || # document empty filters or
      !(filter[:info] || "").empty? # has info string
    )
end

#extensionObject



68
69
70
# File 'lib/brainstem/api_docs/presenter.rb', line 68

def extension
  @extension ||= Brainstem::ApiDocs.output_extension
end

#invalid_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/brainstem/api_docs/presenter.rb', line 115

def invalid_field?(field)
  field.options[:nodoc]
end


154
155
156
157
158
159
160
161
# File 'lib/brainstem/api_docs/presenter.rb', line 154

def link_for_association(association)
  if (associated_presenter = find_by_class(association.target_class)) &&
      !associated_presenter.nodoc?
    relative_path_to_presenter(associated_presenter, :markdown)
  else
    nil
  end
end

#nested_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/brainstem/api_docs/presenter.rb', line 120

def nested_field?(field)
  field.respond_to?(:configuration)
end

#nodoc?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/brainstem/api_docs/presenter.rb', line 87

def nodoc?
  configuration[:nodoc]
end

#relative_path_to_presenter(presenter, format) ⇒ Object

Returns the relative path between this presenter and another given presenter.



213
214
215
216
217
218
# File 'lib/brainstem/api_docs/presenter.rb', line 213

def relative_path_to_presenter(presenter, format)
  my_path        = Pathname.new(File.dirname(suggested_filename_link(format)))
  presenter_path = Pathname.new(presenter.suggested_filename_link(format))

  presenter_path.relative_path_from(my_path).to_s
end

#suggested_filename(format) ⇒ Object



51
52
53
54
55
# File 'lib/brainstem/api_docs/presenter.rb', line 51

def suggested_filename(format)
  filename_pattern
    .gsub('{{name}}', target_class.to_s.underscore)
    .gsub('{{extension}}', extension)
end


58
59
60
61
62
# File 'lib/brainstem/api_docs/presenter.rb', line 58

def suggested_filename_link(format)
  filename_link_pattern
    .gsub('{{name}}', target_class.to_s.underscore)
    .gsub('{{extension}}', extension)
end

#titleObject



92
93
94
# File 'lib/brainstem/api_docs/presenter.rb', line 92

def title
  contextual_documentation(:title) || const.to_s.demodulize
end

#valid_associationsObject



146
147
148
149
150
# File 'lib/brainstem/api_docs/presenter.rb', line 146

def valid_associations
  configuration[:associations]
    .to_h
    .keep_if(&method(:documentable_association?))
end

#valid_fields(fields = ) ⇒ Object Also known as: valid_fields_in



107
108
109
110
111
# File 'lib/brainstem/api_docs/presenter.rb', line 107

def valid_fields(fields = configuration[:fields])
  fields.to_h.reject do |_, field|
    invalid_field?(field) || (nested_field?(field) && valid_fields_in(field).none?)
  end
end

#valid_filtersObject



125
126
127
128
129
# File 'lib/brainstem/api_docs/presenter.rb', line 125

def valid_filters
  configuration[:filters]
    .to_h
    .keep_if(&method(:documentable_filter?))
end

#valid_optionsObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/brainstem/api_docs/presenter.rb', line 18

def valid_options
  super | [
    :const,
    :target_class,
    :filename_pattern,
    :filename_link_pattern,
    :document_empty_associations,
    :document_empty_filters
  ]
end

#valid_sort_ordersObject



141
142
143
# File 'lib/brainstem/api_docs/presenter.rb', line 141

def valid_sort_orders
  configuration[:sort_orders].to_h.reject {|k, v| v[:nodoc] }
end