Module: Betterdocs::Global

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

Class Method Summary collapse

Class Method Details

.actionsObject



109
110
111
# File 'lib/betterdocs/global.rb', line 109

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

.all_docsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/betterdocs/global.rb', line 91

def all_docs
  Dir[api_prefix.to_s + '/**/*_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

.api_base_urlObject



28
29
30
# File 'lib/betterdocs/global.rb', line 28

def api_base_url
  "#{api_protocol}://#{api_host}/#{api_prefix}"
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.



53
54
55
56
57
58
59
60
61
# File 'lib/betterdocs/global.rb', line 53

def asset(path, to: :root)
  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

.assetsObject



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

def assets
  @assets ||= {}
end

.configObject



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

def config
  if block_given?
    yield self
  else
    self
  end
end

.configure(&block) ⇒ Object



78
79
80
81
# File 'lib/betterdocs/global.rb', line 78

def configure(&block)
  instance_eval(&block)
  self
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.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/betterdocs/global.rb', line 65

def each_asset
  for path in assets.keys
    path = path.to_s
    if destination = assets[path]
      if destination == :root && output_directory
        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
end

.section(name) ⇒ Object



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

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

.sectionsObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/betterdocs/global.rb', line 113

def sections
  @sections and return @sections
  Dir.chdir Rails.root.join('app/controllers') 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



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

def sections_clear
  @sections = nil
  self
end

.url_for(options = {}) ⇒ Object



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

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

.url_helpersObject



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

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