data_magic 0.19 → 0.20

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ccfe171cef2e6531ccb7f404aaf9518264abb48e
4
- data.tar.gz: 8ea09be1c4c8d01b5eb5f0bc5b5d818e709820c2
3
+ metadata.gz: ff25f8071c1ca6ce22e590a4b777580455462a79
4
+ data.tar.gz: d55354c90fa70805f751911bb7e4eb265c28e180
5
5
  SHA512:
6
- metadata.gz: fd55e139c938a7762013aada0e8b786bdd0cf267e7bedb9278bfe329dccf8f76f527215192e4c926060b0c12c5e6a4345fb9416f8dfa84ad4544b3f7650a8dd4
7
- data.tar.gz: 37074915707e495cccded1682cdf50f5dfd84a21f510b4d59efea3e1fcf73727cc84fff4b650403c36c2e034bcd2627e3bc29e9977221a014859c27490a43c93
6
+ metadata.gz: 6ef9572077dd3c9e779d52eebf3479675b127dbaeea21a85c66fdc7fbff7d741e3aa91b5892248359234ecc84b03b39fcda6820c67c9abd171df67d98f328110
7
+ data.tar.gz: fa514dc15cd7457f3c64dc0a5358a20d485eae611f4c3b6538a0c13105b510394bd8139922065c75f35bdaf144ebc627c7eb0dadacf4f107238df92d4ffe6e67
@@ -1 +1 @@
1
- ruby-2.0.0-p353
1
+ ruby-2.1.2
@@ -1,8 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
- - 1.9.2
5
- - 1.9.3
6
3
  - 2.0.0
7
- - rbx-19mode
4
+ - 2.1.0
5
+
8
6
 
data/ChangeLog CHANGED
@@ -1,3 +1,8 @@
1
+ === Version 0.20 / 2014-9-21
2
+ * Enhancements
3
+ * Added the ability to nest entries under headings
4
+ * Added the ability to call the translation methods directlry on DataMagic module
5
+
1
6
  === Version 0.19 / 2014-6-1
2
7
  * Enhancements
3
8
  * Can now include ERB in the yml file due to update in yml_reader
data/Guardfile CHANGED
@@ -1,7 +1,7 @@
1
1
  # A sample Guardfile
2
2
  # More info at https://github.com/guard/guard#readme
3
3
 
4
- guard 'rspec', :cli => '--color --format Fuubar' do
4
+ guard 'rspec', cmd: 'rspec --color --format Fuubar' do
5
5
  watch(%r{^spec/.+_spec\.rb$})
6
6
  watch(%r{^lib/(.+)\.rb$}) { "spec" }
7
7
  watch('spec/spec_helper.rb') { "spec" }
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency 'faker', '>= 1.1.2'
21
- gem.add_dependency 'yml_reader', '>= 0.3'
21
+ gem.add_dependency 'yml_reader', '>= 0.4'
22
22
 
23
23
  gem.add_development_dependency 'rspec', '>= 2.12.0'
24
24
  gem.add_development_dependency 'cucumber', '>= 1.2.0'
@@ -37,8 +37,8 @@ Feature: Functionality of the data_magic gem
37
37
  And the value for "url" should include "http://"
38
38
 
39
39
  Scenario: Getting a phone number
40
- Then the value for "phone" should be 1 word long
41
- And the value for "cell" should be 1 word long
40
+ Then the value for "phone" should be a phone number
41
+ And the value for "cell" should be a phone number
42
42
 
43
43
  Scenario: Random phrases
44
44
  Then the value for "catch_phrase" should exist
@@ -114,3 +114,11 @@ Feature: Functionality of the data_magic gem
114
114
  Scenario: It should allow one to add new translator methods
115
115
  When I add the blah translator
116
116
  Then the value for "blah" should be "foobar"
117
+
118
+ Scenario: Getting values from nested entries
119
+ Then the nested value for this is_nested should be "Nested Value"
120
+
121
+ Scenario: Should be able to call the translator methods on DataMagic module
122
+ Then I should be able to call the full_name translator
123
+ And I should be able to call the state translator
124
+ And I should be able to call the today translator
@@ -20,27 +20,27 @@ end
20
20
 
