Sha256: 4833759cda5f60a932e1fff5d2106ae9776f9ed0f64d2640ced0572dedfbf6ca

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

module Apidoco
  # Parse a folder and find all the sub folders and files
  class FolderParser
    attr_accessor :directory, :parents

    def initialize(directory, parents: [])
      self.directory = directory
      self.parents = parents
    end

    def as_json
      {
        is_folder: true,
        name: basename,
        children: children.select(&:published?).yield_self(&method(:sorter)).map(&:as_json)
      }
    end

    def basename
      directory.basename.to_s.titleize
    end

    def children
      directory.children.map do |child|
        hierrarchy = parents + [basename]
        if child.directory?
          Apidoco::FolderParser.new(child, parents: hierrarchy)
        else
          Apidoco::FileParser.new(child, parents: hierrarchy)
        end
      end
    end

    def published?
      true
    end

    def sort_order
      basename
    end

    private

    def sorter(files)
      files.sort do |a, b|
        if a.sort_order.class == b.sort_order.class
          a.sort_order <=> b.sort_order
        elsif a.sort_order.is_a?(String)
          -1
        else
          1
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
apidoco-1.6.4 lib/apidoco/folder_parser.rb
apidoco-1.6.3 lib/apidoco/folder_parser.rb
apidoco-1.6.2 lib/apidoco/folder_parser.rb
apidoco-1.6.1 lib/apidoco/folder_parser.rb