centric_data_magic 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/ChangeLog +142 -0
- data/Gemfile +14 -0
- data/Guardfile +18 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/RELEASE_NOTES.md +11 -0
- data/Rakefile +27 -0
- data/centric_data_magic.gemspec +28 -0
- data/config/data/default.yml +3 -0
- data/config/data/user.yml +3 -0
- data/creating_gem.md +17 -0
- data/cucumber.yml +2 -0
- data/features/data_magic.feature +134 -0
- data/features/defaults.feature +8 -0
- data/features/step_definitions/data_magic_steps.rb +175 -0
- data/features/support/env.rb +11 -0
- data/features/yaml/another.yml +9 -0
- data/features/yaml/example.yml +65 -0
- data/lib/data_magic/core_ext/hash.rb +7 -0
- data/lib/data_magic/core_ext/integer.rb +13 -0
- data/lib/data_magic/core_ext/string.rb +9 -0
- data/lib/data_magic/date_translation.rb +100 -0
- data/lib/data_magic/standard_translation.rb +370 -0
- data/lib/data_magic/translation.rb +15 -0
- data/lib/data_magic/version.rb +5 -0
- data/lib/data_magic.rb +114 -0
- data/spec/lib/data_magic_spec.rb +113 -0
- data/spec/lib/translation_spec.rb +326 -0
- data/spec/spec_helper.rb +30 -0
- metadata +134 -0
@@ -0,0 +1,175 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class TestClass
|
4
|
+
include DataMagic
|
5
|
+
end
|
6
|
+
|
7
|
+
Given(%r{^I have read the yaml file from features/yaml$}) do
|
8
|
+
DataMagic.yml_directory = 'features/yaml'
|
9
|
+
DataMagic.load 'example.yml'
|
10
|
+
end
|
11
|
+
|
12
|
+
Given(/^I have read the default yaml file from the default location$/) do
|
13
|
+
end
|
14
|
+
|
15
|
+
When(/^I ask for the data for "(.+)"$/) do |key|
|
16
|
+
TestClass.instance_variable_set '@private_firstsecond_index', 0
|
17
|
+
@tc = TestClass.new
|
18
|
+
@data = @tc.data_for key
|
19
|
+
end
|
20
|
+
|
21
|
+
Then(/^the value for "(.+)" should be "(.+)"$/) do |key, value|
|
22
|
+
expect(@data[key]).to eql value
|
23
|
+
end
|
24
|
+
|
25
|
+
Then(/^the value for "(.+)" should be (true|false)$/) do |key, value|
|
26
|
+
expect(@data[key].to_s).to eql value
|
27
|
+
end
|
28
|
+
|
29
|
+
Then(/^the value for "(.+)" should be (\d+) word|words long$/) do |key, length|
|
30
|
+
expect(@data[key].split(' ').size).to eql length.to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
Then(/^the value for "(.+)" should have a minimum of (\d+) word|wordss$/) do |key, length|
|
34
|
+
expect(@data[key].split(' ').size).to be >= length.to_i
|
35
|
+
end
|
36
|
+
|
37
|
+
Then(/^the value for "(.*?)" should be (\d+) characters long$/) do |key, length|
|
38
|
+
expect(@data[key].length).to eql length.to_i
|
39
|
+
end
|
40
|
+
|
41
|
+
Then(/^the value for "(.+)" should exist$/) do |key|
|
42
|
+
expect(@data[key]).not_to be_nil
|
43
|
+
end
|
44
|
+
|
45
|
+
When(/^I load the file "(.+)"$/) do |file_name|
|
46
|
+
DataMagic.load file_name
|
47
|
+
end
|
48
|
+
|
49
|
+
Then(/^the value for "(.*?)" should be either "(.*?)", "(.*?)", or "(.*?)"$/) do |key, vala, valb, valc|
|
50
|
+
expect([vala, valb, valc]).to include @data[key]
|
51
|
+
end
|
52
|
+
|
53
|
+
Then(/^the value for "(.*?)" should be between (\d+) and (\d+)$/) do |key, low, high|
|
54
|
+
value = @data[key]
|
55
|
+
expect(value).to be >= low.to_i
|
56
|
+
expect(value).to be <= high.to_i
|
57
|
+
end
|
58
|
+
|
59
|
+
Then(/^the value for "(.*?)" should begin with (\d+) numbers$/) do |key, num|
|
60
|
+
value = @data[key]
|
61
|
+
expect(value[0, num.to_i].is_integer).to be true
|
62
|
+
end
|
63
|
+
|
64
|
+
Then(/^the value for "(.*?)" should have (\d+) upper case letters after a dash$/) do |key, num|
|
65
|
+
value = @data[key]
|
66
|
+
expect(value[4, num.to_i].upcase).to eql value[4, 3]
|
67
|
+
end
|
68
|
+
|
69
|
+
Then(/^the value for "(.*?)" should end with (\d+) lower case letters$/) do |key, num|
|
70
|
+
value = @data[key]
|
71
|
+
expect(value[-1 * num.to_i, num.to_i].downcase).to eql value[-3, 3]
|
72
|
+
end
|
73
|
+
|
74
|
+
Then(/^the value for "(.*?)" should include "(.*?)"$/) do |key, value|
|
75
|
+
expect(@data[key]).to include value
|
76
|
+
end
|
77
|
+
|
78
|
+
Then(/^the value for "(.*?)" should be today's date$/) do |key|
|
79
|
+
expect(@data[key]).to eql Date.today.strftime('%D')
|
80
|
+
end
|
81
|
+
|
82
|
+
Then(/^the value for "(.*?)" should be tomorrow's date$/) do |key|
|
83
|
+
tomorrow = Date.today + 1
|
84
|
+
expect(@data[key]).to eql tomorrow.strftime('%D')
|
85
|
+
end
|
86
|
+
|
87
|
+
Then(/^the value for "(.*?)" should be yesterday's date$/) do |key|
|
88
|
+
yesterday = Date.today - 1
|
89
|
+
expect(@data[key]).to eql yesterday.strftime('%D')
|
90
|
+
end
|
91
|
+
|
92
|
+
Then(/^the value for "(.*?)" should be five days from today$/) do |key|
|
93
|
+
the_day = Date.today + 5
|
94
|
+
expect(@data[key]).to eql the_day.strftime('%D')
|
95
|
+
end
|
96
|
+
|
97
|
+
Then(/^the value for "(.*?)" should be five days ago$/) do |key|
|
98
|
+
the_day = Date.today - 5
|
99
|
+
expect(@data[key]).to eql the_day.strftime('%D')
|
100
|
+
end
|
101
|
+
|
102
|
+
Then(/^the value for "(.*?)" should be a valid month$/) do |key|
|
103
|
+
months = %w[January February March April May June July August September October November December]
|
104
|
+
expect(months).to include @data[key]
|
105
|
+
end
|
106
|
+
|
107
|
+
Then(/^the value for "(.*?)" should be a valid month abbreviation$/) do |key|
|
108
|
+
months = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]
|
109
|
+
expect(months).to include @data[key]
|
110
|
+
end
|
111
|
+
|
112
|
+
Then(/^the value for "(.*?)" should be a valid day$/) do |key|
|
113
|
+
days = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
|
114
|
+
expect(days).to include @data[key]
|
115
|
+
end
|
116
|
+
|
117
|
+
Then(/^the value for "(.*?)" should be a valid day abbreviation$/) do |key|
|
118
|
+
days = %w[Sun Mon Tue Wed Thu Fri Sat]
|
119
|
+
expect(days).to include @data[key]
|
120
|
+
end
|
121
|
+
|
122
|
+
When(/^I add the blah translator$/) do
|
123
|
+
module Blah
|
124
|
+
def blah
|
125
|
+
'foobar'
|
126
|
+
end
|
127
|
+
end
|
128
|
+
DataMagic.add_translator(Blah)
|
129
|
+
|
130
|
+
class TranslatorAdded
|
131
|
+
include DataMagic
|
132
|
+
end
|
133
|
+
ta = TranslatorAdded.new
|
134
|
+
@data = ta.data_for 'dynamic'
|
135
|
+
end
|
136
|
+
|
137
|
+
Then(/^the (?:first|second|third) value for the sequential data should be "(.*?)"$/) do |value|
|
138
|
+
expect(@data['ordered']).to eql value
|
139
|
+
end
|
140
|
+
|
141
|
+
When(/^I ask for the data again$/) do
|
142
|
+
@data = @tc.data_for 'dm'
|
143
|
+
end
|
144
|
+
|
145
|
+
Then(/^the nested value for this is_nested should be "(.*?)"$/) do |value|
|
146
|
+
expect(@data['this']['is_nested']).to eql value
|
147
|
+
end
|
148
|
+
|
149
|
+
Then(/^the nested hash should include (.*?)$/) do |value|
|
150
|
+
expect(@data.keys).to include value
|
151
|
+
end
|
152
|
+
|
153
|
+
Then(/^the value for "(.*?)" should be a phone number$/) do |value|
|
154
|
+
phone = @data[value]
|
155
|
+
if phone.split(' ').length == 2
|
156
|
+
expect(phone).to include '('
|
157
|
+
expect(phone).to include ')'
|
158
|
+
else
|
159
|
+
expect(phone.split(' ').length).to eql 1
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
Then(/^I should be able to call the (.+) translator$/) do |method|
|
164
|
+
value = DataMagic.send method
|
165
|
+
expect(value).not_to be_empty
|
166
|
+
end
|
167
|
+
|
168
|
+
And(/^the value for "([^"]*)" should not mach (.*)$/) do |key, value|
|
169
|
+
expect(@data[key]).not_to eql value
|
170
|
+
end
|
171
|
+
|
172
|
+
Given(/^overwrite @data with (.*)$/) do |hash|
|
173
|
+
# This is a bit dangerous but quick way to make all steps to work with nested data
|
174
|
+
@data = @data['this'][hash]
|
175
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
dm:
|
2
|
+
value1: this is value 1
|
3
|
+
value2: this is value 2
|
4
|
+
full_name: ~full_name
|
5
|
+
first_name: ~first_name
|
6
|
+
last_name: ~last_name
|
7
|
+
name_prefix: ~name_prefix
|
8
|
+
name_suffix: ~name_suffix
|
9
|
+
street: ~street_address
|
10
|
+
street_plus: ~street_address(true)
|
11
|
+
city: ~city
|
12
|
+
state: ~state
|
13
|
+
state_ab: ~state_abbr
|
14
|
+
zip: ~zip_code
|
15
|
+
country: ~country
|
16
|
+
second_address: ~secondary_address
|
17
|
+
company: ~company_name
|
18
|
+
email: ~email_address
|
19
|
+
email_plus: ~email_address('buddy')
|
20
|
+
domain_name: ~domain_name
|
21
|
+
url: ~url
|
22
|
+
user_name: ~user_name
|
23
|
+
phone: ~phone_number
|
24
|
+
cell: ~cell_phone
|
25
|
+
catch_phrase: ~catch_phrase
|
26
|
+
words: ~words
|
27
|
+
sentence: ~sentence
|
28
|
+
sentences: ~sentences
|
29
|
+
paragraphs: ~paragraphs
|
30
|
+
characters: ~characters
|
31
|
+
bool_true: true
|
32
|
+
bool_false: false
|
33
|
+
random: ~randomize ['Tom', 'Dick', 'Harry']
|
34
|
+
ordered: ~sequential ['first', 'second', 'third']
|
35
|
+
range: ~randomize 1..5
|
36
|
+
mask: ~mask "###-AAA_aaa"
|
37
|
+
job_title: ~job_title
|
38
|
+
today: ~today
|
39
|
+
tomorrow: ~tomorrow
|
40
|
+
yesterday: ~yesterday
|
41
|
+
5daysfromtoday: ~5.days_from_today
|
42
|
+
5daysago: ~5.days_ago
|
43
|
+
some_month: ~month
|
44
|
+
month_abbr: ~month_abbr
|
45
|
+
some_day: ~day_of_week
|
46
|
+
day_abbr: ~day_of_week_abbr
|
47
|
+
this:
|
48
|
+
is_nested: Nested Value
|
49
|
+
translate_nested:
|
50
|
+
full_name: ~full_name
|
51
|
+
first_name: ~first_name
|
52
|
+
color_hex: ~color_hex
|
53
|
+
color_name: ~color_name
|
54
|
+
number: ~number(digits = 10, true)
|
55
|
+
decimal: ~decimal(before_decimal = 5, after_decimal = 3)
|
56
|
+
merge: ~merge(separator = '--', [first_name, last_name, number(3)])
|
57
|
+
date_between: ~date_between(from = '01/01/2021', to = '31/12/2021', format = '%d/%m/%y')
|
58
|
+
|
59
|
+
dynamic:
|
60
|
+
blah: ~blah
|
61
|
+
|
62
|
+
other:
|
63
|
+
name: Cheezy
|
64
|
+
address: 123 Main Street
|
65
|
+
email: cheezy@example.com
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DataMagic
|
4
|
+
module DateTranslation
|
5
|
+
#
|
6
|
+
# return today's date
|
7
|
+
#
|
8
|
+
# @param String the format to use for the date. Default is %D
|
9
|
+
#
|
10
|
+
# See http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime
|
11
|
+
# for details of the formats
|
12
|
+
#
|
13
|
+
def today(format = '%D')
|
14
|
+
Date.today.strftime(format)
|
15
|
+
end
|
16
|
+
alias dm_today today
|
17
|
+
|
18
|
+
#
|
19
|
+
# return tomorrow's date
|
20
|
+
#
|
21
|
+
# @param String the format to use for the date. Default is %D
|
22
|
+
#
|
23
|
+
# See http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime
|
24
|
+
# for details of the formats
|
25
|
+
#
|
26
|
+
def tomorrow(format = '%D')
|
27
|
+
tomorrow = Date.today + 1
|
28
|
+
tomorrow.strftime(format)
|
29
|
+
end
|
30
|
+
alias dm_tomorrow tomorrow
|
31
|
+
|
32
|
+
#
|
33
|
+
# return yesterday's date
|
34
|
+
#
|
35
|
+
# @param String the format to use for the date. Default is %D
|
36
|
+
#
|
37
|
+
# See http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime
|
38
|
+
# for details of the formats
|
39
|
+
#
|
40
|
+
def yesterday(format = '%D')
|
41
|
+
yesterday = Date.today - 1
|
42
|
+
yesterday.strftime(format)
|
43
|
+
end
|
44
|
+
alias dm_yesterday yesterday
|
45
|
+
|
46
|
+
#
|
47
|
+
# return a month
|
48
|
+
#
|
49
|
+
def month
|
50
|
+
randomize(Date::MONTHNAMES[1..-1])
|
51
|
+
end
|
52
|
+
alias dm_month month
|
53
|
+
|
54
|
+
#
|
55
|
+
# return a month abbreviation
|
56
|
+
#
|
57
|
+
def month_abbr
|
58
|
+
randomize(Date::ABBR_MONTHNAMES[1..-1])
|
59
|
+
end
|
60
|
+
alias dm_month_abbr month_abbr
|
61
|
+
|
62
|
+
#
|
63
|
+
# return a day of the week
|
64
|
+
#
|
65
|
+
def day_of_week
|
66
|
+
randomize(Date::DAYNAMES)
|
67
|
+
end
|
68
|
+
alias dm_day_of_week day_of_week
|
69
|
+
|
70
|
+
def day_of_week_abbr
|
71
|
+
randomize(Date::ABBR_DAYNAMES)
|
72
|
+
end
|
73
|
+
alias dm_day_of_week_abbr day_of_week_abbr
|
74
|
+
|
75
|
+
##################################################################
|
76
|
+
# #
|
77
|
+
# NEW TRANSLATOR STARTS HERE #
|
78
|
+
# #
|
79
|
+
##################################################################
|
80
|
+
|
81
|
+
#
|
82
|
+
# return random date within the range
|
83
|
+
#
|
84
|
+
def date_between(from = nil, to = nil, format = '%D')
|
85
|
+
raise ArgumentError, 'Invalid date format' if from.to_s.empty? || to.to_s.empty?
|
86
|
+
|
87
|
+
start_date = from.nil? ? Date.today.strftime(format) : Date.strptime(from, format)
|
88
|
+
end_date = to.nil? ? Date.today.strftime(format) : Date.strptime(to, format)
|
89
|
+
|
90
|
+
Faker::Date.between(from: start_date, to: end_date).strftime(format)
|
91
|
+
end
|
92
|
+
alias dm_date_between date_between
|
93
|
+
|
94
|
+
##################################################################
|
95
|
+
# #
|
96
|
+
# NEW TRANSLATOR ENDS HERE #
|
97
|
+
# #
|
98
|
+
##################################################################
|
99
|
+
end
|
100
|
+
end
|