Sha256: 9b4db79ce252b26523871e4f55e78c901cb8867d06143d2d2e549c2b19df8681

Contents?: true

Size: 1.25 KB

Versions: 52

Compression:

Stored size: 1.25 KB

Contents

require 'jsduck/logger'

module JsDuck

  # Parent class for assets that consist of groups.
  # That is: guides, vides, examples.
  #
  # Subclasses must initialize @groups before calling any of the
  # methods in this class.
  class GroupedAsset
    # Should be called from constructor after @groups have been read in,
    # and after it's been ensured that all items in groupes have names.
    def build_map_by_name
      @map_by_name = {}
      each_item do |item|
        @map_by_name[item["name"]] = item
      end
    end

    # Accesses item by name
    def [](name)
      @map_by_name[name]
    end

    # Iterates over all items in all groups
    def each_item(group=nil, &block)
      group = group || @groups

      group.each do |item|
        if item["items"]
          each_item(item["items"], &block)
        else
          block.call(item)
        end
      end
    end

    def map_items(group=nil, &block)
      group = group || @groups

      group.map do |item|
        if item["items"]
          {
            "title" => item["title"],
            "items" => map_items(item["items"], &block)
          }
        else
          block.call(item)
        end
      end
    end

    # Returns all groups as array
    def to_array
      @groups
    end
  end

end

Version data entries

52 entries across 52 versions & 3 rubygems

Version Path
jsduck-5.3.0 lib/jsduck/grouped_asset.rb
jsduck-5.2.0 lib/jsduck/grouped_asset.rb
jsduck-5.1.0 lib/jsduck/grouped_asset.rb
jsduck-5.0.1 lib/jsduck/grouped_asset.rb
jsduck-5.0.0 lib/jsduck/grouped_asset.rb
jsduck-5.0.0.beta5 lib/jsduck/grouped_asset.rb
jsduck-5.0.0.beta4 lib/jsduck/grouped_asset.rb
jsduck-4.10.4 lib/jsduck/grouped_asset.rb
jsduck-5.0.0.beta3 lib/jsduck/grouped_asset.rb
jsduck-4.10.3 lib/jsduck/grouped_asset.rb
jsduck-4.10.2 lib/jsduck/grouped_asset.rb
jsduck-4.10.1 lib/jsduck/grouped_asset.rb
jsduck-4.10.0 lib/jsduck/grouped_asset.rb
jsduck-4.9.0 lib/jsduck/grouped_asset.rb
jsduck-4.8.0 lib/jsduck/grouped_asset.rb
jsduck-5.0.0.beta2 lib/jsduck/grouped_asset.rb
jsduck-4.7.1 lib/jsduck/grouped_asset.rb
jsduck-4.7.0 lib/jsduck/grouped_asset.rb
jsduck-5.0.0.beta01 lib/jsduck/grouped_asset.rb
jsduck-4.6.2 lib/jsduck/grouped_asset.rb