Sha256: e62bee5a3737f675cb3d09f33b378e21626149b634eaaf55c6511a840082b7c9

Contents?: true

Size: 1.1 KB

Versions: 23

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true
require "pathname"

module ThemeCheck
  class FileSystemStorage < Storage
    attr_reader :root

    def initialize(root, ignored_patterns: [])
      @root = Pathname.new(root)
      @ignored_patterns = ignored_patterns
      @files = {}
    end

    def path(relative_path)
      @root.join(relative_path)
    end

    def read(relative_path)
      file(relative_path).read
    end

    def write(relative_path, content)
      file(relative_path).write(content)
    end

    def files
      @file_array ||= glob("**/*")
        .map { |path| path.relative_path_from(@root).to_s }
    end

    def directories
      @directories ||= glob('*')
        .select { |f| File.directory?(f) }
        .map { |f| f.relative_path_from(@root).to_s }
    end

    private

    def glob(pattern)
      @root.glob(pattern).reject do |path|
        relative_path = path.relative_path_from(@root)
        @ignored_patterns.any? { |ignored| relative_path.fnmatch?(ignored) }
      end
    end

    def file(name)
      return @files[name] if @files[name]
      @files[name] = root.join(name)
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
theme-check-1.2.0 lib/theme_check/file_system_storage.rb
theme-check-1.1.0 lib/theme_check/file_system_storage.rb
theme-check-1.0.0 lib/theme_check/file_system_storage.rb
theme-check-0.10.2 lib/theme_check/file_system_storage.rb
theme-check-0.10.1 lib/theme_check/file_system_storage.rb
theme-check-0.10.0 lib/theme_check/file_system_storage.rb
theme-check-0.9.1 lib/theme_check/file_system_storage.rb
theme-check-0.9.0 lib/theme_check/file_system_storage.rb
theme-check-0.8.3 lib/theme_check/file_system_storage.rb
theme-check-0.8.2 lib/theme_check/file_system_storage.rb
theme-check-0.8.1 lib/theme_check/file_system_storage.rb
theme-check-0.8.0 lib/theme_check/file_system_storage.rb
theme-check-0.7.3 lib/theme_check/file_system_storage.rb
theme-check-0.7.2 lib/theme_check/file_system_storage.rb
theme-check-0.7.1 lib/theme_check/file_system_storage.rb
theme-check-0.7.0 lib/theme_check/file_system_storage.rb
theme-check-0.6.0 lib/theme_check/file_system_storage.rb
theme-check-0.5.0 lib/theme_check/file_system_storage.rb
theme-check-0.4.0 lib/theme_check/file_system_storage.rb
theme-check-0.3.3 lib/theme_check/file_system_storage.rb