Sha256: 5cc23affde7f20c2a900d9dfdb7183d57caeddcd69774ca758811a747d9d01d0

Contents?: true

Size: 1.66 KB

Versions: 11

Compression:

Stored size: 1.66 KB

Contents

module EnumStateMachine
  # Provides a set of helper methods for making assertions about the content
  # of various objects
  module Assertions
    # Validates that the given hash *only* includes the specified valid keys.
    # If any invalid keys are found, an ArgumentError will be raised.
    #
    # == Examples
    # 
    #   options = {:name => 'John Smith', :age => 30}
    #   
    #   assert_valid_keys(options, :name)           # => ArgumentError: Invalid key(s): age
    #   assert_valid_keys(options, 'name', 'age')   # => ArgumentError: Invalid key(s): age, name
    #   assert_valid_keys(options, :name, :age)     # => nil
    def assert_valid_keys(hash, *valid_keys)
      invalid_keys = hash.keys - valid_keys
      raise ArgumentError, "Invalid key(s): #{invalid_keys.join(', ')}" unless invalid_keys.empty?
    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}
    #   assert_exclusive_keys(options, :only)                   # => nil
    #   assert_exclusive_keys(options, :except)                 # => nil
    #   assert_exclusive_keys(options, :only, :except)          # => ArgumentError: Conflicting keys: only, except
    #   assert_exclusive_keys(options, :only, :except, :with)   # => ArgumentError: Conflicting keys: only, except
    def assert_exclusive_keys(hash, *exclusive_keys)
      conflicting_keys = exclusive_keys & hash.keys
      raise ArgumentError, "Conflicting keys: #{conflicting_keys.join(', ')}" unless conflicting_keys.length <= 1
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
enum_state_machine-0.8.0 lib/enum_state_machine/assertions.rb
enum_state_machine-0.7.0 lib/enum_state_machine/assertions.rb
enum_state_machine-0.6.0 lib/enum_state_machine/assertions.rb
enum_state_machine-0.5.0 lib/enum_state_machine/assertions.rb
enum_state_machine-0.4.1 lib/enum_state_machine/assertions.rb
enum_state_machine-0.4.0 lib/enum_state_machine/assertions.rb
enum_state_machine-0.3.0 lib/enum_state_machine/assertions.rb
enum_state_machine-0.2.0 lib/enum_state_machine/assertions.rb
enum_state_machine-0.1.1 lib/enum_state_machine/assertions.rb
enum_state_machine-0.1.0 lib/enum_state_machine/assertions.rb
enum_state_machine-0.0.1 lib/enum_state_machine/assertions.rb