Sha256: 70ce4a3e22e624f0631075a742257700cc51c7439fa10b5da66e5f22090e7f97

Contents?: true

Size: 780 Bytes

Versions: 7

Compression:

Stored size: 780 Bytes

Contents

module Lipseys
  class Base

    protected

    def requires!(*args)
      self.class.requires!(*args)
    end

    def self.requires!(hash, *params)
      hash_keys = hash.collect { |k, v| v.is_a?(Array) ? [k, v.collect(&:keys)] : k }.flatten

      params.each do |param|
        if param.is_a?(Array)
          raise ArgumentError.new("Missing required parameter: #{param.first}") unless hash_keys.include?(param.first)

          valid_options = param[1..-1]
          raise ArgumentError.new("Parameter: #{param.first} must be one of: #{valid_options.join(', ')}") unless valid_options.include?(hash[param.first])
        else
          raise ArgumentError.new("Missing required parameter: #{param}") unless hash_keys.include?(param)
        end
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
lipseys-6.2.1 lib/lipseys/base.rb
lipseys-6.2.0 lib/lipseys/base.rb
lipseys-6.1.1 lib/lipseys/base.rb
lipseys-6.1.0 lib/lipseys/base.rb
lipseys-6.0.2 lib/lipseys/base.rb
lipseys-6.0.1 lib/lipseys/base.rb
lipseys-6.0.0 lib/lipseys/base.rb