Class: Billy::Documentation
- Inherits:
-
Object
- Object
- Billy::Documentation
- Defined in:
- lib/billygoat/documentation.rb
Instance Attribute Summary collapse
-
#method ⇒ Object
Documentation includes the method definition and any commented lines preceding the method definition.
-
#mode ⇒ Object
Returns the value of attribute mode.
- #owner ⇒ Object
Instance Method Summary collapse
- #block_parameters ⇒ Object
- #build ⇒ Object
- #build_markdown ⇒ Object
- #file_name ⇒ Object
-
#initialize(args) ⇒ Documentation
constructor
A new instance of Documentation.
- #inspect ⇒ Object
- #inspect_markdown ⇒ Object
- #line_number ⇒ Object
- #lines ⇒ Object
- #name ⇒ Object
- #optional_parameters ⇒ Object
- #parameters ⇒ Object
- #range ⇒ Object
- #required_parameters ⇒ Object
- #source ⇒ Object
- #source_file ⇒ Object
- #unindent ⇒ Object
- #variable_parameters ⇒ Object
Constructor Details
#initialize(args) ⇒ Documentation
Returns a new instance of Documentation.
14 15 16 |
# File 'lib/billygoat/documentation.rb', line 14 def initialize(args) args.each { |k,v| public_send("#{k}=",v) } end |
Instance Attribute Details
#method ⇒ Object
Documentation includes the method definition and any commented lines preceding the method definition
10 11 12 |
# File 'lib/billygoat/documentation.rb', line 10 def method @method end |
#mode ⇒ Object
Returns the value of attribute mode.
12 13 14 |
# File 'lib/billygoat/documentation.rb', line 12 def mode @mode end |
#owner ⇒ Object
45 46 47 |
# File 'lib/billygoat/documentation.rb', line 45 def owner @owner ||= self.method.owner.name.downcase end |
Instance Method Details
#block_parameters ⇒ Object
71 72 73 74 75 |
# File 'lib/billygoat/documentation.rb', line 71 def block_parameters parameters .select{ |pair| pair.first == :block } .map{ |pair| pair.last} end |
#build ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/billygoat/documentation.rb', line 102 def build doc = lines doc[-1] = "goat #{owner} #{name} " required_parameters.each do |parameter| doc[-1] << "[#{parameter}] " end optional_parameters.each do |parameter| doc[-1] << "(#{parameter}) " end variable_parameters.each do |parameter| doc[-1] << "(*#{parameter}) " end block_parameters.each do |parameter| doc[-1] << "{&#{parameter}} " end doc << "\n" doc end |
#build_markdown ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/billygoat/documentation.rb', line 121 def build_markdown doc = build doc.pop doc.map! { |line| line.match(/^\s*#/) ? " #{line}" : line } doc.unshift "### #{doc.pop}" doc << "\n" doc.map! { |line| line.gsub(/(\[)/,'\[').gsub(/(\])/,'\]') } doc end |
#file_name ⇒ Object
33 34 35 |
# File 'lib/billygoat/documentation.rb', line 33 def file_name @file_name ||= self.method.source_location.first end |
#inspect ⇒ Object
18 19 20 21 22 23 24 25 26 |
# File 'lib/billygoat/documentation.rb', line 18 def inspect return unless file_name && line_number case mode when :markdown build_markdown else build end end |
#inspect_markdown ⇒ Object
28 29 30 31 |
# File 'lib/billygoat/documentation.rb', line 28 def inspect_markdown return unless file_name && line_number build_markdown end |
#line_number ⇒ Object
37 38 39 |
# File 'lib/billygoat/documentation.rb', line 37 def line_number @line_number ||= self.method.source_location.last end |
#lines ⇒ Object
98 99 100 |
# File 'lib/billygoat/documentation.rb', line 98 def lines source[range].map(&unindent) end |
#name ⇒ Object
49 50 51 |
# File 'lib/billygoat/documentation.rb', line 49 def name @name ||= self.method.name.downcase end |
#optional_parameters ⇒ Object
59 60 61 62 63 |
# File 'lib/billygoat/documentation.rb', line 59 def optional_parameters parameters .select{ |pair| pair.first == :opt } .map{ |pair| pair.last} end |
#parameters ⇒ Object
41 42 43 |
# File 'lib/billygoat/documentation.rb', line 41 def parameters @parameters ||= self.method.parameters end |
#range ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/billygoat/documentation.rb', line 85 def range head = tail = line_number - 1 tail.downto(0).each do break unless source[head-1].match(/^\s*#/) head -= 1 end (head..tail) end |
#required_parameters ⇒ Object
53 54 55 56 57 |
# File 'lib/billygoat/documentation.rb', line 53 def required_parameters parameters .select{ |pair| pair.first == :req } .map{ |pair| pair.last} end |
#source ⇒ Object
81 82 83 |
# File 'lib/billygoat/documentation.rb', line 81 def source source_file.split("\n") end |
#source_file ⇒ Object
77 78 79 |
# File 'lib/billygoat/documentation.rb', line 77 def source_file File.read(file_name) end |
#unindent ⇒ Object
94 95 96 |
# File 'lib/billygoat/documentation.rb', line 94 def unindent ->(line) { line.gsub(/^\s*/, '') } end |
#variable_parameters ⇒ Object
65 66 67 68 69 |
# File 'lib/billygoat/documentation.rb', line 65 def variable_parameters parameters .select{ |pair| pair.first == :rest } .map{ |pair| pair.last} end |