Sha256: c5fbf52857972408aba93712ee971fb04d3fc0d71bc3f950a8aa78b6df0ad13c

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module Percheron
  module Validators
    class Config

      def initialize(config_file)
        @config_file = config_file
      end

      def valid?
        message = rules.return { |rule| send(rule) }

        if message
          fail Errors::ConfigFileInvalid, message
        else
          true
        end
      end

      private

        attr_reader :config_file

        def rules
          [
            :validate_config_file_defined,
            :validate_config_file_existence,
            :validate_config_file_not_empty,
            :validate_config_file_contents
          ]
        end

        def config_file_contents
          @config_file_contents ||= Hashie::Mash.new(YAML.load_file(config_file))
        end

        def validate_config_file_defined
          'Config file is not defined' if config_file.nil?
        end

        def validate_config_file_existence
          "#{base_message} does not exist" unless config_file.exist?
        end

        def validate_config_file_not_empty
          "#{base_message} is empty" if config_file_contents.empty?
        end

        def validate_config_file_contents
          "#{base_message} is invalid" unless config_file_contents.docker
        end

        def base_message
          "Config file '#{config_file}'"
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
percheron-0.7.1 lib/percheron/validators/config.rb
percheron-0.7.0 lib/percheron/validators/config.rb