fig_magic 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +17 -0
  4. data/.idea/.name +1 -0
  5. data/.idea/.rakeTasks +7 -0
  6. data/.idea/FigMagic.iml +33 -0
  7. data/.idea/atlassian-ide-plugin.xml +5 -0
  8. data/.idea/encodings.xml +6 -0
  9. data/.idea/misc.xml +14 -0
  10. data/.idea/modules.xml +8 -0
  11. data/.idea/workspace.xml +776 -0
  12. data/.rspec +2 -0
  13. data/.ruby-gemset +1 -0
  14. data/.ruby-version +1 -0
  15. data/ChangeLog +2 -0
  16. data/Gemfile +12 -0
  17. data/Guardfile +16 -0
  18. data/LICENSE +22 -0
  19. data/README.md +145 -0
  20. data/Rakefile +25 -0
  21. data/config/data/default.yml +3 -0
  22. data/config/data/user.yml +3 -0
  23. data/config/environments/default.yml +1 -0
  24. data/config/yaml/sample.yml +1 -0
  25. data/config/yaml/test_config.yml +14 -0
  26. data/cucumber.yml +2 -0
  27. data/features/data_magic.feature +124 -0
  28. data/features/defaults.feature +8 -0
  29. data/features/fig_newton.feature +85 -0
  30. data/features/step_definitions/data_magic_steps.rb +164 -0
  31. data/features/step_definitions/fig_newton_steps.rb +95 -0
  32. data/features/support/env.rb +9 -0
  33. data/features/yaml/another.yml +9 -0
  34. data/features/yaml/example.yml +56 -0
  35. data/fig_magic.gemspec +25 -0
  36. data/lib/fig_magic/core_ext/fixnum.rb +11 -0
  37. data/lib/fig_magic/core_ext/string.rb +5 -0
  38. data/lib/fig_magic/date_translation.rb +73 -0
  39. data/lib/fig_magic/missing.rb +35 -0
  40. data/lib/fig_magic/node.rb +15 -0
  41. data/lib/fig_magic/standard_translation.rb +316 -0
  42. data/lib/fig_magic/translation.rb +14 -0
  43. data/lib/fig_magic/version.rb +3 -0
  44. data/lib/fig_magic.rb +93 -0
  45. data/spec/lib/data_magic_spec.rb +60 -0
  46. data/spec/lib/translation_spec.rb +312 -0
  47. data/spec/spec_helper.rb +30 -0
  48. metadata +158 -0
