dummy 0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,30 @@
1
+ module Dummy
2
+ module PhoneNumber
3
+ extend self
4
+
5
+ def phone_number
6
+ Dummy.numerify case rand(20)
7
+ when 0 then "###-###-#### x#####"
8
+ when 1 then "###-###-#### x####"
9
+ when 2 then "###-###-#### x###"
10
+ when 3..4 then "###-###-####"
11
+ when 5 then "###.###.#### x#####"
12
+ when 6 then "###.###.#### x####"
13
+ when 7 then "###.###.#### x###"
14
+ when 8..9 then "###.###.####"
15
+ when 10 then "(###)###-#### x#####"
16
+ when 11 then "(###)###-#### x####"
17
+ when 12 then "(###)###-#### x###"
18
+ when 13..14 then "(###)###-####"
19
+ when 15 then "1-###-###-#### x#####"
20
+ when 16 then "1-###-###-#### x####"
21
+ when 17 then "1-###-###-#### x###"
22
+ when 18..19 then "1-###-###-####"
23
+ end
24
+ end
25
+
26
+ def phone_number_short
27
+ Dummy.numerify("###-###-####")
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,185 @@
1
+ require "yaml"
2
+ require "active_support"
3
+ require "active_record"
4
+ require "rails/generators"
5
+
6
+ module Dummy
7
+ class DataGenerator < Rails::Generators::Base
8
+ def self.source_root
9
+ @source_root ||= File.expand_path("../templates", __FILE__)
10
+ end
11
+
12
+ class_option :base_amount, :type => :numeric, :default => 10,
13
+ :desc => "The base amount of records to generate for each model."
14
+ class_option :growth_ratio, :type => :numeric, :default => 2.0,
15
+ :desc => "The growth ratio of each model, according to its associations."
16
+
17
+ def install_dummy
18
+ initialize_application
19
+ generate_dummy_data
20
+ copy_rake_file
21
+ end
22
+
23
+ private
24
+
25
+ def initialize_application
26
+ require File.expand_path("#{Rails.root}/config/environment.rb")
27
+ say_status :successful, "initialize Rails application"
28
+ end
29
+
30
+ def generate_dummy_data
31
+ get_table_names
32
+ gather_associations
33
+ predict_record_amounts
34
+ generate_and_write_data
35
+ end
36
+
37
+ def get_table_names
38
+ @models = Hash.new
39
+ Dir["app/models/*.rb"].each do |full_path|
40
+ model = File.basename(full_path).chomp(File.extname(full_path)).camelcase.constantize
41
+ @models.merge!({model => {
42
+ :record_amount => 0, :associations => []
43
+ }}) if model.respond_to?(:columns)
44
+ end
45
+ end
46
+
47
+ def gather_associations
48
+ @models.each_key do |model|
49
+ model_symbol = model.to_s.underscore.pluralize.to_sym
50
+ associations = model.reflect_on_all_associations(:belongs_to)
51
+
52
+ associations.each do |assoc|
53
+ assoc_name = assoc.name.to_s.camelcase
54
+ assoc_options = assoc.options
55
+
56
+ if assoc_options.empty?
57
+ @models[model][:associations].push({
58
+ :model => assoc_name.constantize,
59
+ :foreign_key => "#{assoc_name.underscore}_id"
60
+ })
61
+ elsif assoc_options.has_key?(:class_name) and assoc_options.has_key?(:foreign_key)
62
+ @models[model][:associations].push({
63
+ :model => assoc_options[:class_name].constantize, # TODO: handle class_name
64
+ :foreign_key => assoc_options[:foreign_key]
65
+ })
66
+ else
67
+ next
68
+ end
69
+
70
+ assoc_model = @models[model][:associations].last[:model]
71
+ assoc_reflections = assoc_model.reflect_on_all_associations(:has_one)
72
+ @models[model][:associations].pop if assoc_reflections.map(&:name).include?(model_symbol)
73
+ end
74
+ end
75
+ end
76
+
77
+ def predict_record_amounts
78
+ models = @models.dup
79
+ models.each do |model, info|
80
+ predict_record_amount(model, info, models, [])
81
+ end
82
+ end
83
+
84
+ def predict_record_amount(model, info, models, stacked_models)
85
+ info[:associations].each do |assoc|
86
+ next if stacked_models.include?(assoc[:model])
87
+
88
+ if model != assoc[:model]
89
+ stacked_models << assoc[:model]
90
+ predict_record_amount(assoc[:model], @models[assoc[:model]], models, stacked_models)
91
+ end
92
+ end
93
+
94
+ amount = options.base_amount
95
+ if not info[:associations].empty?
96
+ amount = info[:associations].map do |assoc|
97
+ @models[assoc[:model]][:record_amount]
98
+ end.max*options.growth_ratio # **info[:associations].size
99
+ end
100
+
101
+ @models[model][:record_amount] = amount.to_i
102
+ stacked_models.delete(model)
103
+ models.delete(model)
104
+ end
105
+
106
+ def generate_and_write_data
107
+ empty_directory "test/dummy"
108
+ data = Hash.new
109
+
110
+ @models.each do |model, info|
111
+ name = model.to_s.underscore
112
+
113
+ (0..info[:record_amount]-1).each do |num|
114
+ key_value = Hash.new
115
+ fixture_data = Hash.new
116
+
117
+ model.columns.each do |column|
118
+ key_value = generate_record_data(name, info, column)
119
+ fixture_data.merge!(key_value) unless key_value.nil?
120
+ end
121
+
122
+ data[model.table_name] = Hash.new if data[model.table_name].nil?
123
+ data[model.table_name].merge!({ "#{name}_#{num}" => fixture_data })
124
+ end
125
+
126
+ say_status :successful, "generate #{info[:record_amount]} records for '#{name}'"
127
+ end
128
+
129
+ data.each do |name, fixtures|
130
+ content = "# '#{name}' data generated automatically by dummy at #{Time.now.strftime("%H:%M %m/%d/%Y")} (#{fixtures.size} records).\n"
131
+
132
+ content << YAML.dump(fixtures)
133
+
134
+ create_file "test/dummy/#{name}.yml", content
135
+ end
136
+ say_status :successful, "store fixtures"
137
+ end
138
+
139
+ def generate_record_data(name, info, column)
140
+ column_name = column.name
141
+ if(column_name =~ /_at$/ and column.type == :datetime) or column_name == "id"
142
+ return
143
+ end
144
+
145
+ associated_model = associated_class_name(info, column_name)
146
+
147
+ if associated_model
148
+ val = generate_association_data(associated_model)
149
+ column_name.gsub!(/_id$/, "")
150
+ else
151
+ val = generate_regular_data(column)
152
+ end
153
+
154
+ {column_name => val}
155
+ end
156
+
157
+ def associated_class_name(info, name)
158
+ info[:associations].each do |assoc|
159
+ return assoc[:model] if assoc[:foreign_key] == name
160
+ end
161
+ false
162
+ end
163
+
164
+ def generate_association_data(associated_model)
165
+ random_record_num = rand(@models[associated_model][:record_amount])
166
+ "#{associated_model.to_s.underscore}_#{random_record_num}"
167
+ end
168
+
169
+ def generate_regular_data(column)
170
+ val = Dummy.magic_data(column.name, column.type)
171
+
172
+ if val
173
+ val
174
+ else
175
+ say_status :failed, "data generation for '#{column.name}' with type '#{column.type.to_s.downcase}'", :red
176
+ ""
177
+ end
178
+ end
179
+
180
+ def copy_rake_file
181
+ copy_file "dummy.rake", "lib/tasks/dummy.rake"
182
+ end
183
+ end
184
+ end
185
+
@@ -0,0 +1,13 @@
1
+ require 'active_record/fixtures'
2
+
3
+ namespace :dummy do
4
+ namespace :data do
5
+ desc "Load the generated dummy data into the current environment's database."
6
+ task :import => :environment do
7
+ Fixtures.reset_cache
8
+ fixtures_folder = File.join(Rails.root, 'test', 'dummy')
9
+ fixtures = Dir[File.join(fixtures_folder, '*.yml')].map {|f| File.basename(f, '.yml') }
10
+ Fixtures.create_fixtures(fixtures_folder, fixtures)
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dummy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 5
8
+ version: "0.5"
9
+ platform: ruby
10
+ authors:
11
+ - "Gon\xC3\xA7alo Silva"
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+
16
+ date: 2010-07-29 00:00:00 +01:00
17
+ default_executable:
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: rails
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ - rc
32
+ version: 3.0.0.rc
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Generates test routes and consistent fake data, inserts it to the database and runs performance tests with them
36
+ email:
37
+ - goncalossilva@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/dummy/lorem.rb
46
+ - lib/dummy/address.rb
47
+ - lib/dummy/company.rb
48
+ - lib/dummy/name.rb
49
+ - lib/dummy/internet.rb
50
+ - lib/dummy/geolocation.rb
51
+ - lib/dummy/phone_number.rb
52
+ - lib/generators/dummy_generator.rb
53
+ - lib/dummy.rb
54
+ - lib/generators/templates/dummy.rake
55
+ - LICENSE
56
+ - README.md
57
+ - CHANGELOG.md
58
+ has_rdoc: true
59
+ homepage: http://github.com/goncalossilva/dummy
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 1
82
+ - 3
83
+ - 7
84
+ version: 1.3.7
85
+ requirements: []
86
+
87
+ rubyforge_project: dummy
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Generates fake data and test routes and runs performance tests with it
92
+ test_files: []
93
+