Class: Billy::Documentation

Inherits:
Object
  • Object
show all
Defined in:
lib/billygoat/documentation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#methodObject

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

#modeObject

Returns the value of attribute mode.



12
13
14
# File 'lib/billygoat/documentation.rb', line 12

def mode
  @mode
end

#ownerObject



45
46
47
# File 'lib/billygoat/documentation.rb', line 45

def owner
  @owner ||= self.method.owner.name.downcase
end

Instance Method Details

#block_parametersObject



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

#buildObject



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_markdownObject



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_nameObject



33
34
35
# File 'lib/billygoat/documentation.rb', line 33

def file_name
  @file_name ||= self.method.source_location.first
end

#inspectObject



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_markdownObject



28
29
30
31
# File 'lib/billygoat/documentation.rb', line 28

def inspect_markdown
  return unless file_name && line_number
  build_markdown
end

#line_numberObject



37
38
39
# File 'lib/billygoat/documentation.rb', line 37

def line_number
  @line_number ||= self.method.source_location.last
end

#linesObject



98
99
100
# File 'lib/billygoat/documentation.rb', line 98

def lines
  source[range].map(&unindent)
end

#nameObject



49
50
51
# File 'lib/billygoat/documentation.rb', line 49

def name
  @name ||= self.method.name.downcase
end

#optional_parametersObject



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

#parametersObject



41
42
43
# File 'lib/billygoat/documentation.rb', line 41

def parameters
  @parameters ||= self.method.parameters
end

#rangeObject



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_parametersObject



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

#sourceObject



81
82
83
# File 'lib/billygoat/documentation.rb', line 81

def source
  source_file.split("\n")
end

#source_fileObject



77
78
79
# File 'lib/billygoat/documentation.rb', line 77

def source_file
  File.read(file_name)
end

#unindentObject



94
95
96
# File 'lib/billygoat/documentation.rb', line 94

def unindent
  ->(line) { line.gsub(/^\s*/, '') }
end

#variable_parametersObject



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