@@ -0,0 +1,95 @@
1
+ Given (/^I have read the configuration file$/) do
2
+ FigMagic.yml_directory = 'config/yaml'
3
+ FigMagic.load 'test_config.yml'
4
+ end
5
+
6
+ When (/^I have read the default file from the default directory$/) do
7
+ FigMagic.yml = nil
8
+ end
9
+
10
+ When (/^I ask for the value for "([^\"]*)"$/) do |key|
11
+ @value = FigMagic.send key
12
+ end
13
+
14
+ Then (/^I should see "([^\"]*)"$/) do |value|
15
+ expect(@value).to eql value
16
+ end
17
+
18
+ Then (/^I should see (\d+)$/) do |value|
19
+ expect(@value).to eql value.to_i
20
+ end
21
+
22
+ Then (/^I should see true$/) do
23
+ expect(@value).to be true
24
+ end
25
+
26
+ Then (/^I should see false$/) do
27
+ expect(@value).to be false
28
+ end
29
+
30
+ When (/^I ask for a value that does not exist named "([^\"]*)"$/) do |non_existing|
31
+ @does_not_exist = non_existing
32
+ end
33
+
34
+ Then (/^I should raise a NoMethodError exception$/) do
35
+ expect{ FigMagic.send(@does_not_exist) }.to raise_error(NoMethodError)
36
+ end
37
+
38
+ Then (/^I should have a node$/) do
39
+ expect(@value).to be_an_instance_of FigMagic::Node
40
+ end
41
+
42
+ Then (/^the "([^\"]*)" value for the node should be "([^\"]*)"$/) do |key, value|
43
+ expect(@value.send(key)).to eql value
44
+ end
45
+
46
+ When (/^I ask for the node value for "([^\"]*)"$/) do |key|
47
+ @value = @value.send(key)
48
+ end
49
+
50
+ Given (/^I have an environment variable named "([^\"]*)" set to "([^\"]*)"$/) do |env_name, filename|
51
+ FigMagic.yml = nil
52
+ ENV[env_name] = filename
53
+ FigMagic.yml_directory = 'config/yaml'
54
+ FigMagic.instance_variable_set(:@yml, nil)
55
+ end
56
+
57
+ Then (/^the hash of values should look like:$/) do |table|
58
+ expect(table.transpose.hashes.first).to eql @value.to_hash
59
+ end
60
+
61
+ Given (/^I have a yml file that is named after the hostname$/) do
62
+ FigMagic.yml = nil
63
+ FigMagic.yml_directory = 'config/yaml'
64
+ @hostname = Socket.gethostname
65
+ File.open("config/yaml/#{@hostname}.yml", 'w') {|file| file.write("from_the_hostname_file: read from the hostname file\n")}
66
+ end
67
+
68
+ Then (/^I should remove the file$/) do
69
+ File.delete("config/yaml/#{@hostname}.yml")
70
+ end
71
+
72
+ When (/^I ask for a value that does not exist named "(.+)" that has a default value "(.+)"$/) do |key, value|
73
+ @value = FigMagic.send key, value
74
+ end
75
+
76
+ When (/^I ask for a value that does not exist named "(.+)" that has a default block returning "(.+)"$/) do |key, value|
77
+ @value = FigMagic.send(key) {
78
+ value
79
+ }
80
+ end
81
+
82
+ When (/^I ask for a value that does not exist named "(.+)" that has a default lambda returning "(.+)"$/) do |key, value|
83
+ mylambda = lambda {|property| @lambda_property = property; return value}
84
+ @value = FigMagic.send key, &mylambda
85
+ end
86
+
87
+ When (/^I ask for a value that does not exist named "(.+)" that has a default proc returning "(.+)"$/) do |key, value|
88
+ myproc = Proc.new {value}
89
+ @value = FigMagic.send(key, &myproc)
90
+ end
91
+
92
+ Then (/^the lambda should be passed the property "(.+)"$/) do |expected_property|
93
+ expect(@lambda_property).to eq(expected_property)
94
+ end
95
+
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '../../', 'lib'))
2
+
3
+ require 'rspec/expectations'
4
+ require 'fig_magic'
5
+
6
+ Before do
7
+ FigMagic.yml_directory = nil
8
+ FigMagic.yml = nil
9
+ end
@@ -0,0 +1,9 @@
1
+ other_file:
2
+ name: Sneezy
3
+ address: 555 Easy Money Drive
4
+ email: sneezy@example.com
5
+
6
+ more_info:
7
+ name: Wheezy
8
+ address: 999 Alergy Ave
9
+ email: wheezy@example.com
@@ -0,0 +1,56 @@
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
+ title: ~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
+
50
+ dynamic:
51
+ blah: ~blah
52
+
53
+ other:
54
+ name: Cheezy
55
+ address: 123 Main Street
56
+ email: cheezy@example.com
data/fig_magic.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/fig_magic/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.name = "fig_magic"
6
+ gem.version = FigMagic::VERSION
7
+ gem.platform = Gem::Platform::RUBY
8
+ gem.authors = ["Justin Commu"]
9
+ gem.email = ["jcommu@gmail.com"]
10
+ gem.license = 'MIT'
11
+ gem.homepage = "http://github.com/dingokiller/fig_magic"
12
+ gem.summary = %q{Combines FigNewton & DataMagic into a single gem & namespace}
13
+ gem.description = %q{Provides datasets to application stored in YAML files}
14
+
15
+ gem.files = `git ls-files`.split("\n")
16
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'faker', '>= 1.1.2'
21
+ gem.add_dependency 'yml_reader', '>= 0.5'
22
+
23
+ gem.add_development_dependency 'rspec', '>= 2.12.0'
24
+ gem.add_development_dependency 'cucumber', '>= 1.2.0'
25
+ end
@@ -0,0 +1,11 @@
1
+ class Fixnum
2
+ def days_from_today(format = '%D')
3
+ the_day = Date.today + self
4
+ the_day.strftime(format)
5
+ end
6
+
7
+ def days_ago(format = '%D')
8
+ the_day = Date.today - self
9
+ the_day.strftime(format)
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ class String
2
+ def is_integer
3
+ true if Integer(self) rescue false
4
+ end
5
+ end
@@ -0,0 +1,73 @@
1
+ module FigMagic
2
+ module DateTranslation
3
+ #
4
+ # return today's date
5
+ #
6
+ # @param String the format to use for the date. Default is %D
7
+ #
8
+ # See http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime
9
+ # for details of the formats
10
+ #
11
+ def today(format = '%D')
12
+ Date.today.strftime(format)
13
+ end
14
+ alias_method :dm_today, :today
15
+
16
+ #
17
+ # return tomorrow's date
18
+ #
19
+ # @param String the format to use for the date. Default is %D
20
+ #
21
+ # See http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime
22
+ # for details of the formats
23
+ #
24
+ def tomorrow(format = '%D')
25
+ tomorrow = Date.today + 1
26
+ tomorrow.strftime(format)
27
+ end
28
+ alias_method :dm_tomorrow, :tomorrow
29
+
30
+ #
31
+ # return yesterday's date
32
+ #
33
+ # @param String the format to use for the date. Default is %D
34
+ #
35
+ # See http://ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-i-strftime
36
+ # for details of the formats
37
+ #
38
+ def yesterday(format = '%D')
39
+ yesterday = Date.today - 1
40
+ yesterday.strftime(format)
41
+ end
42
+ alias_method :dm_yesterday, :yesterday
43
+
44
+ #
45
+ # return a month
46
+ #
47
+ def month
48
+ randomize(Date::MONTHNAMES[1..-1])
49
+ end
50
+ alias_method :dm_month, :month
51
+
52
+ #
53
+ # return a month abbreviation
54
+ #
55
+ def month_abbr
56
+ randomize(Date::ABBR_MONTHNAMES[1..-1])
57
+ end
58
+ alias_method :dm_month_abbr, :month_abbr
59
+
60
+ #
61
+ # return a day of the week
62
+ #
63
+ def day_of_week
64
+ randomize(Date::DAYNAMES)
65
+ end
66
+ alias_method :dm_day_of_week, :day_of_week
67
+
68
+ def day_of_week_abbr
69
+ randomize(Date::ABBR_DAYNAMES)
70
+ end
71
+ alias_method :dm_day_of_week_abbr, :day_of_week_abbr
72
+ end
73
+ end
@@ -0,0 +1,35 @@
1
+ require 'yaml'
2
+ require 'socket'
3
+
4
+ module FigNewton
5
+ module Missing
6
+ def method_missing(*args, &block)
7
+ read_file unless @yml
8
+ m = args.first
9
+ value = @yml[m.to_s]
10
+ value = args[1] if value.nil?
11
+ value = block.call(m.to_s) if value.nil? and block
12
+ super if value.nil?
13
+ value = FigNewton::Node.new(value) unless type_known? value
14
+ value
15
+ end
16
+
17
+ def read_file
18
+ @yml = nil
19
+ @yml = YAML.load_file "#{yml_directory}/#{ENV['FIG_NEWTON_FILE']}" if ENV['FIG_NEWTON_FILE']
20
+ unless @yml
21
+ hostname = Socket.gethostname
22
+ hostfile = "#{yml_directory}/#{hostname}.yml"
23
+ @yml = YAML.load_file hostfile if File.exist? hostfile
24
+ end
25
+ FigNewton.load('default.yml') if @yml.nil?
26
+ end
27
+
28
+ private
29
+
30
+ def type_known?(value)
31
+ known_types = [String, Integer, TrueClass, FalseClass]
32
+ known_types.any? { |type| value.kind_of? type }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/missing"
2
+
3
+ module FigNewton
4
+ class Node
5
+ include FigNewton::Missing
6
+
7
+ def initialize(yml)
8
+ @yml = yml
9
+ end
10
+
11
+ def to_hash
12
+ @yml
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,316 @@
1
+ module FigMagic
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 a credit card number
129
+ #
130
+ def credit_card_number
131
+ Faker::Business.credit_card_number
132
+ end
133
+ alias_method :dm_credit_card_number, :credit_card_number
134
+
135
+ #
136
+ # return a credit card type
137
+ #
138
+ def credit_card_type
139
+ Faker::Business.credit_card_type
140
+ end
141
+ alias_method :dm_credit_card_type, :credit_card_type
142
+
143
+ #
144
+ # return random words - default is 3 words
145
+ #
146
+ def words(number = 3)
147
+ Faker::Lorem.words(number).join(' ')
148
+ end
149
+ alias_method :dm_words, :words
150
+
151
+ #
152
+ # return a random sentence - default minimum word count is 4
153
+ #
154
+ def sentence(min_word_count = 4)
155
+ Faker::Lorem.sentence(min_word_count)
156
+ end
157
+ alias_method :dm_sentence, :sentence
158
+
159
+ #
160
+ # return random sentences - default is 3 sentences
161
+ #
162
+ def sentences(sentence_count = 3)
163
+ Faker::Lorem.sentences(sentence_count).join(' ')
164
+ end
165
+ alias_method :dm_sentences, :sentences
166
+
167
+ #
168
+ # return random paragraphs - default is 3 paragraphs
169
+ #
170
+ def paragraphs(paragraph_count = 3)
171
+ Faker::Lorem.paragraphs(paragraph_count).join('\n\n')
172
+ end
173
+ alias_method :dm_paragraphs, :paragraphs
174
+
175
+ #
176
+ # return random characters - default is 255 characters
177
+ #
178
+ def characters(character_count = 255)
179
+ Faker::Lorem.characters(character_count)
180
+ end
181
+ alias_method :dm_characters, :characters
182
+
183
+ #
184
+ # return a random email address
185
+ #
186
+ def email_address(name=nil)
187
+ Faker::Internet.email(name)
188
+ end
189
+ alias_method :dm_email_address, :email_address
190
+
191
+ #
192
+ # return a random domain name
193
+ #
194
+ def domain_name
195
+ Faker::Internet.domain_name
196
+ end
197
+ alias_method :dm_domain_name, :domain_name
198
+
199
+ #
200
+ # return a random url
201
+ #
202
+ def url
203
+ Faker::Internet.url
204
+ end
205
+ alias_method :dm_url, :url
206
+
207
+ #
208
+ # return a random user name
209
+ #
210
+ def user_name
211
+ Faker::Internet.user_name
212
+ end
213
+ alias_method :dm_user_name, :user_name
214
+
215
+ #
216
+ # return a random phone number
217
+ #
218
+ def phone_number
219
+ value = Faker::PhoneNumber.phone_number
220
+ remove_extension(value)
221
+ end
222
+ alias_method :dm_phone_number, :phone_number
223
+
224
+ #
225
+ # return a random cell number
226
+ #
227
+ def cell_phone
228
+ value = Faker::PhoneNumber.cell_phone
229
+ remove_extension(value)
230
+ end
231
+ alias_method :dm_cell_phone, :cell_phone
232
+
233
+ #
234
+ # return a random value from an array or range
235
+ #
236
+ def randomize(value)
237
+ case value
238
+ when Array then value[rand(value.size)]
239
+ when Range then rand((value.last+1) - value.first) + value.first
240
+ else value
241
+ end
242
+ end
243
+ alias_method :dm_randomize, :randomize
244
+
245
+ #
246
+ # return an element from the array. The first request will return
247
+ # the first element, the second request will return the second,
248
+ # and so forth.
249
+ #
250
+ def sequential(value)
251
+ index = index_variable_for(value)
252
+ index = (index ? index + 1 : 0)
253
+ index = 0 if index == value.length
254
+ set_index_variable(value, index)
255
+ value[index]
256
+ end
257
+
258
+ #
259
+ # return a value based on a mast
260
+ # The # character will be replaced with a number
261
+ # The A character will be replaced with an upper case letter
262
+ # The a character will be replaced with a lower case letter
263
+ #
264
+ def mask(value)
265
+ result = ''
266
+ value.each_char do |ch|
267
+ case ch
268
+ when '#' then result += randomize(0..9).to_s
269
+ when 'A' then result += ('A'..'Z').to_a[rand(26)]
270
+ when 'a' then result += ('a'..'z').to_a[rand(26)]
271
+ else result += ch
272
+ end
273
+ end
274
+ result
275
+ end
276
+ alias_method :dm_mask, :mask
277
+
278
+
279
+
280
+ private
281
+
282
+ def set_index_variable(ary, value)
283
+ index_hash[index_name(ary)] = value
284
+ end
285
+
286
+ def index_variable_for(ary)
287
+ value = index_hash[index_name(ary)]
288
+ index_hash[index_name(ary)] = -1 unless value
289
+ index_hash[index_name(ary)]
290
+ end
291
+
292
+ def index_name(ary)
293
+ "#{ary[0]}#{ary[1]}_index".gsub(' ', '_').downcase
294
+ end
295
+
296
+ def index_hash
297
+ dh = data_hash[parent]
298
+ data_hash[parent] = {} unless dh
299
+ data_hash[parent]
300
+ end
301
+
302
+ def data_hash
303
+ $fig_magic_data_hash ||= {}
304
+ end
305
+
306
+ def process(value)
307
+ eval value
308
+ end
309
+
310
+ def remove_extension(phone)
311
+ index = phone.index('x')
312
+ phone = phone[0, (index-1)] if index
313
+ phone
314
+ end
315
+ end
316
+ end
@@ -0,0 +1,14 @@
1
+ require 'fig_magic/date_translation'
2
+ require 'fig_magic/standard_translation'
3
+
4
+ module FigMagic
5
+ class Translation
6
+ include StandardTranslation
7
+ include DateTranslation
8
+
9
+ def initialize(parent)
10
+ @parent = parent
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module FigMagic
2
+ VERSION = "0.1"
3
+ end