Module: MountDoc::MountDocHelper

Included in:
MountDocController
Defined in:
app/helpers/mount_doc/mount_doc_helper.rb

Constant Summary collapse

HTTP_Methods =
[
  :GET,
  :POST,
  :PUT,
  :DELETE
].join("\n")

Instance Method Summary collapse

Instance Method Details

#actions(class_doc) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 67

def actions(class_doc)
  class_doc.meths.select{ |meth|
    meth.scope == :instance
  }.map{ |meth|
    { method: meth,
      routes: routes_for(@controller_name.gsub('::', '/'), meth.name)
    }
  }.select { |hash|
    hash[:routes].size > 0
  }
end

#controllersObject



51
52
53
54
55
56
57
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 51

def controllers
  @controllers ||= begin
    Dir[File.join(::Rails.application.root, 'app/controllers/**/*_controller.rb')].map { |cn|
      cn.sub(%r{_controller\.rb$}, '').sub(%r{^#{::Rails.application.root}/app/controllers/}, '')
    }
  end
end

#doc_dirObject



13
14
15
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 13

def doc_dir
  @doc_dir ||= File.expand_path(mount_doc_config.doc_file_path, ::Rails.application.root)
end

#filesObject



17
18
19
20
21
22
23
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 17

def files
  @files ||= Dir[File.join(doc_dir, '**/*')].reject { |fn|
    File.directory?(fn)
  }.map { |fn|
    fn.sub(%r{^#{doc_dir}/},'').force_encoding('utf-8')
  }
end

#markdown(text) ⇒ Object



88
89
90
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 88

def markdown(text)
  GitHub::Markup.render('tmp.markdown', text)
end

#markup(text) ⇒ Object



79
80
81
82
83
84
85
86
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 79

def markup(text)
  case MountDoc::Config.markup
  when :markdown
    markdown(text)
  else
    rdoc(text)
  end
end

#modelsObject



59
60
61
62
63
64
65
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 59

def models
  @models ||= begin
    Dir[File.join(::Rails.application.root, 'app/models/**/*.rb')].map { |cn|
      cn.sub(%r{\.rb$}, '').sub(%r{^#{::Rails.application.root}/app/models/}, '')
    }
  end
end

#mount_doc_configObject



9
10
11
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 9

def mount_doc_config
  MountDoc::Config
end

#rdoc(text) ⇒ Object



92
93
94
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 92

def rdoc(text)
  RDoc::Markup::ToHtml.new.convert(text)
end

#routesObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 31

def routes
  @routes ||= ::Rails.application.routes.routes.to_a.select { |route|
    controllers.include?(route.defaults[:controller].to_s)
  }.map {|route|
    method = HTTP_Methods.match(route.verb).to_s
    method = 'ANY' if method.empty?
    {
      method: method,
      path: route.path.spec
    }.merge(route.defaults)
  }
end

#routes_for(controller, action) ⇒ Object



44
45
46
47
48
49
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 44

def routes_for(controller, action)
  _routes = routes.dup.select{ |route|
    route[:controller].to_s.to_sym == controller.to_s.to_sym && route[:action].to_s.to_sym == action.to_s.to_sym
  }
  _routes
end

#syntaxhighlight(lang, text) ⇒ Object



96
97
98
99
100
101
# File 'app/helpers/mount_doc/mount_doc_helper.rb', line 96

def syntaxhighlight(lang, text)
  CodeRay.scan(text, lang.to_s.downcase.to_sym).html({
    css: :style,
    line_numbers: :inline
  })
end