Skip to content

Instantly share code, notes, and snippets.

@mattwynne
Forked from burtlo/event_steps.rb
Created November 23, 2010 09:11
Show Gist options
  • Save mattwynne/711496 to your computer and use it in GitHub Desktop.
Save mattwynne/711496 to your computer and use it in GitHub Desktop.
module EventsHelper
def create_events(events_table)
events_table.map_headers! {|header| header.is_a?(Symbol) ? header : header.gsub(/\s/,'_').to_sym }
system_time = TimeService.now
events_table.hashes.each do |event|
event[:name] ||= "Event #{system_time.strftime("%H:%M:%S")}"
event[:enabled] ||= true
event[:time] ||= system_time
Event.queue(data)
end
Event.create
end
end
World(EventsHelper)
#
# Create the following type of event statement
# with the data specified in the table
#
Given /^(?:I create )?the following events:$/ do |events|
create_events(events)
end
#
# Recently Logged in event
#
Given /^(?:I create )?an event, named '([^']*)', that a customer has logged in within the last (\d+) minutes?$/ do |name,recency|
data = [ [ 'name', 'type', 'how recent (in minutes)' ], [ name, 'Recent Communication' recency ]]
create_events Cucumber::Ast::Table.new(data)
end
#
# Purchase event
#
Given /^(?:I create )?an event, named '([^']*)', that a customer has purchased '([^']*)' within the last (\d+)\s?(?:-|to)?\s?(\d+) days?$/ do |name,product_name,start_day,end_day|
data = [ [ 'name', 'type', 'product name', 'start day', 'end day' ], [ name, 'Product Purchase', product_name, start_day, end_day ]]
create_events Cucumber::Ast::Table.new(data)
end
#
# ... more events ...
#
@burtlo
Copy link

burtlo commented Nov 23, 2010

This is great. Thanks for taking the refactor further.

I think this would be a great example to share on the Cucumber Wiki.

  1. Multiple similar steps
  2. Refactor steps to the collector step
  3. Refactor out the helper

I think that if we showed the first version with the redundancy within the step definitions, then my step at refactoring, then your final refactoring step (a beautiful bow),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment