Sha256: 63fe4db8a8a6b7ad2e617003888dae6ff390931a4d15cd16e26d2acb6883d30f

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'yaml'

module GitContext
  module Storage
    class YML
      attr_reader :config

      def initialize(config)
        @config = config
      end

      def setup
        return if FileTest.exist?(config_filepath)

        FileUtils.touch(config_filepath)
      end

      def load
        return unless config_filepath && FileTest.exist?(config_filepath)

        load_profiles_from_file
        load_contexts_from_file
      end

      def save
        File.open(config_filepath, 'w') do |file|
          file.write(YAML.dump(serialized_data))
        end
      end

      private

      def load_profiles_from_file
        content['profiles']&.each do |profile|
          user = User.new(profile['name'], profile['email'], profile['signing_key'])
          config.profiles << Profile.new(profile['profile_name'], user)
        end
      end

      def load_contexts_from_file
        content['contexts']&.each do |context|
          config.contexts << Context.new(context['work_dir'], context['profile_name'])
        end
      end

      def content
        @content ||= YAML.load_file(config_filepath) || {}
      end

      def serialized_data
        config.serialize
      end

      def config_filepath
        File.join(git_context_dir, CONFIG_FILE)
      end

      def git_context_dir
        File.join(config.home, BASE_STORAGE_DIR)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
git_context-0.3.0 lib/git_context/storage/yml.rb