Sha256: a56e8a88e949e850b263d42a93b879c6d78a87dc593089450a68e14ef971e41f

Contents?: true

Size: 1.79 KB

Versions: 22

Compression:

Stored size: 1.79 KB

Contents

module JsDuck

  class Enum
    def initialize(classes)
      @classes = classes
    end

    # Applies additional processing to all enum-classes.
    def process_all!
      @classes.each_value do |cls|
        if cls[:enum]
          process(cls)
        end
      end
    end

    private

    # processes single class
    def process(cls)
      expand_default(cls)
      strip_inheritdoc(cls)
      cls[:enum][:type] = infer_type(cls) unless cls[:enum][:type]
    end

    # Given an enum class, returns the type infered from its values.
    def infer_type(cls)
      if cls[:members].length > 0
        types = cls[:members].map {|p| p[:type] }
        types.sort.uniq.join("/")
      else
        "Object"
      end
    end

    # Expands default value like widget.* into list of properties
    def expand_default(cls)
      if cls[:enum][:default] =~ /\A(.*)\.\*\Z/
        each_alias($1) do |name, owner|
          cls[:members] << {
            :tagname => :property,
            :id => 'property-' + name,
            :name => name,
            :default => "'" + name + "'",
            :type => "String",
            :meta => {:private => owner[:private]},
            :private => owner[:private],
            :files => cls[:files],
            :owner => cls[:name],
            :doc => "Alias for {@link #{owner[:name]}}.",
          }
        end
      end
    end

    def each_alias(prefix)
      @classes.each_value do |cls|
        if cls[:aliases] && cls[:aliases][prefix]
          cls[:aliases][prefix].each {|name| yield(name, cls) }
        end
      end
    end

    # Remove the auto-inserted inheritdoc tag so the auto-detected enum
    # values default to being public.
    def strip_inheritdoc(cls)
      cls[:members].each do |p|
        p[:inheritdoc] = nil if p[:autodetected]
      end
    end
  end

end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
jsduck-4.10.4 lib/jsduck/enum.rb
jsduck-4.10.3 lib/jsduck/enum.rb
jsduck-4.10.2 lib/jsduck/enum.rb
jsduck-4.10.1 lib/jsduck/enum.rb
jsduck-4.10.0 lib/jsduck/enum.rb
jsduck-4.9.0 lib/jsduck/enum.rb
jsduck-4.8.0 lib/jsduck/enum.rb
jsduck-4.7.1 lib/jsduck/enum.rb
jsduck-4.7.0 lib/jsduck/enum.rb
jsduck-4.6.2 lib/jsduck/enum.rb
jsduck-4.6.1 lib/jsduck/enum.rb
jsduck-4.6.0 lib/jsduck/enum.rb
jsduck-4.5.1 lib/jsduck/enum.rb
jsduck-4.5.0 lib/jsduck/enum.rb
jsduck-4.4.1 lib/jsduck/enum.rb
jsduck-4.4.0 lib/jsduck/enum.rb
jsduck-4.3.2 lib/jsduck/enum.rb
jsduck-4.3.1 lib/jsduck/enum.rb
jsduck-4.3.0 lib/jsduck/enum.rb
jsduck-4.2.1 lib/jsduck/enum.rb