Sha256: bc59ad2946409946d38f2270011cf95112bd8653153d088a22d412ed04c7b418

Contents?: true

Size: 569 Bytes

Versions: 2

Compression:

Stored size: 569 Bytes

Contents

module EnsureIt
  patch Object do
    def ensure_hash(*args, default: {}, **opts)
      default
    end

    def ensure_hash!(*args, default: nil, **opts)
      opts[:message] ||= '#{subject} should be a Hash'
      EnsureIt.raise_error(:ensure_hash!, **opts)
    end
  end

  patch Hash do
    using EnsureIt if ENSURE_IT_REFINED

    def ensure_hash(*args, **opts)
      return self if opts.empty? || opts[:symbolize_keys] != true
      Hash[map { |k, v| [k.ensure_symbol, v] }.reject { |x| x[0].nil? }]
    end
    alias_method :ensure_hash!, :ensure_hash
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ensure_it-0.1.3 lib/ensure_it/ensure_hash.rb
ensure_it-0.1.2 lib/ensure_it/ensure_hash.rb