Sha256: 5da8350a65e1a5715abf357a3dc44f67336b4ec3cc7d1e8fb7e70ac2c536da78

Contents?: true

Size: 1.86 KB

Versions: 15

Compression:

Stored size: 1.86 KB

Contents

class Hash
  # Provides a set of helper methods for making assertions about the content
  # of various objects

  unless respond_to?(:assert_valid_keys)
    # Validate all keys in a hash match <tt>*valid_keys</tt>, raising ArgumentError
    # on a mismatch. Note that keys are NOT treated indifferently, meaning if you
    # use strings for keys but assert symbols as keys, this will fail.
    #
    #   { name: 'Rob', years: '28' }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key: :years. Valid keys are: :name, :age"
    #   { name: 'Rob', age: '28' }.assert_valid_keys('name', 'age') # => raises "ArgumentError: Unknown key: :name. Valid keys are: 'name', 'age'"
    #   { name: 'Rob', age: '28' }.assert_valid_keys(:name, :age)   # => passes, raises nothing
    # Code from ActiveSupport
    def assert_valid_keys(*valid_keys)
      valid_keys.flatten!
      each_key do |k|
        unless valid_keys.include?(k)
          raise ArgumentError.new("Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}")
        end
      end
    end
  end

  # Validates that the given hash only includes at *most* one of a set of
  # exclusive keys.  If more than one key is found, an ArgumentError will be
  # raised.
  #
  # == Examples
  #
  #   options = {:only => :on, :except => :off}
  #   options.assert_exclusive_keys(:only)                   # => nil
  #   options.assert_exclusive_keys(:except)                 # => nil
  #   options.assert_exclusive_keys(:only, :except)          # => ArgumentError: Conflicting keys: only, except
  #   options.assert_exclusive_keys(:only, :except, :with)   # => ArgumentError: Conflicting keys: only, except
  def assert_exclusive_keys(*exclusive_keys)
    conflicting_keys = exclusive_keys & keys
    raise ArgumentError, "Conflicting keys: #{conflicting_keys.join(', ')}" unless conflicting_keys.length <= 1
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
state_machines-0.6.0 lib/state_machines/assertions.rb
state_machines-0.5.0 lib/state_machines/assertions.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/state_machines-0.2.2/lib/state_machines/assertions.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/state_machines-0.2.2/lib/state_machines/assertions.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/state_machines-0.2.2/lib/state_machines/assertions.rb
state_machines-0.4.0 lib/state_machines/assertions.rb
state_machines-0.3.0 lib/state_machines/assertions.rb
state_machines-0.2.2 lib/state_machines/assertions.rb
state_machines-0.2.1 lib/state_machines/assertions.rb
state_machines-0.2.0 lib/state_machines/assertions.rb
state_machines-0.1.4 lib/state_machines/assertions.rb
state_machines-0.1.3 lib/state_machines/assertions.rb
state_machines-0.1.2 lib/state_machines/assertions.rb
state_machines-0.1.1 lib/state_machines/assertions.rb
state_machines-0.1.0 lib/state_machines/assertions.rb