Sha256: 469083d2370d05fce01e388efe0adb893c2c1e199edab428070c833aa8bba37a

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'legion/settings/rules'
require 'legion/settings/validators'
require 'legion/settings/constants'

module Legion
  module Settings
    class Validator
      include Rules
      include Validators

      attr_reader :failures

      def initialize
        @failures = []
      end

      def run(settings, service = nil)
        validate_legion(settings[:legion])
        validate_transport(settings[:transport])
        validate_categories(settings)
        case service
        when 'client'
          validate_client(settings[:client])
        when 'api'
          validate_api(settings[:api])
        when 'rspec'
          validate_client(settings[:client])
          validate_api(settings[:api])
        end
        @failures
      end

      def reset!
        failure_count = @failures.size
        @failures = []
        failure_count
      end
      alias reset reset!

      private

      def validate_categories(settings)
        CATEGORIES.each do |category|
          if is_a_hash?(settings[category])
            validate_method = "validate_#{category.to_s.chop}".to_sym
            settings[category].each do |name, details|
              send(validate_method, details.merge(name: name.to_s))
            end
          else
            invalid(settings[category], "#{category} must be a hash")
          end
        end
      end

      def invalid(object, message)
        @failures << {
          object:  object,
          message: message
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
legion-settings-1.1.2 lib/legion/settings/validator.rb