Sha256: 85f49224243672788e64fe6eb3f84ba767ffbac134ce054568d3bf245feaee8a

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module PGit
  class Configuration
    class Validator
      attr_reader :yaml

      def initialize(config_path)
        @expanded_path = File.expand_path(config_path)
        if File.exists? @expanded_path
          config_file = File.open(@expanded_path, 'r')
          @yaml = YAML.load(config_file)

          validate_existence_of_at_least_one_project
          validate_presence_of_items_in_each_project
        else
          raise PGit::Configuration::NotFoundError.new(@expanded_path)
        end
      end

      private

      def validate_presence_of_items_in_each_project
        projects = @yaml["projects"]
        all_present = projects.all? do |project|
          project["api_token"] &&
            project["path"] &&
            project["id"]
        end

        unless all_present
          raise PGit::Configuration::MissingAttributesError.new(@expanded_path)
        end
      end

      def validate_existence_of_at_least_one_project
        unless @yaml["projects"]
          raise PGit::Configuration::ProjectMissingError.new(@expanded_path)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pgit-0.0.4 lib/pgit/configuration/validator.rb