Sha256: 508d9c74274bc4eedb41b2f386860682f5f903e222c4fa7cd0c6fec6940315c0

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module PanamaxTemplateValidator
  class Template
    attr_reader :errors

    def initialize(template_attrs)
      @template_attrs = template_attrs
      @errors = []
    end

    def validate
      validate_presence_of('name')
      validate_presence_of('description')
      validate_presence_of('documentation')
      validate_length_of('documentation', '>', 40)
      image_names = @template_attrs['images'].map { |image| image['name'] }
      @template_attrs['images'].each do |image_attrs|
        image = Image.new(image_attrs, image_names)
        image.validate
        @errors += image.errors
      end
    end

    private

    def validate_presence_of(key)
      if @template_attrs[key].nil? || @template_attrs[key] == ''
        @errors << "template #{key} is required"
      end
    end

    def validate_length_of(key, inequality, length)
      return unless @template_attrs[key]
      unless @template_attrs[key].length.send(inequality, length)
        @errors << "template #{key} should #{inequality} #{length}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
panamax_template_validator-0.0.2 lib/panamax_template_validator/template.rb