Class: ApiClass

Inherits:
Object
  • Object
show all
Defined in:
lib/tools/api_doc_generator.rb

Direct Known Subclasses

ApiDoc

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name) ⇒ ApiClass

Returns a new instance of ApiClass.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tools/api_doc_generator.rb', line 45

def initialize(class_name)
  @class_name = class_name
  @yard_class = @@class_hash[class_name]
  if yard_class.nil?
    raise "class_hash[#{class_name}] is Nil"
  end
  @mhash = categorize_members(@yard_class)
  if @yard_class.path.include?('::')
    ruby_module = Object::const_get(yard_class.path.split(/::/)[0])
    @ruby_class = ruby_module.const_get(yard_class.name)
  else
    @ruby_class = Object::const_get(yard_class.path)
  end

  if @ruby_class.respond_to?(:tag_name)
    @xml_tag = "<#{ruby_class.tag_name}>"
  else
    @xml_tag = '-'
  end
  docstring_all = confluence_translate(yard_class.docstring.all.split(/@/)[0])
  docstring_parts = docstring_all.split(/h4. Data Model\n/)
  @description = docstring_parts[0]
  @model = docstring_parts.size > 1 ? docstring_parts[1] : nil
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



29
30
31
# File 'lib/tools/api_doc_generator.rb', line 29

def class_name
  @class_name
end

#descriptionObject

Returns the value of attribute description.



34
35
36
# File 'lib/tools/api_doc_generator.rb', line 34

def description
  @description
end

#mhashObject

Returns the value of attribute mhash.



31
32
33
# File 'lib/tools/api_doc_generator.rb', line 31

def mhash
  @mhash
end

#modelObject

Returns the value of attribute model.



35
36
37
# File 'lib/tools/api_doc_generator.rb', line 35

def model
  @model
end

#ruby_classObject

Returns the value of attribute ruby_class.



32
33
34
# File 'lib/tools/api_doc_generator.rb', line 32

def ruby_class
  @ruby_class
end

#xml_tagObject

Returns the value of attribute xml_tag.



33
34
35
# File 'lib/tools/api_doc_generator.rb', line 33

def xml_tag
  @xml_tag
end

#yard_classObject

Returns the value of attribute yard_class.



30
31
32
# File 'lib/tools/api_doc_generator.rb', line 30

def yard_class
  @yard_class
end

Class Method Details

.class_hash=(class_hash) ⇒ Object



37
38
39
# File 'lib/tools/api_doc_generator.rb', line 37

def self.class_hash=(class_hash)
  @@class_hash = class_hash
end

.rootpath=(rootpath) ⇒ Object



41
42
43
# File 'lib/tools/api_doc_generator.rb', line 41

def self.rootpath=(rootpath)
  @@rootpath = rootpath
end

Instance Method Details

#categorize_members(yard_class) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/tools/api_doc_generator.rb', line 70

def categorize_members(yard_class)
  mhash = {
      :class_attributes => OrderedHash.new,
      :instance_attributes => OrderedHash.new,
      :class_methods => Array.new,
      :instance_methods => Array.new
  }
  yard_class.children.each do |member|
    attr_symbol = member.name.to_s.gsub(/=$/, '').to_sym
    if member.name == :initialize
      mhash[:constructor] = member
    elsif yard_class.class_attributes[attr_symbol]
      mhash[:class_attributes][attr_symbol] = yard_class.class_attributes[attr_symbol]
    elsif yard_class.instance_attributes[attr_symbol]
      mhash[:instance_attributes][attr_symbol] = yard_class.instance_attributes[attr_symbol]
    elsif member.scope == :class
      mhash[:class_methods] << member
    elsif member.scope == :instance
      mhash[:instance_methods] << member
    end
  end
  mhash
end

#confluence_translate(input) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/tools/api_doc_generator.rb', line 256

