Sha256: b7f0dc9b30329f196f5c8101a8be6f58cd0d85dd9aa2918dc85dc684bce2e6fe

Contents?: true

Size: 1.64 KB

Versions: 16

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require "json"
require "pathname"

module SublimeTextKit
  module Metadata
    # Abstract class for processing metadata.
    class Base
      attr_reader :name, :project_dir, :metadata_dir, :metadata_file

      def self.create projects_dir, metadata_dir
        instance = new projects_dir, metadata_dir
        return unless valid_dir? instance.project_dir, "Projects"
        return unless valid_dir? instance.metadata_dir, "Metadata"

        project_paths = ::Pathname.new(instance.project_dir).children.select(&:directory?)
        project_paths.each { |project_dir| new(project_dir, metadata_dir).save }
      end

      def self.delete metadata_dir
        instance = new "", metadata_dir
        return unless valid_dir? instance.metadata_dir, "Metadata"

        ::Pathname.glob("#{instance.metadata_dir}/*.#{instance.file_extension}").each(&:delete)
      end

      def initialize project_dir, metadata_dir
        @name = File.basename project_dir
        @project_dir = File.expand_path project_dir
        @metadata_dir = File.expand_path metadata_dir
        @metadata_file = File.join @metadata_dir, "#{name}.#{file_extension}"
      end

      def file_extension
        "sublime-unknown"
      end

      def to_h
        {}
      end

      def save
        return if File.exist? metadata_file

        File.open(metadata_file, "w") { |file| file.write JSON.dump(to_h) }
      end

      def self.valid_dir? dir, label
        if File.exist? dir
          true
        else
          puts "#{label} directory doesn't exist: #{dir}."
          false
        end
      end
      private_class_method :valid_dir?
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
sublime_text_kit-10.0.2 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-10.0.1 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-10.0.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-9.6.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-9.5.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-9.4.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-9.3.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-9.2.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-9.1.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-9.0.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-8.5.2 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-8.5.1 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-8.5.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-8.4.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-8.3.0 lib/sublime_text_kit/metadata/base.rb
sublime_text_kit-8.2.0 lib/sublime_text_kit/metadata/base.rb