Sha256: f7abf88f6d69c1522813a36f59fc1f965205428dd8a2a686804f6fcfbcf8b21a

Contents?: true

Size: 876 Bytes

Versions: 2

Compression:

Stored size: 876 Bytes

Contents

module Minitel
  module StrictArgs
    extend self
    def enforce(args, keywords, uuid_field)
      ensure_strict_args(args.keys, keywords)
      ensure_no_nils(args, keywords)
      ensure_is_uuid(args[uuid_field])
    end

    # A Ruby 2.1 required keyword argument sorta backport
    def ensure_strict_args(keys, accept_keys)
      if keys.sort != accept_keys.sort
        delta = accept_keys - keys
        raise ArgumentError, "missing or extra keywords: #{delta.join(', ')}"
      end
    end

    def ensure_no_nils(args, keys)
      keys.each do |key|
       raise ArgumentError, "keyword #{key} is nil" unless args[key]
      end
    end

    def ensure_is_uuid(uuid)
      unless uuid =~ /\A[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}\z/i
        raise ArgumentError, "'#{uuid.inspect}' not formated like a uuid"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
minitel-0.3.0 lib/minitel/strict_args.rb
minitel-0.2.0 lib/minitel/strict_args.rb