Sha256: 62ff1c5cb4e5d9436b570cb7a01d55963ad2ee728a49d655850d81dae245aec4

Contents?: true

Size: 1.69 KB

Versions: 20

Compression:

Stored size: 1.69 KB

Contents

# lifted from state_machine plugin
module Gridify
  # 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

20 entries across 20 versions & 2 rubygems

Version Path
gridify-0.1.2 lib/gridify/assertions.rb
gridify-0.1.1 lib/gridify/assertions.rb
gridify-0.1.0 lib/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.26 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.25 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.24 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.22 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.21 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.20 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.19 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.18 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.17 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.16 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.14 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.13 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.10 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.5 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.4 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.2 lib/jquery/gridify/assertions.rb
compass-jquery-plugin-0.3.0.beta.1 lib/jquery/gridify/assertions.rb