Skip to content
Browse files

start of sequential translator

  • Loading branch information...
1 parent 5f5c476 commit a01cf7d66de82e96822caeac085cf3264074693a @cheezy committed Jul 9, 2013
View
7 features/data_magic.feature
@@ -78,6 +78,13 @@ Feature: Functionality of the data_magic gem
Then the value for "random" should be either "Tom", "Dick", or "Harry"
And the value for "range" should be between 1 and 5
+ Scenario: Returning a sequential value from an array
+ Then the first value for the sequential data should be "first"
+ When I ask for the data again
+ Then the second value for the sequential data should be "second"
+ When I ask for the data again
+ Then the third value for the sequential data should be "third"
+
Scenario: Returning a value based on a mask
Then the value for "mask" should begin with 3 numbers
And the value for "mask" should have 3 upper case letters after a dash
View
11 features/step_definitions/data_magic_steps.rb
@@ -13,7 +13,8 @@ class TestClass
end
When /^I ask for the data for "(.+)"$/ do |key|
- @data = TestClass.new.data_for key
+ @tc = TestClass.new
+ @data = @tc.data_for key
end
@@ -132,3 +133,11 @@ class TranslatorAdded
ta = TranslatorAdded.new
@data = ta.data_for 'dynamic'
end
+
+Then(/^the (?:first|second|third) value for the sequential data should be "(.*?)"$/) do |value|
+ @data['ordered'].should == value
+end
+
+When(/^I ask for the data again$/) do
+ @data = @tc.data_for 'dm'
+end
View
1 features/yaml/example.yml
@@ -31,6 +31,7 @@ dm:
bool_true: true
bool_false: false
random: ~randomize ['Tom', 'Dick', 'Harry']
+ ordered: ~sequential ['first', 'second', 'third']
range: ~randomize 1..5
mask: ~mask "###-AAA_aaa"
title: ~title
View
6 lib/data_magic.rb
@@ -33,7 +33,11 @@ def prep_data(data)
end
def translate(value)
- Translation.new.send :process, value
+ translation.send :process, value
+ end
+
+ def translation
+ @translation ||= Translation.new
end
class << self
View
21 lib/data_magic/translation.rb
@@ -3,7 +3,7 @@
module DataMagic
class Translation
include DateTranslation
-
+
#
# return a random name (first and last)
#
@@ -227,6 +227,13 @@ def randomize(value)
end
alias_method :dm_randomize, :randomize
+ def sequential(value)
+ index = index_variable_for(value)
+ index = (index ? index + 1 : 0)
+ set_index_variable(value, index)
+ value[index]
+ end
+
#
# return a value based on a mast
# The # character will be replaced with a number
@@ -251,6 +258,18 @@ def mask(value)
private
+ def set_index_variable(ary, value)
+ instance_variable_set(index_name(ary), value)
+ end
+
+ def index_variable_for(ary)
+ instance_variable_get(index_name(ary))
+ end
+
+ def index_name(ary)
+ "@private_#{ary[0]}#{ary[1]}_index"
+ end
+
def process(value)
eval value
end

0 comments on commit a01cf7d

Please sign in to comment.
Something went wrong with that request. Please try again.