21
21
 
22
22
  Then /^the value for "(.+)" should be "(.+)"$/ do |key, value|
23
- @data[key].should == value
23
+ expect(@data[key]).to eql value
24
24
  end
25
25
 
26
26
  Then /^the value for "(.+)" should be (true|false)$/ do |key, value|
27
- @data[key].should == eval(value)
27
+ expect(@data[key]).to eql eval(value)
28
28
  end
29
29
 
30
30
  Then /^the value for "(.+)" should be (\d+) word|words long$/ do |key, length|
31
- @data[key].split(' ').size.should == length.to_i
31
+ expect(@data[key].split(' ').size).to eql length.to_i
32
32
  end
33
33
 
34
34
  Then /^the value for "(.+)" should have a minimum of (\d+) word|wordss$/ do |key, length|
35
- @data[key].split(' ').size.should >= length.to_i
35
+ expect(@data[key].split(' ').size).to be >= length.to_i
36
36
  end
37
37
 
38
38
  Then /^the value for "(.*?)" should be (\d+) characters long$/ do |key, length|
39
- @data[key].length.should == length.to_i
39
+ expect(@data[key].length).to eql length.to_i
40
40
  end
41
41
 
42
42
  Then /^the value for "(.+)" should exist$/ do |key|
43
- @data[key].should_not be_nil
43
+ expect(@data[key]).not_to be_nil
44
44
  end
45
45
 
46
46
  When /^I load the file "(.+)"$/ do |file_name|
@@ -48,76 +48,76 @@ When /^I load the file "(.+)"$/ do |file_name|
48
48
  end
49
49
 
50
50
  Then /^the value for "(.*?)" should be either "(.*?)", "(.*?)", or "(.*?)"$/ do |key, vala, valb, valc|
51
- [vala, valb, valc].should include @data[key]
51
+ expect([vala, valb, valc]).to include @data[key]
52
52
  end
53
53
 
54
54
  Then /^the value for "(.*?)" should be between (\d+) and (\d+)$/ do |key, low, high|
55
55
  value = @data[key]
56
- value.should >= low.to_i
57
- value.should <= high.to_i
56
+ expect(value).to be >= low.to_i
57
+ expect(value).to be <= high.to_i
58
58
  end
59
59
 
60
60
  Then /^the value for "(.*?)" should begin with (\d+) numbers$/ do |key, num|
61
61
  value = @data[key]
62
- value[0,num.to_i].is_integer.should be_true
62
+ expect(value[0,num.to_i].is_integer).to be true
63
63
  end
64
64
 
65
65
  Then /^the value for "(.*?)" should have (\d+) upper case letters after a dash$/ do |key, num|
66
66
  value = @data[key]
67
- value[4,num.to_i].upcase.should == value[4,3]
67
+ expect(value[4,num.to_i].upcase).to eql value[4,3]
68
68
  end
69
69
 
70
70
  Then /^the value for "(.*?)" should end with (\d+) lower case letters$/ do |key, num|
71
71
  value = @data[key]
72
- value[-1 * num.to_i,num.to_i].downcase.should == value[-3,3]
72
+ expect(value[-1 * num.to_i,num.to_i].downcase).to eql value[-3,3]
73
73
  end
74
74
 
75
75
  Then /^the value for "(.*?)" should include "(.*?)"$/ do |key, value|
76
- @data[key].should include value
76
+ expect(@data[key]).to include value
77
77
  end
78
78
 
79
79
  Then /^the value for "(.*?)" should be today\'s date$/ do |key|
80
- @data[key].should == Date.today.strftime('%D')
80
+ expect(@data[key]).to eql Date.today.strftime('%D')
81
81
  end
82
82
 
83
83
  Then /^the value for "(.*?)" should be tomorrow\'s date$/ do |key|
84
84
  tomorrow = Date.today + 1
85
- @data[key].should == tomorrow.strftime('%D')
85
+ expect(@data[key]).to eql tomorrow.strftime('%D')
86
86
  end
87
87
 
88
88
  Then /^the value for "(.*?)" should be yesterday\'s date$/ do |key|
89
89
  yesterday = Date.today - 1
