fixturies 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/fixturies.rb +102 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 47e32771f6457c1f6056bbde762249f8635bb914
4
+ data.tar.gz: 94debe4b532f990b01c3672a8843a7bc7606e275
5
+ SHA512:
6
+ metadata.gz: 6325c80d912dd7f6c8a05ecfbe97c30cc880fcd77bccf0934274dcbebce656a692809a5ffd4d8dee774e15f7ea6bff21c39ea0108bf1afe18c362428cbad12b2
7
+ data.tar.gz: 930ce542423ced42ffd8bdefa97363a8fdf3aefdfd5a0cba562e1aa825039ed6436a97c557664fe51142ab76fcb9078f5583aa0e0f81b2d6ef1e0259917e94ac
data/lib/fixturies.rb ADDED
@@ -0,0 +1,102 @@
1
+ class Fixturies
2
+
3
+ class << self
4
+
5
+ def create_fixtures
6
+ self.new.create_fixtures
7
+ end
8
+
9
+ def table(table_name)
10
+ self.table_names << table_name
11
+ end
12
+
13
+ def model(model_klass)
14
+ self.table_names << model_klass.table_name
15
+ end
16
+
17
+ def build(*things, &proc)
18
+ things.each do |thing|
19
+ thing.is_a?(String) ? table(thing) : model(thing)
20
+ end
21
+ builders << proc
22
+ end
23
+
24
+ def table_names
25
+ unless defined? @table_names
26
+ @table_names = Set.new
27
+ end
28
+ @table_names
29
+ end
30
+
31
+ def builders
32
+ unless defined? @builders
33
+ @builders = []
34
+ end
35
+ @builders
36
+ end
37
+
38
+ end
39
+
40
+ attr_reader :record_identifiers
41
+
42
+ def intitialize
43
+ @record_identifiers = {}
44
+ end
45
+
46
+ def create_fixtures
47
+ clear_db
48
+ build_all_records
49
+ create_fixture_files
50
+ end
51
+
52
+ def build_all_records
53
+ self.class.builders.each do |builder|
54
+ instance_eval(builder)
55
+ end
56
+ end
57
+
58
+ def identify(record, name)
59
+ record_identifiers[record_key(record)] = name
60
+ end
61
+
62
+ private
63
+ def record_key(record)
64
+ "#{record.class.table_name}#{record.id}"
65
+ end
66
+
67
+ private
68
+ def create_fixture_files
69
+
70
+ self.class.table_names.each do |table_name|
71
+
72
+ # create a simple ActiveRecord klass to connect
73
+ # to the table and handle querying for us
74
+ klass = Class.new(ActiveRecord::Base) {
75
+ self.table_name = table_name
76
+ }
77
+
78
+ hash = {}
79
+ klass.all.each_with_index do |record, i|
80
+ name = record_identifiers[record_key(record)] || "#{table_name.singularize}_#{i}"
81
+ hash[name] = record.attributes
82
+ end
83
+
84
+ FileUtils.mkdir_p(Rails.root.join('spec', 'fixtures'))
85
+ File.open(Rails.root.join('spec', 'fixtures', "#{table_name}.yml"), 'w+') do |f|
86
+ f.write(hash.to_yaml)
87
+ end
88
+
89
+ end
90
+ end
91
+
92
+ private
93
+ def clear_db
94
+ self.class.table_names.each do |table_name|
95
+ quoted_table_name = ActiveRecord::Base.connection.quote_table_name(table_name)
96
+ sql = "DELETE FROM #{quoted_table_name} "
97
+ ActiveRecord::Base.connection.delete(sql, "#{name} Delete all")
98
+ end
99
+
100
+ end
101
+
102
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fixturies
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nate Brustein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The speed of fixtures meets the maintanability of factories
14
+ email: nate@pedago.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/fixturies.rb
20
+ homepage: http://rubygems.org/gems/fixturies
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: The speed of fixtures meets the maintanability of factories
44
+ test_files: []