Class: TomDoc::TomDoc
- Inherits:
-
Object
- Object
- TomDoc::TomDoc
- Defined in:
- lib/tomdoc/tomdoc.rb
Instance Attribute Summary collapse
-
#raw ⇒ Object
Returns the value of attribute raw.
Class Method Summary collapse
Instance Method Summary collapse
- #args ⇒ Object
- #description ⇒ Object
- #examples ⇒ Object
-
#initialize(text) ⇒ TomDoc
constructor
A new instance of TomDoc.
- #raises ⇒ Object
- #returns ⇒ Object
- #sections ⇒ Object
- #to_s ⇒ Object
- #tomdoc ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
Constructor Details
#initialize(text) ⇒ TomDoc
Returns a new instance of TomDoc.
19 20 21 |
# File 'lib/tomdoc/tomdoc.rb', line 19 def initialize(text) @raw = text.to_s.strip end |
Instance Attribute Details
#raw ⇒ Object
Returns the value of attribute raw.
17 18 19 |
# File 'lib/tomdoc/tomdoc.rb', line 17 def raw @raw end |
Class Method Details
.valid?(text) ⇒ Boolean
27 28 29 |
# File 'lib/tomdoc/tomdoc.rb', line 27 def self.valid?(text) new(text).valid? end |
Instance Method Details
#args ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tomdoc/tomdoc.rb', line 65 def args args = [] last_indent = nil return args unless sections[1] sections[1].split("\n").each do |line| next if line.strip.empty? indent = line.scan(/^\s*/)[0].to_s.size if last_indent && indent > last_indent args.last.description += line.squeeze(" ") else param, desc = line.split(" - ") args << Arg.new(param.strip, desc.strip) if param && desc end last_indent = indent end args end |
#description ⇒ Object
61 62 63 |
# File 'lib/tomdoc/tomdoc.rb', line 61 def description sections.first end |
#examples ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/tomdoc/tomdoc.rb', line 88 def examples if tomdoc =~ /(\s*Examples\s*(.+?)\s*(?:Returns|Raises))/m $2.split("\n\n") else [] end end |
#raises ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/tomdoc/tomdoc.rb', line 116 def raises if tomdoc =~ /^\s*(Raises.+)/m lines = $1.split("\n") statements = [] lines.each do |line| if line =~ /^\s+/ statements.last << line.squeeze(' ') else statements << line end end statements else [] end end |
#returns ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tomdoc/tomdoc.rb', line 96 def returns if tomdoc =~ /^\s*(Returns.+)/m lines = $1.split("\n") statements = [] lines.each do |line| next if line =~ /^\s*Raises/ if line =~ /^\s+/ statements.last << line.squeeze(' ') else statements << line end end statements else [] end end |
#sections ⇒ Object
57 58 59 |
# File 'lib/tomdoc/tomdoc.rb', line 57 def sections tomdoc.split("\n\n") end |
#to_s ⇒ Object
23 24 25 |
# File 'lib/tomdoc/tomdoc.rb', line 23 def to_s @raw end |
#tomdoc ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/tomdoc/tomdoc.rb', line 49 def tomdoc clean = raw.split("\n").map do |line| line =~ /^(\s*# ?)/ ? line.sub($1, '') : nil end.compact.join("\n") clean end |
#valid? ⇒ Boolean
31 32 33 34 35 |
# File 'lib/tomdoc/tomdoc.rb', line 31 def valid? validate rescue InvalidTomDoc false end |
#validate ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tomdoc/tomdoc.rb', line 37 def validate if !raw.include?('Returns') raise InvalidTomDoc.new("No `Returns' statement.") end if tomdoc.split("\n\n").size < 2 raise InvalidTomDoc.new("No description section found.") end true end |