Class: JsDuck::GroupedAsset

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/grouped_asset.rb

Overview

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.

Direct Known Subclasses

Examples, Guides, Videos

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object

Accesses item by name



21
22
23
# File 'lib/jsduck/grouped_asset.rb', line 21

def [](name)
  @map_by_name[name]
end

#build_map_by_nameObject

Should be called from constructor after @groups have been read in, and after it’s been ensured that all items in groupes have names.



13
14
15
16
17
18
# File 'lib/jsduck/grouped_asset.rb', line 13

def build_map_by_name
  @map_by_name = {}
  each_item do |item|
    @map_by_name[item["name"]] = item
  end
end

#each_item(group = nil, &block) ⇒ Object

Iterates over all items in all groups



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jsduck/grouped_asset.rb', line 26

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

#map_items(group = nil, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jsduck/grouped_asset.rb', line 38

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

#to_arrayObject

Returns all groups as array



54
55
56
# File 'lib/jsduck/grouped_asset.rb', line 54

def to_array
  @groups
end