Created
July 29, 2011 22:52
-
-
Save sfsekaran/1114922 to your computer and use it in GitHub Desktop.
Cucumber Definite Article Helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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