Sha256: 50780838e455db2eb9a0429cead27b75839e289a7cc74ef8d280f504b954a40e

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module Percheron
  module Validators
    class Unit

      def initialize(unit)
        @unit = unit
      end

      def valid?
        message = rules.return { |rule| send(rule) }
        message ? fail(Errors::UnitInvalid, formatted_message(message)) : true
      end

      private

        attr_reader :unit

        def formatted_message(message)
          if unit.name
            "Container config for '%s' is invalid: %s" % [ unit.name, message ]
          else
            "Container config is invalid: #{message}"
          end
        end

        def rules
          [
            :validate_name,
            :validate_dockerfile_and_image_name,
            :validate_dockerfile,
            :validate_image,
            :validate_version
          ]
        end

        # rubocop:disable Style/GuardClause
        def validate_name
          if unit.name.nil? || !unit.name.to_s.match(/[\w]{3,}/)
            'Name is invalid'
          end
        end

        def validate_dockerfile_and_image_name
          if unit.dockerfile.nil? && unit.docker_image.nil?
            'Dockerfile OR image name not provided'
          end
        end

        def validate_dockerfile
          if !unit.dockerfile.nil? && !File.exist?(unit.dockerfile)
            'Dockerfile is invalid'
          end
        end

        def validate_image
          if !unit.docker_image.nil? && !unit.docker_image.match(/^.+:.+$/)
            'Docker image is invalid'
          end
        end
        # rubocop:enable Style/GuardClause

        def validate_version
          unit.version ? nil : fail(ArgumentError)
        rescue ArgumentError
          'Version is invalid'
        end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
percheron-0.7.7 lib/percheron/validators/unit.rb