Class: Shaf::ApiDoc::Task

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/shaf/api_doc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Task

Returns a new instance of Task.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
# File 'lib/shaf/api_doc.rb', line 10

def initialize
  yield self if block_given?
  validate_attributes!
  @document_class ||= Document
  define_tasks
end

Instance Attribute Details

#document_classObject

Returns the value of attribute document_class.



8
9
10
# File 'lib/shaf/api_doc.rb', line 8

def document_class
  @document_class
end

#html_output_dirObject

Returns the value of attribute html_output_dir.



8
9
10
# File 'lib/shaf/api_doc.rb', line 8

def html_output_dir
  @html_output_dir
end

#source_dirObject

Returns the value of attribute source_dir.



8
9
10
# File 'lib/shaf/api_doc.rb', line 8

def source_dir
  @source_dir
end

#yaml_output_dirObject

Returns the value of attribute yaml_output_dir.



8
9
10
# File 'lib/shaf/api_doc.rb', line 8

def yaml_output_dir
  @yaml_output_dir
end

Instance Method Details

#attribute(line) ⇒ Object



107
108
109
# File 'lib/shaf/api_doc.rb', line 107

def attribute(line)
  line[/\A\s*attribute[^s]\s*\(?\s*:(\w+)/, 1]
end

#comment(line) ⇒ Object



103
104
105
# File 'lib/shaf/api_doc.rb', line 103

def comment(line)
  line[/\A\s*#(.*)/, 1]
end

#curie(line) ⇒ Object



115
116
117
# File 'lib/shaf/api_doc.rb', line 115

def curie(line)
  line[/\A\s*curie\s*\(?\s*:'?([-\w]+)'?/, 1]
end

#define_tasksObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/shaf/api_doc.rb', line 23

def define_tasks
  namespace :doc do
    desc "Generate API documentation"
    task :generate do
      files = Dir.glob(File.join(source_dir, "*.rb"))
      files.each do |file|
        read_file file do |doc|
          next unless doc.has_enough_info?
          doc.write_html @html_output_dir
          doc.write_yaml @yaml_output_dir
        end
      end
    end

    desc "Remove generated documentation"
    task :clean do
      [
        Dir.glob(File.join(@yaml_output_dir, "*.yml")),
        Dir.glob(File.join(@html_output_dir, "*.html"))
      ].flatten.each do |file|
        File.unlink file
      end
    end
  end
end

#embed(line) ⇒ Object



119
120
121
# File 'lib/shaf/api_doc.rb', line 119

def embed(line)
  line[/\A\s*embed\s*\(?\s*:'?([-:\w]+)'?/, 1]
end

#empty_line?(line) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/shaf/api_doc.rb', line 87

def empty_line?(line)
  true if line[/\A[#\s*]*\Z/]
end


111
112
113
# File 'lib/shaf/api_doc.rb', line 111

def link(line)
  line[/\A\s*link\s*\(?\s*:'?([-:\w]+)'?/, 1]
end

#model(line) ⇒ Object



95
96
97
# File 'lib/shaf/api_doc.rb', line 95

def model(line)
  line[/\A\s*model\s*(?:::)?(\w+)/, 1]
end

#parse_line(line, doc, comment) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/shaf/api_doc.rb', line 69

def parse_line(line, doc, comment)
  if model = model(line)
    doc.model = model
  elsif serializer_class = serializer_class(line)
    doc.serializer_class = serializer_class
  elsif policy = policy(line)
    doc.policy = policy
  elsif attr = attribute(line)
    doc.attribute(attr, comment)
  elsif rel = link(line)
    doc.link(rel, comment)
  elsif rel = curie(line)
    doc.curie(rel, comment)
  elsif name = embed(line)
    doc.embedded(name, comment)
  end
end

#policy(line) ⇒ Object



99
100
101
# File 'lib/shaf/api_doc.rb', line 99

def policy(line)
  line[/\A\s*policy\s*(?:::)?(\w+)/, 1]
end

#read_file(file) {|doc| ... } ⇒ Object

Yields:

  • (doc)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/shaf/api_doc.rb', line 49

def read_file(file)
  doc = document_class.new
  comment = Comment.new

  File.readlines(file).each do |line|
    next if empty_line?(line)

    if c = comment(line)
      comment << c
      next
    end

    parse_line(line, doc, comment)
    comment = Comment.new
  end

  return doc unless block_given?
  yield doc
end

#serializer_class(line) ⇒ Object



91
92
93
# File 'lib/shaf/api_doc.rb', line 91

def serializer_class(line)
  line[/\A\s*class\s*(\w+)\Z/, 1]
end

#validate_attributes!Object



17
18
19
20
21
# File 'lib/shaf/api_doc.rb', line 17

def validate_attributes!
  raise "source_dir must be set!" unless source_dir
  raise "html_output_dir must be configured in ApiDocTask" unless html_output_dir
  raise "yaml_output_dir must be configured in ApiDocTask" unless yaml_output_dir
end