90
- @data[key].should == yesterday.strftime('%D')
90
+ expect(@data[key]).to eql yesterday.strftime('%D')
91
91
  end
92
92
 
93
93
  Then /^the value for "(.*?)" should be five days from today$/ do |key|
94
94
  the_day = Date.today + 5
95
- @data[key].should == the_day.strftime('%D')
95
+ expect(@data[key]).to eql the_day.strftime('%D')
96
96
  end
97
97
 
98
98
  Then /^the value for "(.*?)" should be five days ago$/ do |key|
99
99
  the_day = Date.today - 5
100
- @data[key].should == the_day.strftime('%D')
100
+ expect(@data[key]).to eql the_day.strftime('%D')
101
101
  end
102
102
 
103
103
  Then /^the value for "(.*?)" should be a valid month$/ do |key|
104
104
  months = %w[January February March April May June July August September October November December]
105
- months.should include @data[key]
105
+ expect(months).to include @data[key]
106
106
  end
107
107
 
108
108
  Then /^the value for "(.*?)" should be a valid month abbreviation$/ do |key|
109
109
  months = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]
110
- months.should include @data[key]
110
+ expect(months).to include @data[key]
111
111
  end
112
112
 
113
113
  Then /^the value for "(.*?)" should be a valid day$/ do |key|
114
114
  days = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
115
- days.should include @data[key]
115
+ expect(days).to include @data[key]
116
116
  end
117
117
 
118
118
  Then /^the value for "(.*?)" should be a valid day abbreviation$/ do |key|
119
119
  days = %w[Sun Mon Tue Wed Thu Fri Sat]
120
- days.should include @data[key]
120
+ expect(days).to include @data[key]
121
121
  end
122
122
 
123
123
  When /^I add the blah translator$/ do
@@ -136,9 +136,28 @@ When /^I add the blah translator$/ do
136
136
  end
137
137
 
138
138
  Then(/^the (?:first|second|third) value for the sequential data should be "(.*?)"$/) do |value|
139
- @data['ordered'].should == value
139
+ expect(@data['ordered']).to eql value
140
140
  end
141
141
 
142
142
  When(/^I ask for the data again$/) do
143
143
  @data = @tc.data_for 'dm'
144
144
  end
145
+
146
+ Then(/^the nested value for this is_nested should be "(.*?)"$/) do |value|
147
+ expect(@data['this']['is_nested']).to eql value
148
+ end
149
+
150
+ Then(/^the value for "(.*?)" should be a phone number$/) do |value|
151
+ phone = @data[value]
152
+ if phone.split(' ').length == 2
153
+ expect(phone).to include "("
154
+ expect(phone).to include ")"
155
+ else
156
+ expect(phone.split(' ').length).to eql 1
157
+ end
158
+ end
159
+
160
+ Then(/^I should be able to call the (.+) translator$/) do |method|
161
+ value = DataMagic.send method
162
+ expect(value).not_to be_empty
163
+ end
@@ -44,6 +44,8 @@ dm:
44
44
  month_abbr: ~month_abbr
45
45
  some_day: ~day_of_week
46
46
  day_abbr: ~day_of_week_abbr
47
+ this:
48
+ is_nested: Nested Value
47
49
 
48
50
  dynamic:
49
51
  blah: ~blah
@@ -2,11 +2,15 @@ require 'data_magic/core_ext/string'
2
2
  require 'data_magic/core_ext/fixnum'
3
3
  require "data_magic/version"
4
4
  require "data_magic/translation"
5
+ require 'data_magic/date_translation'
6
+ require 'data_magic/standard_translation'
5
7
  require 'yml_reader'
6
8
  require 'faker'
7
9
 
8
10
  module DataMagic
9
11
  extend YmlReader
12
+ extend StandardTranslation
13
+ extend DateTranslation
10
14
 
11
15
  attr_reader :parent
12
16
 
@@ -42,6 +46,7 @@ module DataMagic
42
46
  data.each do |key, value|
43
47
  unless value.nil?
44
48
  next if !value.respond_to?('[]') || value.is_a?(Numeric)
49
+ next if value.is_a?(Hash)
45
50
  data[key] = translate(value[1..-1]) if value[0,1] == "~"
46
51
  end
47
52
  end
