markdown_ruby_documentation 0.12.0 → 0.13.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a09494db3392aaca6d279d31c3023d6ffd20c508
4
- data.tar.gz: 4e8b9fba4a5b9cdc51c6452aa4659217f0549990
3
+ metadata.gz: 473a53d5f03184b6f4fd03121ff0c9234ff2e0ef
4
+ data.tar.gz: 68055dc5da34403f9284502c8805114ff1863833
5
5
  SHA512:
6
- metadata.gz: da975ebe6e44e8efa59e7104ea7693fc854cbd0a501efd601c3df94a572b4b76ab1d422cc6e1eadeef09d0039cbf88fa4787c3dcbc45a013fa9a99e43e4a16d2
7
- data.tar.gz: 0dc703252b6360aa7ca0d36b7736c5b148935ae21818613cc9a47e0794849ecc23155e5a4fbbac7edf888efb531f3c53d73dc95d0d3052e8c44474846b35e511
6
+ metadata.gz: eaf7ffa30459a699a9217e0be2277dec446265dc6a20ba177f7715f8ece4f5b6491cda7ed5959b633da079fc1180411f60e7994b100fd0174ff1aeecc9464833
7
+ data.tar.gz: c98fefe346906e267609a5c2188fc5fd1be500d0835734911bcca6f4232efd24022d31aec2447414295fd45b4943255e572915fb6eade4ff037d74cf2631d5ed
@@ -35,7 +35,7 @@ module MarkdownRubyDocumentation
35
35
  def constants
36
36
  subject.constants.each_with_object({}) do |v, const|
37
37
  c = subject.const_get(v)
38
- const[v] = self.class.format(c) unless c.class == Module || c.class == Class
38
+ const[v] = self.class.format(c) unless [Regexp, Module, Class].include?(c.class)
39
39
  end
40
40
  end
41
41
  end
@@ -1,6 +1,6 @@
1
1
  module MarkdownRubyDocumentation
2
2
  class Generate
3
- # @param [Class] subjects ruby classes to generate documentation from.
3
+ # @param [Array[Class, String]] subjects ruby classes to generate documentation from and file location.
4
4
  # @param [Module] erb_methods must contain #link_to_markdown and contain any additional methods for comment ERB
5
5
  # @param [Proc] output_object given name: and text: for use in saving the the files.
6
6
  # @param [String] load_path
@@ -18,16 +18,18 @@ module MarkdownRubyDocumentation
18
18
  erb_methods_class.extend TemplateParser::CommentMacros
19
19
  erb_methods_class.extend erb_methods
20
20
  TemplateParser::CommentMacros.include erb_methods
21
- left_padding(subjects)
22
- progressbar(subjects)
21
+ subject_classes = subjects.map { |h| h.fetch(:class) }
22
+ left_padding(subject_classes)
23
+ progressbar(subject_classes)
23
24
  progressbar.title = "Compiling Markdown".ljust(left_padding)
24
25
  batches = subjects.each_slice(parallel_config.fetch(:in_threads, 2))
25
26
  threads = []
26
27
  batches.each do |batch|
27
28
  threads << Thread.new(batch) do |(_batch)|
28
29
  Array[_batch].flatten.map do |subject|
