Sha256: 30b74624382a0c59f3495f28d92746ad1ac7490ee3e709d74ec8e06c4ce0cce4

Contents?: true

Size: 1.73 KB

Versions: 15

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

module Listen
  # @private api
  class Record
    # Represents a directory entry (dir or file)
    class Entry
      # file: "/home/me/watched_dir", "app/models", "foo.rb"
      # dir, "/home/me/watched_dir", "."
      def initialize(root, relative, name = nil)
        @root = root
        @relative = relative
        @name = name
      end

      attr_reader :root, :relative, :name

      def children
        child_relative = _join
        (_entries(sys_path) - %w(. ..)).map do |name|
          Entry.new(@root, child_relative, name)
        end
      end

      def meta
        lstat = ::File.lstat(sys_path)
        { mtime: lstat.mtime.to_f, mode: lstat.mode, size: lstat.size }
      end

      # record hash is e.g.
      # if @record["/home/me/watched_dir"]["project/app/models"]["foo.rb"]
      # if @record["/home/me/watched_dir"]["project/app"]["models"]
      # record_dir_key is "project/app/models"
      def record_dir_key
        ::File.join(*[@relative, @name].compact)
      end

      def sys_path
        # Use full path in case someone uses chdir
        ::File.join(*[@root, @relative, @name].compact)
      end

      def real_path
        @real_path ||= ::File.realpath(sys_path)
      end

      private

      def _join
        args = [@relative, @name].compact
        args.empty? ? nil : ::File.join(*args)
      end

      def _entries(dir)
        return Dir.entries(dir) unless RUBY_ENGINE == 'jruby'

        # JRuby inconsistency workaround, see:
        # https://github.com/jruby/jruby/issues/3840
        exists = ::File.exist?(dir)
        directory = ::File.directory?(dir)
        return Dir.entries(dir) unless exists && !directory
        raise Errno::ENOTDIR, dir
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
qiita_org-0.1.35 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.34 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.33 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.32 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.31 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.30 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.29 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.28 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.27 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.26 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
qiita_org-0.1.25 gems/ruby/2.7.0/gems/listen-3.3.1/lib/listen/record/entry.rb
listen-3.3.1 lib/listen/record/entry.rb
listen-3.3.0 lib/listen/record/entry.rb
listen-3.3.0.pre.3 lib/listen/record/entry.rb
listen-3.3.0.pre.2 lib/listen/record/entry.rb