Module: StateMachine::Assertions
- Included in:
- Branch, Event, Graph, Machine, MachineCollection, NodeCollection, Path, PathCollection, State, StateContext, TransitionCollection
- Defined in:
- lib/state_machine/assertions.rb
Overview
Provides a set of helper methods for making assertions about the content of various objects
Instance Method Summary collapse
-
#assert_exclusive_keys(hash, *exclusive_keys) ⇒ Object
Validates that the given hash only includes at most one of a set of exclusive keys.
-
#assert_valid_keys(hash, *valid_keys) ⇒ Object
Validates that the given hash only includes the specified valid keys.
Instance Method Details
#assert_exclusive_keys(hash, *exclusive_keys) ⇒ Object
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
= {:only => :on, :except => :off}
assert_exclusive_keys(, :only) # => nil
assert_exclusive_keys(, :except) # => nil
assert_exclusive_keys(, :only, :except) # => ArgumentError: Conflicting keys: only, except
assert_exclusive_keys(, :only, :except, :with) # => ArgumentError: Conflicting keys: only, except
31 32 33 34 |
# File 'lib/state_machine/assertions.rb', line 31 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 |
#assert_valid_keys(hash, *valid_keys) ⇒ Object
Validates that the given hash only includes the specified valid keys. If any invalid keys are found, an ArgumentError will be raised.
Examples
= {:name => 'John Smith', :age => 30}
assert_valid_keys(, :name) # => ArgumentError: Invalid key(s): age
assert_valid_keys(, 'name', 'age') # => ArgumentError: Invalid key(s): age, name
assert_valid_keys(, :name, :age) # => nil
15 16 17 18 |
# File 'lib/state_machine/assertions.rb', line 15 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 |