Method: Milkode::Database#fileList

Defined in:
lib/milkode/cdweb/lib/database.rb

#fileList(base) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/milkode/cdweb/lib/database.rb', line 139

def fileList(base)
  base_parts = base.split("/")
  base_depth = base_parts.length

  # 'depth==0'の時はMilkodeYaml#contentsからファイルリストを生成して高速化
  if (base_depth == 0)
    return yaml_load.contents.sort_by{|v| v.name}.map{|v| [v.name, false] }
  end

  # base/以下のファイルを全て取得
  records = @documents.find_shortpath_below(base)

  # ファイルリストの生成
  paths = records.map {|record|
    DocumentRecord.new(record).shortpath.split("/")
  }.find_all {|parts|
    # 先頭フォルダ名が一致するものをピックアップ
    parts.length > base_depth && parts[0, base_depth] == base_parts
  }.map {|parts|
    # [path, is_file]
    [parts[0, base_depth + 1].join("/"), parts.length == base_depth + 1]
  }.sort_by {|parts|
    # 配列の比較を利用したディレクトリ優先ソート
    # aaa, bbb/, aaa/, bbb -> [aaa/, bbb/, aaa, bbb]
    [parts[1] ? 1 : 0, parts[0].downcase] # [is_file(int), path(downcase)]
  }.uniq
  
  paths
end