Sha256: d301d74a00aafca188d1522118a02f1b7ccb84e1a9a859c4bc7e4f54766bb86b

Contents?: true

Size: 945 Bytes

Versions: 1

Compression:

Stored size: 945 Bytes

Contents

module Stig
  module Generators
    module Hash
      extend self

      # Public: Generates a random Hash. If no keys are passed, a Hash with a
      # default proc is returned.
      #
      # generator - An object implementing #call or #random.
      # keys      - Objects to use as Hash keys.
      #
      # Returns a Hash.
      # Raises ArgumentError when an invalid generator was supplied.
      def random(generator, *keys)
        unless generator.respond_to?(:call) || generator.respond_to?(:random)
          msg = "no #call or #random implemented for #{generator}"
          raise ArgumentError, msg
        end

        method = generator.respond_to?(:call) ? :call : :random

        if keys.empty?
          hash = ::Hash.new { |hash, key| hash[key] = generator.send(method) }
        else
          hash = {}
          keys.each { |key| hash[key] = generator.send(method) }
        end

        hash
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stig-0.1.0 lib/stig/generators/hash.rb