Module: Betterdocs::Global

Extended by:
Tins::DSLAccessor
Defined in:
lib/betterdocs/global.rb

Class Method Summary collapse

Class Method Details

.actionsObject



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

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

.all_docsObject



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

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



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

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

.api_url(url = nil) ⇒ Object



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

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

.api_url_optionsObject



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

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.



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

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



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

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

.assets(hash = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/betterdocs/global.rb', line 80

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

.assets=(hash) ⇒ Object



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

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

.configObject



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

def config
  if block_given?
    yield self
  else
    self
  end
end

.configuration_fileObject



118
119
120
121
# File 'lib/betterdocs/global.rb', line 118

def configuration_file
  complex_config.betterdocs
rescue ComplexConfig::ConfigurationFileMissing
end

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



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

def configure(config = configuration_file, &block)
  Betterdocs.rails.env.development? or return
  config and configure_via_yaml(config)
  block  and instance_eval(&block)
  self
end

.configure_via_yaml(config) ⇒ Object



123
124
125
126
127
# File 'lib/betterdocs/global.rb', line 123

def configure_via_yaml(config)
  config.each do |name, value|
    __send__(name, value.dup)
  end
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.



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

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



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

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

.section(name) ⇒ Object



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

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

.sectionsObject



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

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



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

def sections_clear
  @sections = nil
  self
end

.url_for(options = {}) ⇒ Object



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

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

.url_helpersObject



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

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