Module: Betterdocs::Global

Extended by:
ComplexConfig::Provider::Shortcuts, Tins::DSLAccessor
Defined in:
lib/betterdocs/global.rb

Class Method Summary collapse

Class Method Details

.actionsObject



165
166
167
# File 'lib/betterdocs/global.rb', line 165

def actions
  all_docs.reduce([]) { |a, d| a.concat(d.actions) }
end

.all_docsObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/betterdocs/global.rb', line 145

def all_docs
  Dir.chdir controllers_dir do
    Dir["#{api_prefix}/**/*_controller.rb"].each_with_object([]) do |cf, all|
      controller_name = cf.sub(/\.rb$/, '').camelcase
      controller =
        begin
          controller_name.constantize
        rescue NameError => e
          STDERR.puts "Skipping #{cf.inspect}, #{e.class}: #{e}"
          next
        end
      if docs = controller.ask_and_send(:docs)
        all << docs
      else
        STDERR.puts "Skipping #{cf.inspect}, #{controller_name.inspect} doesn't respond to :docs method"
      end
    end
  end
end

.api_base_urlObject



60
61
62
# File 'lib/betterdocs/global.rb', line 60

def api_base_url
  "#{api_protocol}://#{api_host}/#{api_prefix}"
end

.api_url(url = nil) ⇒ Object



46
47
48
# File 'lib/betterdocs/global.rb', line 46

def api_url(url = nil)
  handle_url(:api, url)
end

.api_url_optionsObject



64
65
66
# File 'lib/betterdocs/global.rb', line 64

def api_url_options
  { protocol: api_protocol, host: api_host, format: api_default_format }
end

.asset(path, to: :root) ⇒ Object

Defines an asset for the file at path. If to was given it will be copied to this path (it includes the basename) below templates_directory in the output, otherwise it will be copied directly to templates_directory.



96
97
98
99
100
101
102
103
104
105
# File 'lib/betterdocs/global.rb', line 96

def asset(path, to: :root)
  @assets ||= {}
  if destination = to.ask_and_send(:to_str)
    @assets[path.to_s] = destination
  elsif to == :root
    @assets[path.to_s] = to
  else
    raise ArgumentError, "keyword argument to needs to be a string or :root"
  end
end

.asset_url(url = nil) ⇒ Object



54
55
56
# File 'lib/betterdocs/global.rb', line 54

def asset_url(url = nil)
  handle_url(:asset, url)
end

.assets(hash = nil) ⇒ Object



86
87
88
89
90
# File 'lib/betterdocs/global.rb', line 86

def assets(hash = nil)
  @assets ||= {}
  hash&.each { |path, to| asset path, to: to }
  @assets
end

.assets=(hash) ⇒ Object



81
82
83
84
# File 'lib/betterdocs/global.rb', line 81

def assets=(hash)
  @assets&.clear
  assets(hash)
end

.configObject



137
138
139
140
141
142
143
# File 'lib/betterdocs/global.rb', line 137

def config
  if block_given?
    yield self
  else
    self
  end
end

.configuration_fileObject



120
121
122
123
# File 'lib/betterdocs/global.rb', line 120

def configuration_file
  cc.betterdocs
rescue ComplexConfig::ConfigurationFileMissing
end

.configure(config = configuration_file, &block) ⇒ Object



131
132
133
134
135
# File 'lib/betterdocs/global.rb', line 131

def configure(config = configuration_file, &block)
  config and configure_via_yaml(config)
  block  and instance_eval(&block)
  self
end

.configure_via_yaml(config) ⇒ Object



125
126
127
128
129
# File 'lib/betterdocs/global.rb', line 125

def configure_via_yaml(config)
  config.each do |name, value|
    __send__(name, value.dup)
  end
end

.default_sanitize(code = nil) ⇒ Object



76
77
78
79
# File 'lib/betterdocs/global.rb', line 76

def default_sanitize(code = nil)
  code and @default_sanitize = eval(code)
  @default_sanitize
end

.each_assetObject

Maps the assets original source path to its destination path in the output by yielding to every asset’s source/destination pair.



109
110
111
112
113
114
115
116
117
118
# File 'lib/betterdocs/global.rb', line 109

def each_asset
  for (path, destination) in assets
    path = path.to_s
    if destination == :root
      yield path, File.join(output_directory.to_s, File.basename(path))
    else
      yield path, File.join(output_directory.to_s, destination.to_str)
    end
  end
end

.platform_url(url = nil) ⇒ Object



34
35
36
# File 'lib/betterdocs/global.rb', line 34

def platform_url(url = nil)
  handle_url(:platform, url)
end

.section(name) ⇒ Object



185
186
187
# File 'lib/betterdocs/global.rb', line 185

def section(name)
  sections[name] if sections.key?(name)
end

.sectionsObject



169
170
171
172
173
174
175
176
177
178
# File 'lib/betterdocs/global.rb', line 169

def sections
  @sections and return @sections
  Dir.chdir controllers_dir do
    actions.each_with_object(@sections = {}) do |action, sections|
      sections[action.section] ||= Section.new(action.section)
      sections[action.section] << action
    end
  end
  @sections.freeze
end

.sections_clearObject



180
181
182
183
# File 'lib/betterdocs/global.rb', line 180

def sections_clear
  @sections = nil
  self
end

.url_for(options = {}) ⇒ Object



193
194
195
196
# File 'lib/betterdocs/global.rb', line 193

def url_for(options = {})
  Betterdocs.rails.application.routes.url_for(
    options | Betterdocs::Global.config.api_url_options)
end

.url_helpersObject



189
190
191
# File 'lib/betterdocs/global.rb', line 189

def url_helpers
  Betterdocs.rails.application.routes.url_helpers
end