def confluence_translate(input)
  map = OrderedHash.new
  map[/\|/] = "\\|"
  map[/====/] = "h4. "
  map[/\*/] = "\\*"
  map[/\n\s{6}\\\*\s/] = "\n****\s"
  map[/\n\s{4}\\\*\s/] = "\n***\s"
  map[/\n\s{2}\\\*\s/] = "\n**\s"
  map[/\n\\\*\s/] = "\n*\s"
  map[/<[\/]*b>/] = "*"
  map[/<[\/]*i>/] = "_"
  map[/\[/] = "\\["
  map[/\{#/] = "[#"
  map[/\{http/] = "[http"
  map[/\{/] = "[#"
  map[/\}/] = "]"
  output = input
  map.each do |regex, replacement|
    output.gsub!(regex, replacement)
  end
  output
end

#instance_attributes_table(mode = :all) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/tools/api_doc_generator.rb', line 125

def instance_attributes_table(mode=:all)
  table = String.new
  table <<  "\\\\\n||XML Element||Ruby Class||Inherits From||\n"
  table << "|#{xml_tag}|[##{class_name}]|#{yard_class.superclass}|\n"
  table << "\\\\\n||XML Child Node||Ruby Attribute||Data Type||Description||\n"
  xml_names = xml_name_hash
  @mhash[:instance_attributes].values.each do |attribute|
    read = attribute[:read]
    return_tag = read.docstring.tag(:return)
    ruby_name = read.name.to_s
    xml_name = xml_names[ruby_name] ? xml_names[ruby_name] : "-"
    data_type = return_tag.types[0]
    description = confluence_translate(return_tag.text.gsub(/\n/, ' '))
    table_row = "|#{xml_name}|#{ruby_name}|#{data_type}|#{description.gsub(/\|/, '\\|')}|\n"
    case mode
      when :xml
        if  xml_name != '-'
          table << table_row
        end
      when :ruby
        if  xml_name == '-'
          table << table_row
        end
      else
        table << table_row
    end
  end
  table
end

#method_documentation(method) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/tools/api_doc_generator.rb', line 209

def method_documentation(method)

  method_documentation = String.new
  if method.nil?
    raise "method is nil"
  end
  method_documentation << "\nh5. #{method.path}\n"

  method_documentation << "||Method||Return Type||Description||\n"
  if method.name == :initialize
    return_type = @yard_class.name
    description = 'constructor'
  else
    return_tag = method.docstring.tag(:return)
    if return_tag.nil?
      raise "#{method.name} return tag is nil"
    end
    return_type = return_tag.types[0]
    description = confluence_translate(return_tag.text.gsub(/\n/, ' '))
  end
  method_documentation << "|#{method.name}|#{return_type}|#{example.description}|\n"

  if method.respond_to?(:docstring)
    params = method.docstring.tags(:param)
    if params && params.size > 0
      method_documentation << "\n||Parameter||Data Type||Description||\n"
      params.each do |p|
        description = confluence_translate(p.text.gsub(/\n/, ' '))
        method_documentation << "|#{p.name}|#{p.types.join(', ')}|#{example.description}|\n"
      end
    end
  end

  method_documentation << "{code:lang=none|title=Ruby Source Code}\n"
  method_documentation << method.source
  method_documentation << "{code}\n"

  if method.docstring.has_tag?('example')
    method_documentation << "\n{code:lang=none|title=Usage Example}\n"
    filename = method.docstring.tag('example').name.split(/[:)}]/)[2]
    example = IO.read(File.join(@@rootpath, filename))
    method_documentation << example
    method_documentation << "{code}\n"
  end
  method_documentation
end

#methods_documentationObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/tools/api_doc_generator.rb', line 174

def methods_documentation

  methods_documentation = String.new
  
  #methods_documentation ""
  #methods_documentation "h4. Constructor"
  #method = mhash[:constructor]
  #methods_documentation_method(method, yard_class) if method

  methods =mhash[:class_methods]
  if methods.size > 0
    methods_documentation << "\nh4. Class Methods\n"
    methods.each do |method|
      methods_documentation << method_documentation(method)
    end
  end

  methods =mhash[:instance_methods]
  if methods.size > 0
    public_methods = Array.new
    methods.each do |method|
      if method.docstring.has_tag?(:api) && method.docstring.tag(:api).text == 'external'
        public_methods << method
      end
    end
    if public_methods.size > 0
      methods_documentation << "\nh4. Instance Methods\n"
      public_methods.each do |method|
        methods_documentation << method_documentation(method)
      end
    end
  end
  methods_documentation
end

#titleObject



94
95
96
97
98
# File 'lib/tools/api_doc_generator.rb', line 94

def title
  title = "\n{anchor:#{yard_class.name}}\n"
  title << "h3. Class #{yard_class.path}"
  title
end

#xml_exampleObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/tools/api_doc_generator.rb', line 112

def xml_example
  xml_example = String.new
  if yard_class.docstring.has_tag?('example')
    xml_example << "\nh4. XML Example\n"
    xml_example << "{code:lang=xml}\n"
    filename = yard_class.docstring.tag('example').name.split(/[:)}]/)[2]
    example = IO.read(File.join(@@rootpath, filename))
    xml_example << example
    xml_example << "{code}\n"
  end
  xml_example
end

#xml_name_hashObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tools/api_doc_generator.rb', line 155

def xml_name_hash
  xml_name_hash = Hash.new
  if @ruby_class.respond_to?(:attributes)
    @ruby_class.attributes.each do |attribute|
      xml_name_hash[attribute.name.to_s] = "@#{attribute.tag}"
    end
  end
  if @ruby_class.respond_to?(:elements)
    @ruby_class.elements.each do |element|
      if element.options.size == 0 or element.options[:single]
        xml_name_hash[element.name.to_s] = "<#{element.tag}>"
      else
        xml_name_hash[element.name.to_s] = "<#{element.tag}> \\[1..\\*]"
      end
    end
  end
  xml_name_hash
end