Module: RSpec::Situations::ClassExtensions
- Defined in:
- lib/rspec/situations/class_extensions.rb
Instance Method Summary collapse
-
#_rsits(*keys) ⇒ Object
Get situations defined by the given keys.
-
#_rsits_describe_name(*keys) ⇒ Object
Combine each situation’s description into a string.
-
#describe_situation(*keys, &block) ⇒ Object
Public API method for describing a set of situations.
-
#situation(key, description = nil, &block) ⇒ Object
Public API method for defining a situation.
Instance Method Details
#_rsits(*keys) ⇒ Object
Get situations defined by the given keys
34 35 36 37 38 39 40 |
# File 'lib/rspec/situations/class_extensions.rb', line 34 def _rsits( *keys ) # Array of all situation objects pertaining to current context if keys.length > 0 _rsits_combined_hash.select{ |k,v| keys.include? k } else _rsits_combined_hash end end |
#_rsits_describe_name(*keys) ⇒ Object
Combine each situation’s description into a string
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rspec/situations/class_extensions.rb', line 44 def _rsits_describe_name( *keys ) hash = _rsits *keys 'when ' + keys.map do |key| if hash[key] hash[key].description else # Fall back to simple key.to_s if the situation isn't found key end end.join( ' and ' ) end |
#describe_situation(*keys, &block) ⇒ Object
Public API method for describing a set of situations
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rspec/situations/class_extensions.rb', line 13 def describe_situation( *keys, &block ) # Describe and invoke all relevant situations if keys.last.kind_of? String # If the last argument is a string description = keys.last # Use it as the description keys = keys[0..-1] else description = _rsits_describe_name *keys # Otherwise, generate a description end describe description do before do _rsits( *keys ).each do |situation| # For each situation we request, example.instance_eval &situation.block # Execute its block in the context of an example end end instance_eval &block # Execute the describe block in the current context end end |
#situation(key, description = nil, &block) ⇒ Object
Public API method for defining a situation
7 8 9 |
# File 'lib/rspec/situations/class_extensions.rb', line 7 def situation( key, description = nil, &block ) # RSpec extention method to add a Situation object to the hash cache _rsits_hash[key] = Situation.new( key, description, &block ) end |