Friday, September 20, 2024 2:33:33 AM
> settings

Customize


Authenticate

> directory.rb
# frozen_string_literal: true

class Project
  class Directory < Entry
    def initialize(cache, ...)
      super(...)

      @cache = cache
      @type = :directory
    end

    def children
      @cache.children(key)
        .values
        .sort_by { |entry| [(entry.directory? ? 0 : 1), entry.name.downcase] }
    end

    #
    # Returns true if the provided entry is a parent of this entry
    #
    # @param parent [Project::Entry] The entry (Directory/File) to check
    #
    # @return [Boolean]
    #
    def parent?(parent)
      return false if parent.nil?

      parent.key.slice(0, key.size) == key
    end

    #
    # Returns true if the provided entry is a child of this entry
    #
    # @param child [Project::Entry] The entry (Directory/File) to check
    #
    # @return [Boolean]
    #
    def child?(child)
      return false if child.nil?

      child_key = child.key

      # Remove the filename only if the file is not a root file
      # The root file matters because it becomes an empty array after this transformation
      child_key = child_key[..-2] if child_key.size > 1 && child.file?

      child_key.slice(0, key.size) == key
    end

    def self_or_child?(child)
      self == child || child?(child)
    end
  end
end
All opinions represented herein are my own
- © 2024 itsthedevman
- build 340fbb8