Sha256: 05cf1ba17b648538a99256ff33782b697fbcc5a15b17b2024c9994e0f8b290e0

Contents?: true

Size: 1.65 KB

Versions: 11

Compression:

Stored size: 1.65 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
            "Unit config for '%s' is invalid: %s" % [ unit.name, message ]
          else
            "Unit 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

11 entries across 11 versions & 1 rubygems

Version Path
percheron-0.8.1 lib/percheron/validators/unit.rb
percheron-0.8.0 lib/percheron/validators/unit.rb
percheron-0.7.16 lib/percheron/validators/unit.rb
percheron-0.7.15 lib/percheron/validators/unit.rb
percheron-0.7.14 lib/percheron/validators/unit.rb
percheron-0.7.13 lib/percheron/validators/unit.rb
percheron-0.7.12 lib/percheron/validators/unit.rb
percheron-0.7.11 lib/percheron/validators/unit.rb
percheron-0.7.10 lib/percheron/validators/unit.rb
percheron-0.7.9 lib/percheron/validators/unit.rb
percheron-0.7.8 lib/percheron/validators/unit.rb