From 257e8ff5eb56a287eb0fc91e2473d2364fc34448 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Sun, 5 Dec 2010 21:00:41 +0200 Subject: [PATCH] Extracted ParamList class. Removed parameter list creation code from Table class as it's only needed for EventTable and MethodTable - so it's now in separate class that only those table-classes use. --- lib/jsduck.rb | 1 + lib/jsduck/event_table.rb | 3 ++- lib/jsduck/method_table.rb | 7 ++++++- lib/jsduck/param_list.rb | 30 ++++++++++++++++++++++++++++++ lib/jsduck/table.rb | 20 -------------------- 5 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 lib/jsduck/param_list.rb diff --git a/lib/jsduck.rb b/lib/jsduck.rb index e6c7b22e..81b3ba6e 100755 --- a/lib/jsduck.rb +++ b/lib/jsduck.rb @@ -10,6 +10,7 @@ require 'jsduck/doc_links' require 'jsduck/table' require 'jsduck/cfg_table' require 'jsduck/property_table' +require 'jsduck/param_list' require 'jsduck/method_table' require 'jsduck/event_table' require 'jsduck/page' diff --git a/lib/jsduck/event_table.rb b/lib/jsduck/event_table.rb index 1864e1a7..16b9f85a 100644 --- a/lib/jsduck/event_table.rb +++ b/lib/jsduck/event_table.rb @@ -8,10 +8,11 @@ module JsDuck @title = "Public Events" @column_title = "Event" @row_class = "method-row" + @param_list = ParamList.new end def signature_suffix(item) - " : " + short_param_list(item) + " : " + @param_list.short(item[:params]) end end diff --git a/lib/jsduck/method_table.rb b/lib/jsduck/method_table.rb index cf63caef..feef3ff6 100644 --- a/lib/jsduck/method_table.rb +++ b/lib/jsduck/method_table.rb @@ -8,10 +8,15 @@ module JsDuck @title = "Public Methods" @column_title = "Method" @row_class = "method-row" + @param_list = ParamList.new end def signature_suffix(item) - short_param_list(item) + " : " + (item[:return] ? (item[:return][:type] || "void") : "void") + @param_list.short(item[:params]) + " : " + return_type(item) + end + + def return_type(item) + item[:return] ? (item[:return][:type] || "void") : "void" end end diff --git a/lib/jsduck/param_list.rb b/lib/jsduck/param_list.rb new file mode 100644 index 00000000..486dc2fc --- /dev/null +++ b/lib/jsduck/param_list.rb @@ -0,0 +1,30 @@ +module JsDuck + + # Renders method/event parameter lists + class ParamList + # Creates short parameters list used in signatures. + def short(params) + if params.length > 0 + "( " + params.collect {|p| format_short(p) }.join(", ") + " )" + else + "()" + end + end + + def format_short(param) + type = param[:type] || "Object" + name = param[:name] || "" + str = "#{type} #{name}" + if optional?(param) + "[" + str + "]" + else + str + end + end + + def optional?(param) + return param[:doc] =~ /\(optional\)/ + end + end + +end diff --git a/lib/jsduck/table.rb b/lib/jsduck/table.rb index 1af2119e..f4b3fca7 100644 --- a/lib/jsduck/table.rb +++ b/lib/jsduck/table.rb @@ -45,26 +45,6 @@ module JsDuck return "#{item[:name]}" + signature_suffix(item) end - # Creates parameter list used in method and event signature. - def short_param_list(item) - if item[:params].length == 0 - return "()" - end - - params = item[:params].collect do |p| - type = p[:type] || "Object" - name = p[:name] || "" - str = "#{type} #{name}" - if p[:doc] =~ /\(optional\)/ - "[" + str + "]" - else - str - end - end - - return "( " + params.join(", ") + " )" - end - # 116 chars is also where ext-doc makes its cut, but unlike # ext-doc we only make the cut when there's more than 120 chars. # -- GitLab