Class: Documentize::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/documentize/builder.rb

Constant Summary collapse

REGEX =
{
  bad_do_block:  /\sdo.*?end/,
  do_block:      /\sdo[\s\|.*]/
}

Class Method Summary collapse

Class Method Details

.build_args(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/documentize/builder.rb', line 22

def self.build_args(args)
  str = ""
  str << "("
  args.each do |arg|
    str << "#{arg[:name]}"
    str << " = #{arg[:default]}" if arg[:default]
    str << ", " unless arg == args[-1]
  end
  str << ")"
end

.build_code(code, indent = 0) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/documentize/builder.rb', line 33

def self.build_code(code, indent = 0)
  str = ""
  str << build_docs(code) if code[:doc]
  str << "  "*indent
  str << "#{code[:type]} #{code[:name]}"
  str << build_args(code[:args]) if code[:args]
  str << "\n\n"

  code[:content].each do |item|
    if item.is_a?(Hash)

      str << build_code(item, indent+1)

    else

      indent -= 1 if item =~ /end/ && item !~ REGEX[:bad]

      str << "  "*(indent+1) + "#{item} \n"
      str << "\n" if item =~ /end/ && item !~ REGEX[:bad]

      indent += 1 if item =~ REGEX[:do_block] && item !~ REGEX[:bad]

    end
  end

  str << "  "*indent
  str << "end\n"
  indent -= 1
  str << "\n"
end

.build_docs(branch) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/documentize/builder.rb', line 9

def self.build_docs(branch)
  doc_str = ""
  doc_str << "# Public: #{branch[:desc]}\n#\n"
  if branch[:args]
    branch[:args].each do |arg|
      doc_str << "# #{arg[:name]} - #{arg[:type]}: #{arg[:desc]}\n"
    end
    doc_str << "# \n"
  end
  doc_str
end