Skip to content

Instantly share code, notes, and snippets.

@sfsekaran
Created July 29, 2011 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfsekaran/1114922 to your computer and use it in GitHub Desktop.
Save sfsekaran/1114922 to your computer and use it in GitHub Desktop.
Cucumber Definite Article Helper
module DefiniteArticleHelper
def the
@the ||= DefiniteArticle.new
end
def push_context
@the_contexts ||= []
@the_contexts.push(the.clone)
@the = nil
end
def pop_context
@the_contexts ||= []
old_context = @the.clone
@the = @the_contexts.pop() unless @the_contexts.empty?
old_context
end
def in_a_new_context(shared_state={})
logged_out = log_out
push_context
shared_state.each do |key, val|
the.send("#{key}=", val)
end
yield
log_out
context = pop_context
log_in(the.user) if logged_out
context
end
def clear_contexts
@the_contexts.try(:clear)
end
class DefiniteArticle
def initialize
@data = {}
end
def method_missing(method_name, value=nil)
if method_name.to_s =~ /(.*)=$/
@data[$1.to_sym] = value
else
@data[method_name]
end
end
end
end
After do
clear_contexts
end
World(DefiniteArticleHelper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment