Skip to content

Instantly share code, notes, and snippets.

@larrytheliquid
Created September 23, 2009 03:56
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 larrytheliquid/191691 to your computer and use it in GitHub Desktop.
Save larrytheliquid/191691 to your computer and use it in GitHub Desktop.
Reply to Cucumber Transform comment http://is.gd/3AqhI
def self.TokenTransform(token, pattern, &transform)
unless Kernel.const_defined? token
class << val = []
def to_s
"(#{Regexp.union(*self)})"
end
end
Kernel.const_set token, val
end
no_captures = Regexp.new(pattern.source.gsub(/\^|\$/, '').gsub('(', '(?:'),
pattern.options)
Kernel.const_get(token) << no_captures
Transform(pattern, &transform)
end
TokenTransform "PERSON", /^a young child$/ do
...code that creates a young child person and returns it...
end
TokenTransform "PERSON", /^an elderly person$/ do
...code that creates an elderly person and returns it...
end
TokenTransform "USER", /^a guest user$/ do
...code that creates a guest user and returns it...
end
TokenTransform "USER", /^a user with role (w+)$/ do | roleString |
...code that creates a user with a certain role and returns it...
end
Given /^#{PERSON} added by #{USER}$/ do | person, user |
... (code that uses person and user as their corresponding types, already converted from a string)...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment