fixture_generator 0.0.1
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.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.md +27 -0
- data/Rakefile +19 -0
- data/fixture_generator.gemspec +20 -0
- data/lib/fixture_generator.rb +172 -0
- data/lib/fixture_generator/version.rb +3 -0
- metadata +69 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Fixture Generator
|
2
|
+
=============
|
3
|
+
|
4
|
+
`fixture_generator` gem can be used to manage fixtures by generating them using Rails code.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
gem install fixture_generator
|
10
|
+
|
11
|
+
Usage
|
12
|
+
-----
|
13
|
+
|
14
|
+
class MyFixtureGenerator < FixtureGenerator
|
15
|
+
def populate
|
16
|
+
# Create your records here and add them to the fixture set by calling
|
17
|
+
# 'add_record(record_name, record_object)'. That's it!
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
rake fixtures:generate GENERATOR="MyFixtureGenerator"
|
22
|
+
|
23
|
+
Author
|
24
|
+
------
|
25
|
+
|
26
|
+
B V Satyaram <[bvsatyaram.com](http://bvsatyaram.com)>
|
27
|
+
Inspired by back mark plugin authored by Vikram Venkatesan
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
|
3
|
+
namespace :fixtures do
|
4
|
+
desc "Generates test fixture files"
|
5
|
+
task :generate => :environment do
|
6
|
+
# Work with test db.
|
7
|
+
RAILS_ENV = ENV['RAILS_ENV'] = "test"
|
8
|
+
|
9
|
+
# Clean the database first.
|
10
|
+
puts "*** FixturePopulator :: Cleaning the database"
|
11
|
+
Rake::Task["cleandbtest"].invoke
|
12
|
+
|
13
|
+
print "*** FixturePopulator :: Generating data"
|
14
|
+
# Replace the following line with the fixture generator class of your project.
|
15
|
+
eval ENV['GENERATOR'] + ".generate"
|
16
|
+
|
17
|
+
puts "\nDone"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "fixture_generator/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "fixture_generator"
|
7
|
+
s.version = FixtureGenerator::VERSION
|
8
|
+
s.authors = ["Satyaram B V"]
|
9
|
+
s.email = ["bvsatyaram@gmail.com"]
|
10
|
+
s.homepage = "http://www.bvsatyaram.com/"
|
11
|
+
s.summary = %q{Manage fixtures by generating them using Rails code}
|
12
|
+
s.description = %q{Manage fixtures by generating them using Rails code}
|
13
|
+
|
14
|
+
s.rubyforge_project = "fixture_generator"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
end
|
@@ -0,0 +1,172 @@
|
|
1
|
+
require "fixture_generator/version"
|
2
|
+
|
3
|
+
# Provides the functionality for creating active record objects and associating
|
4
|
+
# fixture names to them and then writing them to yml files.
|
5
|
+
|
6
|
+
class FixtureGenerator
|
7
|
+
# Map from model class name to the corresponding table name.
|
8
|
+
attr_accessor :models
|
9
|
+
|
10
|
+
# Map from each table name to fixtures for that table name.
|
11
|
+
#
|
12
|
+
# {
|
13
|
+
# 'users' => {:greg => <object>, :mark => <object>},
|
14
|
+
# 'cars' => {:benz => <object>, :ferrari => <object>}
|
15
|
+
# }
|
16
|
+
#
|
17
|
+
attr_accessor :object_map
|
18
|
+
|
19
|
+
# Mapping from active record object to the fixture name.
|
20
|
+
attr_accessor :fixture_name_map
|
21
|
+
|
22
|
+
# Generates fixture data and writes them to yml files.
|
23
|
+
#
|
24
|
+
def self.generate(opts = {})
|
25
|
+
_generator = self.new
|
26
|
+
|
27
|
+
# Register the models.
|
28
|
+
(ActiveRecord::Base.send(:subclasses) - (opts[:except] || [])).each do |model_klass|
|
29
|
+
_generator.register_model(model_klass)
|
30
|
+
end
|
31
|
+
|
32
|
+
ActiveRecord::Base.transaction do
|
33
|
+
ActiveRecord::Base.connection.execute "SET AUTOCOMMIT=0;"
|
34
|
+
begin
|
35
|
+
_generator.populate
|
36
|
+
end
|
37
|
+
ActiveRecord::Base.connection.execute "SET AUTOCOMMIT=1;"
|
38
|
+
end
|
39
|
+
|
40
|
+
_generator.write_to_file
|
41
|
+
end
|
42
|
+
|
43
|
+
# Creates the ActiveRecord records to be written to yml files.
|
44
|
+
#
|
45
|
+
# To be implemented by the project specific generator.
|
46
|
+
#
|
47
|
+
def populate
|
48
|
+
raise NotImplementedError
|
49
|
+
end
|
50
|
+
|
51
|
+
# Registers the model with class name +klass+ and corresponding table name
|
52
|
+
# +table_name+
|
53
|
+
#
|
54
|
+
def register_model(klass)
|
55
|
+
table_name = klass.table_name
|
56
|
+
self.models[klass] = table_name
|
57
|
+
self.object_map[table_name] = {}
|
58
|
+
|
59
|
+
self.class_eval do
|
60
|
+
# Define accessors for the fixtures for this new model.
|
61
|
+
define_method(table_name) do |name|
|
62
|
+
self.object_map[table_name][name.to_sym]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# Writes the generated fixture records to files under 'fixtures' directory.
|
68
|
+
#
|
69
|
+
# XXX Note that any existing fixtures will be replaced with the new ones by
|
70
|
+
# this method.
|
71
|
+
#
|
72
|
+
def write_to_file
|
73
|
+
newline
|
74
|
+
say "Writing fixtures to YAML files"
|
75
|
+
records_by_table_name = {}
|
76
|
+
|
77
|
+
self.models.each do |model_class, table_name|
|
78
|
+
# Load all records of this model.
|
79
|
+
records_by_table_name[table_name] ||= []
|
80
|
+
records_by_table_name[table_name] += model_class.all
|
81
|
+
end
|
82
|
+
|
83
|
+
records_by_table_name.each do |table_name, records|
|
84
|
+
yaml_data = ""
|
85
|
+
|
86
|
+
if records.any?
|
87
|
+
# Sequence number for records for which we do not have names.
|
88
|
+
i = "0"
|
89
|
+
data_array = []
|
90
|
+
|
91
|
+
records.uniq.each do |record|
|
92
|
+
# Get the name given for this record while populating. If not
|
93
|
+
# available (autogenerated through callbacks), use a sequence number
|
94
|
+
# prefixed by the table name.
|
95
|
+
fixture_name = self.fixture_name_map[record] || "#{table_name}_#{i.succ!}"
|
96
|
+
attrs = record.attributes
|
97
|
+
attrs['delta'] = false if attrs.key?('delta')
|
98
|
+
data_array << {fixture_name => attrs}.to_yaml.gsub(/--- \n/, "")
|
99
|
+
end
|
100
|
+
|
101
|
+
yaml_data = data_array.join("\n")
|
102
|
+
|
103
|
+
File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", "w") do |file|
|
104
|
+
file.write yaml_data
|
105
|
+
end
|
106
|
+
end
|
107
|
+
dot
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
# Private constructor for providing a static interface to the generator.
|
114
|
+
#
|
115
|
+
def initialize
|
116
|
+
self.models = {}
|
117
|
+
self.object_map = {}
|
118
|
+
self.fixture_name_map = {}
|
119
|
+
|
120
|
+
# Lower the logging level since we are about to create many records.
|
121
|
+
_logger = Logger.new(STDOUT)
|
122
|
+
_logger.level = Logger::ERROR
|
123
|
+
ActiveRecord::Base.logger = _logger
|
124
|
+
end
|
125
|
+
|
126
|
+
# Adds a fixture +record+ with the given +name+.
|
127
|
+
#
|
128
|
+
def add_record(name, record)
|
129
|
+
klass_map = self.object_map[self.models[record.class]]
|
130
|
+
|
131
|
+
# Panic if attempted to insert duplicate fixture record.
|
132
|
+
if klass_map.include?(name.to_sym)
|
133
|
+
raise "Duplicate fixture for #{record.class} - #{name}"
|
134
|
+
end
|
135
|
+
|
136
|
+
self.fixture_name_map[record] = name.to_s
|
137
|
+
klass_map[name.to_sym] = record
|
138
|
+
dot
|
139
|
+
|
140
|
+
return record
|
141
|
+
end
|
142
|
+
|
143
|
+
##############################################################################
|
144
|
+
# PRINTING
|
145
|
+
##############################################################################
|
146
|
+
|
147
|
+
def say(str)
|
148
|
+
print "*** FixtureGenerator :: #{str}"
|
149
|
+
end
|
150
|
+
|
151
|
+
def say_populating(model_name)
|
152
|
+
newline
|
153
|
+
say "Populating #{model_name}"
|
154
|
+
end
|
155
|
+
|
156
|
+
def warn(msg)
|
157
|
+
print_and_flush "!!! WARNING: #{msg}\n"
|
158
|
+
end
|
159
|
+
|
160
|
+
def dot
|
161
|
+
print_and_flush(".")
|
162
|
+
end
|
163
|
+
|
164
|
+
def newline
|
165
|
+
print_and_flush("\n")
|
166
|
+
end
|
167
|
+
|
168
|
+
def print_and_flush(msg)
|
169
|
+
print msg
|
170
|
+
$stdout.flush
|
171
|
+
end
|
172
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fixture_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Satyaram B V
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-08-26 00:00:00 +05:30
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Manage fixtures by generating them using Rails code
|
22
|
+
email:
|
23
|
+
- bvsatyaram@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- fixture_generator.gemspec
|
36
|
+
- lib/fixture_generator.rb
|
37
|
+
- lib/fixture_generator/version.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: http://www.bvsatyaram.com/
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project: fixture_generator
|
64
|
+
rubygems_version: 1.3.6
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Manage fixtures by generating them using Rails code
|
68
|
+
test_files: []
|
69
|
+
|