29
- progressbar.title = subject.name.ljust(left_padding)
30
- Page.new(subject: subject,
30
+ progressbar.title = subject.fetch(:class).name.ljust(left_padding)
31
+ Page.new(subject_class: subject.fetch(:class),
32
+ subject_location: subject.fetch(:file_path).to_s,
31
33
  output_object: output_object,
32
34
  erb_methods_class: erb_methods_class,
33
35
  load_path: load_path).call.tap { progressbar.increment }
@@ -59,16 +61,18 @@ module MarkdownRubyDocumentation
59
61
  end
60
62
 
61
63
  class Page
62
- attr_reader :subject, :output_object, :erb_methods_class, :load_path
64
+ attr_reader :subject, :subject_location, :output_object, :erb_methods_class, :load_path
63
65
 
64
- def initialize(subject:,
66
+ def initialize(subject_class:,
67
+ subject_location:,
65
68
  methods: [],
66
69
  load_path:,
67
70
  output_object:,
68
71
  erb_methods_class:)
69
- initialize_methods(methods, subject)
72
+ initialize_methods(methods, subject_class, subject_location)
70
73
  @erb_methods_class = erb_methods_class
71
- @subject = subject
74
+ @subject = subject_class
75
+ @subject_location = subject_location
72
76
  methods = methods
73
77
  @methods = methods
74
78
  @load_path = load_path
@@ -84,21 +88,21 @@ module MarkdownRubyDocumentation
84
88
  end
85
89
 
86
90
  private
87
- def initialize_methods(methods, subject)
91
+ def initialize_methods(methods, subject, subject_location)
88
92
  if methods.empty?
89
- all_instance_and_class_methods(methods, subject)
93
+ all_instance_and_class_methods(methods, subject, subject_location)
90
94
  else
91
- methods.map! { |method| method.is_a?(Symbol) ? InstanceMethod.new("#{subject.name}##{method}", context: subject) : method }
95
+ methods.map! { |method| method.is_a?(Symbol) ? InstanceMethod.new("#{subject.name}##{method}", context: subject, file_path: subject_location) : method }
92
96
  end
93
97
  end
94
98
 
95
- def all_instance_and_class_methods(methods, subject)
99
+ def all_instance_and_class_methods(methods, subject, subject_location)
96
100
  native_instance_methods = (subject.instance_methods(false) - Object.instance_methods(false)).concat(subject.private_instance_methods(false) - Object.private_instance_methods(false))
97
101
  super_instance_methods = (subject.instance_methods(true) - Object.instance_methods(true)).concat(subject.private_instance_methods(true) - Object.private_instance_methods(true)) - native_instance_methods
98
102
  klass_m = subject.methods(false).concat(subject.private_methods(false)) - Object.methods
99
- methods.concat super_instance_methods.reverse.map { |method| InstanceMethod.new("#{subject.name}##{method}", context: subject, visibility: :super) }
100
- methods.concat native_instance_methods.map { |method| InstanceMethod.new("#{subject.name}##{method}", context: subject, visibility: :native) }
101
- methods.concat klass_m.map { |method| ClassMethod.new("#{subject.name}.#{method}", context: subject) }
103
+ methods.concat super_instance_methods.reverse.map { |method| InstanceMethod.new("#{subject.name}##{method}", context: subject, visibility: :super, file_path: subject_location) }
104
+ methods.concat native_instance_methods.map { |method| InstanceMethod.new("#{subject.name}##{method}", context: subject, visibility: :native, file_path: subject_location) }
105
+ methods.concat klass_m.map { |method| ClassMethod.new("#{subject.name}.#{method}", context: subject, file_path: subject_location) }
102
106
  end
103
107
 
104
108
  def methods_pipeline
@@ -10,13 +10,13 @@ module MarkdownRubyDocumentation
10
10
 
11
11
  def call(hash)
12
12
  hash.each do |name, values|
13
- hash[name][:text] = "#{values[:text]}\n\n[show on github](#{create_link(name: name, method_object: values[:method_object])})"
13
+ hash[name][:text] = "#{values[:text]}\n\n[show on github](#{create_link(name: name, method_object: values[:method_object], context: subject)})"
14
14
  end
15
15
  end
16
16
 
17
- def create_link(name: nil, method_object: nil)
17
+ def create_link(name: nil, method_object: nil, context: Kernel)
18
18
  if name && method_object.nil?
19
- method_object = Method.create("##{name}")
19
+ method_object = Method.create("##{name}", context: context)
20
20
  end
21
21
  MethodUrl.new(subject: subject, base_url: base_url, root: root, method_object: method_object).to_s
22
22
  end
@@ -66,7 +66,7 @@ module MarkdownRubyDocumentation
66
66
  end
67
67
 
68
68
  def to_s
69
- file, lineno = subject.public_send(method_object.type, method_object.name).source_location
69
+ file, lineno = method_object.source_location
70
70
  FileUrl.new(file_path: file, base_url: base_url, root: root).link(file, lineno)
71
71
  end
72
72
  end
@@ -2,12 +2,15 @@ module MarkdownRubyDocumentation
2
2
  class Method
3
3
  InvalidMethodReference = Class.new(StandardError)
4
4
  attr_reader :method_reference, :visibility
5
+ attr_accessor :file_path, :line_no
5
6
  protected :method_reference
6
7
 
7
- def initialize(method_reference, context: Kernel, visibility: :public)
8
+ def initialize(method_reference, context: Kernel, visibility: :public, file_path: nil, line_no: nil)
8
9
  @method_reference = method_reference.to_s
9
10
  @context = context
10
- @visibility = visibility
11
+ @visibility = visibility
12
+ @file_path = file_path
13
+ @line_no = line_no
11
14
  end
12
15
 
13
16
  # @param [String] method_reference
@@ -16,16 +19,16 @@ module MarkdownRubyDocumentation
16
19
  # "Constant.class_method_name" class method on a specific constant.
17
20
  # "SomeClass#instance_method_name" an instance method on a specific constant.
18
21
  # "#instance_method_name" an instance method in the current scope.
19
- def self.create(method_reference, null_method: false, context: Kernel, visibility: :public)
22
+ def self.create(method_reference, null_method: false, context: Kernel, visibility: :public, file_path: nil)
20
23
  return method_reference if method_reference.is_a?(Method)
21
24
  case method_reference
22
25
  when InstanceMethod
23
- InstanceMethod.new(method_reference, context: context, visibility: visibility)
26
+ InstanceMethod.new(method_reference, context: context, visibility: visibility, file_path: file_path)
24
27
  when ClassMethod
25
- ClassMethod.new(method_reference, context: context, visibility: visibility)
28
+ ClassMethod.new(method_reference, context: context, visibility: visibility, file_path: file_path)
26
29
  else
27
30
  if null_method
28
- NullMethod.new(method_reference, context: context, visibility: visibility)
31
+ NullMethod.new(method_reference, context: context, visibility: visibility, file_path: file_path)
29
32
  else
30
33
  raise InvalidMethodReference, "method_reference is formatted incorrectly: '#{method_reference}'"
31
34
  end
@@ -63,7 +66,7 @@ module MarkdownRubyDocumentation
63
66
  constant = method_reference.split(type_symbol).first
64
67
  begin
65
68
  constant.constantize
66
- rescue NameError => e
69
+ rescue NameError
67
70
  @context.const_get(constant)
68
71
  end
69
72
  end
@@ -97,6 +100,14 @@ module MarkdownRubyDocumentation
97
100
  context.public_send(type, name)
98
101
  end
99
102
 
103
+ def source_location
104
+ if file_path && line_no
105
+ [file_path, line_no]
106
+ else
107
+ context.public_send(type, name).source_location
108
+ end
109
+ end
110
+
100
111
  def type
101
112
  raise NotImplementedError
102
113
  end
@@ -41,12 +41,45 @@ module MarkdownRubyDocumentation
41
41
  end
42
42
 
43
43
  def ruby_class_meth_comment(method)
44
- method.context.public_send(method.type, method.name).comment
45
-
44
+ comment = method.context.public_send(method.type, method.name).comment
45
+ if comment.blank?
46
+ look_for_class_macro_comment(method)
47
+ else
48
+ comment
49
+ end
46
50
  rescue MethodSource::SourceNotFoundError => e
47
51
  raise e.class, "#{ method.context}#{method.type_symbol}#{method.name}, \n#{e.message}"
48
52
  end
49
53
 
54
+ CLASS_MACROS = [:attribute]
55
+ def source_location(file_path, name)
56
+ return unless file_path && name
57
+ found_match = nil
58
+ CLASS_MACROS.each do |macro|
59
+ if (ln = get_line_number(file_path.split(":").first, "#{macro} #{Regexp.escape(name.inspect)}"))
60
+ found_match = ln
61
+ end
62
+ end
63
+ [file_path, found_match] if found_match
64
+ end
65
+
66
+ def get_line_number(file, word)
67
+ return unless file && word
68
+ count = 0
69
+ file_or_result = File.open(file, "r") { |file| file.each_line { |line|
70
+ count += 1
71
+ return count if line =~ /^[\s]*#{word}/
72
+ }}
73
+ file_or_result.is_a?(File) ? nil : file_or_result
74
+ end
75
+
76
+ def look_for_class_macro_comment(method)
77
+ return "" unless (sl = source_location(method.file_path, method.name))
78
+ MethodSource.comment_helper(sl, method.name).tap do |comment|
79
+ method.line_no = sl[1] unless comment.blank?
80
+ end
81
+ end
82
+
50
83
  def ruby_class_meth_source_location(method)
51
84
  method.context.public_send(method.type, method.name).source_location
52
85
  end
@@ -1,3 +1,3 @@
1
1
  module MarkdownRubyDocumentation
2
- VERSION = "0.12.0"
2
+ VERSION = "0.13.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_ruby_documentation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dustin Zeisler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-23 00:00:00.000000000 Z
11
+ date: 2016-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: method_source