Sha256: 74a9b04775743489ef9796f634049d5c33f44a1d7864646aee97dd26708497a7

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 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][:property].length > 0
        types = cls[:members][:property].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][:property] << {
            :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][:property].each do |p|
        p[:inheritdoc] = nil if p[:autodetected]
      end
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jsduck-4.0.1 lib/jsduck/enum.rb
jsduck-4.0.0 lib/jsduck/enum.rb
jsduck-4.0.beta2 lib/jsduck/enum.rb