Commit 591e13e8 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Generalized Class.methods to Class.members(type).

The same method now retrieves all members of a class:
  :cfg, :method, :property, and :event.

Plus minor Merger enhancement - Missing name is now an empty string,
not nil.
parent 790fc1e9
Loading
Loading
Loading
Loading
+16 −13
Original line number Diff line number Diff line
@@ -18,25 +18,28 @@ module JsDuck
      @doc[:extends] ? @classes[@doc[:extends]] : nil
    end

    # Returns array of all methods in a class, sorted by name.
    # See methods_hash for details.
    def methods
      methods_hash.values.sort {|a,b| a[:name] <=> b[:name] }
    # Returns array of all members of particular type in a class,
    # sorted by name.  See members_hash for details.
    def members(type)
      members_hash(type).values.sort {|a,b| a[:name] <=> b[:name] }
    end

    # Returns hash of methods in class (and parent classes).
    # When parent and child have methods with same name,
    # method from child overrides tha parent method.
    # Returns hash of members of class (and parent classes).
    # Members are methods, properties, cfgs, events (member type
    # is speified through 'type' parameter).
    #
    # We also set :member property to each method to the full class
    # When parent and child have members with same name,
    # member from child overrides tha parent member.
    #
    # We also set :member property to each member to the full class
    # name where it belongs, so one can tell them apart afterwards.
    def methods_hash
      parent_methods = parent ? parent.methods_hash : {}
      @doc[:method].each do |m|
    def members_hash(type)
      parent_members = parent ? parent.members_hash(type) : {}
      @doc[type].each do |m|
        m[:member] = full_name
        parent_methods[m[:name]] = m
        parent_members[m[:name]] = m
      end
      parent_methods
      parent_members
    end

    # A way to access full class name with similar syntax to
+2 −0
Original line number Diff line number Diff line
@@ -167,6 +167,8 @@ module JsDuck
        code[:name]
      elsif code[:type] == :assignment
        name_type == :full_name ? code[:left].join(".") : code[:left].last
      else
        ""
      end
    end

+12 −13
Original line number Diff line number Diff line
@@ -45,11 +45,11 @@ module JsDuck
    end

    def configs
      table("configs", "Config Options", "Config Options", @cls[:cfg].collect {|c| config_row(c) })
      table("configs", "Config Options", "Config Options", @cls.members(:cfg).collect {|c| config_row(c) })
    end

    def config_row(cfg)
      table_row("config-row " + expandable_class(cfg),
      table_row("config-row", cfg,
        "<a id='#{@cls.full_name}-#{cfg[:name]}'></a>" +
        "<b><a href='source/sample.html#cfg-#{@cls.full_name}-#{cfg[:name]}'>#{cfg[:name]}</a></b> : #{cfg[:type]}" +
        mdesc(cfg)
@@ -57,11 +57,11 @@ module JsDuck
    end

    def properties
      table("props", "Public Properties", "Property", @cls[:property].collect {|p| property_row(p) })
      table("props", "Public Properties", "Property", @cls.members(:property).collect {|p| property_row(p) })
    end

    def property_row(prop)
      table_row("property-row " + expandable_class(prop),
      table_row("property-row", prop,
        "<a id='#{@cls.full_name}-#{prop[:name]}'></a>" +
        "<b><a href='source/sample.html#prop-#{@cls.full_name}-#{prop[:name]}'>#{prop[:name]}</a></b> : #{prop[:type]}" +
        mdesc(prop)
@@ -69,25 +69,24 @@ module JsDuck
    end

    def methods
      table("methods", "Public Methods", "Method", @cls.methods.collect {|m| method_row(m) })
      table("methods", "Public Methods", "Method", @cls.members(:method).collect {|m| method_row(m) })
    end

    def method_row(method)
      table_row("method-row " + expandable_class(method),
      table_row("method-row", method,
        "<a id='#{@cls.full_name}-#{method[:name]}'></a>" +
        "<b><a href='source/sample.html#prop-#{@cls.full_name}-#{method[:name]}'>#{method[:name]}</a></b>()" +
        " : " + (method[:return] ? (method[:return][:type] || "void") : "void") +
        mdesc(method),
        Class.short_name(method[:member])
        mdesc(method)
      )
    end

    def events
      table("events", "Public Events", "Event", @cls[:event].collect {|e| event_row(e) })
      table("events", "Public Events", "Event", @cls.members(:event).collect {|e| event_row(e) })
    end

    def event_row(event)
      table_row("method-row " + expandable_class(event),
      table_row("method-row", event,
        "<a id='#{@cls.full_name}-#{event[:name]}'></a>" +
        "<b><a href='source/sample.html#prop-#{@cls.full_name}-#{event[:name]}'>#{event[:name]}</a></b> : ()" +
        mdesc(event)
@@ -124,12 +123,12 @@ module JsDuck
      ].join("\n")
    end

    def table_row(className, contents, defined_by=nil)
    def table_row(className, item, contents)
      [
       "<tr class='#{className}'>",
       "<tr class='#{className} #{expandable_class(item)}'>",
         "<td class='micon'><a href='#expand' class='exi'>&nbsp;</a></td>",
         "<td class='sig'>#{contents}</td>",
         "<td class='msource'>#{defined_by ? defined_by : @cls.short_name}</td>",
         "<td class='msource'>#{Class.short_name(item[:member])}</td>",
       "</tr>",
      ].join("")
    end
+4 −4
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ require "test/unit"

class TestClass < Test::Unit::TestCase

  def test_local_methods
  def test_local_members
    cls = JsDuck::Class.new({
      :name => "MyClass",
      :method => [
@@ -12,13 +12,13 @@ class TestClass < Test::Unit::TestCase
      ]
    });

    ms = cls.methods_hash
    ms = cls.members_hash(:method)
    assert_equal(2, ms.values.length)
    assert_equal("MyClass", ms["foo"][:member])
    assert_equal("MyClass", ms["bar"][:member])
  end

  def test_parent_methods
  def test_parent_members
    classes = {}
    parent = JsDuck::Class.new({
      :name => "ParentClass",
@@ -38,7 +38,7 @@ class TestClass < Test::Unit::TestCase
    }, classes);
    classes["MyClass"] = child

    ms = child.methods_hash
    ms = child.members_hash(:method)
    assert_equal(3, ms.values.length)
    assert_equal("MyClass", ms["foo"][:member], "foo should be overridden in child class")
    assert_equal("MyClass", ms["bar"][:member], "bar is only in child class")