@@ -0,0 +1,300 @@
1
+ module DataMagic
2
+ module StandardTranslation
3
+
4
+ attr_reader :parent
5
+
6
+ #
7
+ # return a random name (first and last)
8
+ #
9
+ def full_name
10
+ Faker::Name.name
11
+ end
12
+ alias_method :dm_full_name, :full_name
13
+
14
+ #
15
+ # return a random first name
16
+ #
17
+ def first_name
18
+ Faker::Name.first_name
19
+ end
20
+ alias_method :dm_first_name, :first_name
21
+
22
+ #
23
+ # return a random last name
24
+ #
25
+ def last_name
26
+ Faker::Name.last_name
27
+ end
28
+ alias_method :dm_last_name, :last_name
29
+
30
+ #
31
+ # return a random name prefix
32
+ #
33
+ def name_prefix
34
+ Faker::Name.prefix
35
+ end
36
+ alias_method :dm_name_prefix, :name_prefix
37
+
38
+ #
39
+ # return a random name suffix
40
+ #
41
+ def name_suffix
42
+ Faker::Name.suffix
43
+ end
44
+ alias_method :dm_name_suffix, :name_suffix
45
+
46
+ #
47
+ # return a random title
48
+ #
49
+ def title
50
+ Faker::Name.title
51
+ end
52
+ alias_method :dm_title, :title
53
+
54
+ #
55
+ # return a random street address
56
+ #
57
+ def street_address(include_secondary=false)
58
+ Faker::Address.street_address(include_secondary)
59
+ end
60
+ alias_method :dm_street_address, :street_address
61
+
62
+ #
63
+ # return a random secondary address
64
+ #
65
+ def secondary_address
66
+ Faker::Address.secondary_address
67
+ end
68
+ alias_method :dm_secondary_address, :secondary_address
69
+
70
+ #
71
+ # return a random city
72
+ #
73
+ def city
74
+ Faker::Address.city
75
+ end
76
+ alias_method :dm_city, :city
77
+
78
+ #
79
+ # return a random state
80
+ #
81
+ def state
82
+ Faker::Address.state
83
+ end
84
+ alias_method :dm_state, :state
85
+
86
+ #
87
+ # return a random state abbreviation
88
+ #
89
+ def state_abbr
90
+ Faker::Address.state_abbr
91
+ end
92
+ alias_method :dm_state_abbr, :state_abbr
93
+
94
+ #
95
+ # return a random 5 or 9 digit zip code
96
+ #
97
+ def zip_code
98
+ Faker::Address.zip
99
+ end
100
+ alias_method :dm_zip_code, :zip_code
101
+
102
+ #
103
+ # return a random country
104
+ #
105
+ def country
106
+ Faker::Address.country
107
+ end
108
+ alias_method :dm_country, :country
109
+
110
+
111
+ #
112
+ # return a random company name
113
+ #
114
+ def company_name
115
+ Faker::Company.name
116
+ end
117
+ alias_method :dm_company_name, :company_name
118
+
119
+ #
120
+ # return a random catch phrase
121
+ #
122
+ def catch_phrase
123
+ Faker::Company.catch_phrase
124
+ end
125
+ alias_method :dm_catch_phrase, :catch_phrase
126
+
127
+ #
128
+ # return random words - default is 3 words
129
+ #
130
+ def words(number = 3)
131
+ Faker::Lorem.words(number).join(' ')
132
+ end
133
+ alias_method :dm_words, :words
134
+
135
+ #
136
+ # return a random sentence - default minimum word count is 4
137
+ #
138
+ def sentence(min_word_count = 4)
139
+ Faker::Lorem.sentence(min_word_count)
140
+ end
141
+ alias_method :dm_sentence, :sentence
142
+
143
+ #
144
+ # return random sentences - default is 3 sentences
145
+ #
146
+ def sentences(sentence_count = 3)
147
+ Faker::Lorem.sentences(sentence_count).join(' ')
148
+ end
149
+ alias_method :dm_sentences, :sentences
150
+
151
+ #
152
+ # return random paragraphs - default is 3 paragraphs
153
+ #
154
+ def paragraphs(paragraph_count = 3)
155
+ Faker::Lorem.paragraphs(paragraph_count).join('\n\n')
156
+ end
157
+ alias_method :dm_paragraphs, :paragraphs
158
+
159
+ #
160
+ # return random characters - default is 255 characters
161
+ #
162
+ def characters(character_count = 255)
163
+ Faker::Lorem.characters(character_count)
164
+ end
165
+ alias_method :dm_characters, :characters
166
+
167
+ #
168
+ # return a random email address
169
+ #
170
+ def email_address(name=nil)
171
+ Faker::Internet.email(name)
172
+ end
173
+ alias_method :dm_email_address, :email_address
174
+
175
+ #
176
+ # return a random domain name
177
+ #
178
+ def domain_name
179
+ Faker::Internet.domain_name
180
+ end
181
+ alias_method :dm_domain_name, :domain_name
182
+
183
+ #
184
+ # return a random url
185
+ #
186
+ def url
187
+ Faker::Internet.url
188
+ end
189
+ alias_method :dm_url, :url
190
+
191
+ #
192
+ # return a random user name
193
+ #
194
+ def user_name
195
+ Faker::Internet.user_name
196
+ end
197
+ alias_method :dm_user_name, :user_name
198
+
199
+ #
200
+ # return a random phone number
201
+ #
202
+ def phone_number
203
+ value = Faker::PhoneNumber.phone_number
204
+ remove_extension(value)
205
+ end
206
+ alias_method :dm_phone_number, :phone_number
207
+
208
+ #
209
+ # return a random cell number
210
+ #
211
+ def cell_phone
212
+ value = Faker::PhoneNumber.cell_phone
213
+ remove_extension(value)
214
+ end
215
+ alias_method :dm_cell_phone, :cell_phone
216
+
217
+ #
218
+ # return a random value from an array or range
219
+ #
220
+ def randomize(value)
221
+ case value
222
+ when Array then value[rand(value.size)]
223
+ when Range then rand((value.last+1) - value.first) + value.first
224
+ else value
225
+ end
226
+ end
227
+ alias_method :dm_randomize, :randomize
228
+
229
+ #
230
+ # return an element from the array. The first request will return
231
+ # the first element, the second request will return the second,
232
+ # and so forth.
233
+ #
234
+ def sequential(value)
235
+ index = index_variable_for(value)
236
+ index = (index ? index + 1 : 0)
237
+ index = 0 if index == value.length
238
+ set_index_variable(value, index)
239
+ value[index]
240
+ end
241
+
242
+ #
243
+ # return a value based on a mast
244
+ # The # character will be replaced with a number
245
+ # The A character will be replaced with an upper case letter
246
+ # The a character will be replaced with a lower case letter
247
+ #
248
+ def mask(value)
249
+ result = ''
250
+ value.each_char do |ch|
251
+ case ch
252
+ when '#' then result += randomize(0..9).to_s
253
+ when 'A' then result += ('A'..'Z').to_a[rand(26)]
254
+ when 'a' then result += ('a'..'z').to_a[rand(26)]
255
+ else result += ch
256
+ end
257
+ end
258
+ result
259
+ end
260
+ alias_method :dm_mask, :mask
261
+
262
+
263
+
264
+ private
265
+
266
+ def set_index_variable(ary, value)
267
+ index_hash[index_name(ary)] = value
268
+ end
269
+
270
+ def index_variable_for(ary)
271
+ value = index_hash[index_name(ary)]
272
+ index_hash[index_name(ary)] = -1 unless value
273
+ index_hash[index_name(ary)]
274
+ end
275
+
276
+ def index_name(ary)
277
+ "#{ary[0]}#{ary[1]}_index".gsub(' ', '_').downcase
278
+ end
279
+
280
+ def index_hash
281
+ dh = data_hash[parent]
282
+ data_hash[parent] = {} unless dh
283
+ data_hash[parent]
284
+ end
285
+
286
+ def data_hash
287
+ $data_magic_data_hash ||= {}
288
+ end
289
+
290
+ def process(value)
291
+ eval value
292
+ end
293
+
294
+ def remove_extension(phone)
295
+ index = phone.index('x')
296
+ phone = phone[0, (index-1)] if index
297
+ phone
298
+ end
299
+ end
300
+ end