activerecord-fixture_builder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2a3846efeb1061c5669bafed528d78cdf11025f
4
+ data.tar.gz: 115b42ce1d5d06a421c9340c5a3a828bbda17f55
5
+ SHA512:
6
+ metadata.gz: 7c83331be07357f56043d30d242fd35c1a43ed1304179d2209baf24e311043eb43e81959a7e54496977b60429ec6901bee9ba5a6988329641685cee8b0092782
7
+ data.tar.gz: 50b8fad8019c8917444407ce9d83613c30cf29d8674dca72db9070a257f12ed7d1c1c8224be0dd94ade29c230014931f9e02b05cfb4dd6b6591c576db6708acd
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-fixture_builder.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jared Grippe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # Activerecord::FixtureBuilder
2
+
3
+ A simple fixture builder for active record.
4
+
5
+ (no rails required)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'activerecord-fixture_builder'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord-fixture_builder
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_record/fixture_builder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-fixture_builder"
8
+ spec.version = ActiveRecord::FixtureBuilder::VERSION
9
+ spec.authors = ["Jared Grippe"]
10
+ spec.email = ["jared@deadlyicon.com"]
11
+ spec.description = %q{A simple fixture builder for active record}
12
+ spec.summary = %q{A simple fixture builder for active record}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", "~> 4.0"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "pry"
26
+ end
@@ -0,0 +1,85 @@
1
+ require 'active_record/fixture_builder/version'
2
+ require 'active_support/core_ext/module/delegation'
3
+
4
+ module ActiveRecord
5
+ class FixtureBuilder
6
+ autoload :Configuration, 'active_record/fixture_builder/configuration'
7
+ autoload :Database, 'active_record/fixture_builder/database'
8
+ autoload :Fixture, 'active_record/fixture_builder/fixture'
9
+ autoload :Builder, 'active_record/fixture_builder/builder'
10
+ end
11
+ end
12
+
13
+ class ActiveRecord::FixtureBuilder
14
+
15
+ def initialize(&block)
16
+ configure(&block) if block_given?
17
+ end
18
+
19
+ def config
20
+ @config ||= Configuration.new
21
+ end
22
+
23
+ def configure
24
+ yield config
25
+ self
26
+ end
27
+
28
+ def database
29
+ config.freeze
30
+ @database ||= Database.new(config)
31
+ end
32
+
33
+ def fixtures
34
+ config.freeze
35
+ @fixtures ||= Dir[config.fixtures_path+'*.yml'].map do |path|
36
+ Fixture.new self, Pathname(path)
37
+ end
38
+ end
39
+
40
+ def builders
41
+ config.freeze
42
+ @builders ||= Dir[config.builders_path+'*.rb'].map do |path|
43
+ Builder.new self, Pathname(path)
44
+ end
45
+ end
46
+
47
+ def load_fixtures!
48
+ fixtures.each(&:load!)
49
+ end
50
+
51
+ def build_fixtures!
52
+ database.truncate_all_tables!
53
+ builders.each(&:build!)
54
+ end
55
+
56
+ def write_fixtures!
57
+ database.table_names.each do |table_name|
58
+ config.fixtures_path.join("#{table_name}.yml").open('w') do |file|
59
+ records = database.select_all(table_name)
60
+
61
+ fixture_data = {}
62
+ records.each_with_index do |record, index|
63
+ fixture_data["#{table_name}_#{index.to_s.rjust(3,'0')}"] = record
64
+ end
65
+
66
+ file.write fixture_data.to_yaml
67
+ end
68
+ end
69
+ end
70
+
71
+ def inspect
72
+ super.split(/ |>/).first+'>'
73
+ end
74
+
75
+ PUBLIC_INSTANCE_METHODS = public_instance_methods - superclass.public_instance_methods
76
+
77
+ class << self
78
+ def instance
79
+ @instance ||= new
80
+ end
81
+
82
+ delegate *PUBLIC_INSTANCE_METHODS, to: :instance
83
+ end
84
+
85
+ end
@@ -0,0 +1,17 @@
1
+ class ActiveRecord::FixtureBuilder::Builder
2
+
3
+ def initialize fixture_builder, path
4
+ @fixture_builder, @path = fixture_builder, path
5
+ end
6
+ attr_reader :path
7
+
8
+ def build!
9
+ load @path
10
+ end
11
+
12
+ def inspect
13
+ path = @path.relative_path_from(@fixture_builder.config.fixtures_path)
14
+ %[#<#{self.class} #{path.to_s}>]
15
+ end
16
+
17
+ end
@@ -0,0 +1,40 @@
1
+ require 'pathname'
2
+
3
+ class ActiveRecord::FixtureBuilder::Configuration
4
+
5
+ attr_writer :root, :fixtures_path, :builders_path
6
+
7
+ attr_accessor :excluded_tables
8
+
9
+ def initialize
10
+ @excluded_tables = %w{schema_migrations}
11
+ end
12
+
13
+ def root
14
+ Pathname @root || case
15
+ when defined? Bundler
16
+ Bundler.root
17
+ when defined? Rails
18
+ Rails.root
19
+ else
20
+ Dir.pwd
21
+ end
22
+ end
23
+
24
+ def fixtures_path
25
+ Pathname(@fixtures_path || root.join('spec/fixtures'))
26
+ end
27
+
28
+ def builders_path
29
+ Pathname(@builders_path || root.join('spec/fixture_builders'))
30
+ end
31
+
32
+ def connection
33
+ @connection || ::ActiveRecord::Base.connection
34
+ end
35
+
36
+ def inspect
37
+ super.split(/ |>/).first+'>'
38
+ end
39
+
40
+ end
@@ -0,0 +1,48 @@
1
+ class ActiveRecord::FixtureBuilder::Database
2
+
3
+ def initialize(config)
4
+ @config = config
5
+ end
6
+
7
+ def connection
8
+ @config.connection
9
+ end
10
+
11
+ def truncate table_name
12
+ connection.delete "DELETE FROM #{connection.quote_table_name(table_name)}"
13
+ end
14
+
15
+ def table_names
16
+ connection.tables - @config.excluded_tables
17
+ end
18
+
19
+ def truncate_all_tables!
20
+ table_names.each{|table_name| truncate table_name }
21
+ end
22
+
23
+ def reset_pk_sequence table_name
24
+ connection.reset_pk_sequence! table_name
25
+ end
26
+
27
+ def insert table_name, fixture_name, record
28
+ return if record.blank?
29
+
30
+ columns = Hash[connection.columns(table_name).map { |c| [c.name, c] }]
31
+ table_name = connection.quote_table_name(table_name)
32
+
33
+ column_names = []
34
+ values = []
35
+
36
+ record.each do |column_name, value|
37
+ column_names << connection.quote_column_name(column_name)
38
+ values << connection.quote(value, columns[column_name])
39
+ end
40
+
41
+ connection.insert "INSERT INTO #{table_name} (#{column_names.join(', ')}) VALUES (#{values.join(', ')})", "Inserting Fixture #{fixture_name.inspect}"
42
+ end
43
+
44
+ def select_all table_name
45
+ connection.select_all("SELECT * FROM #{connection.quote_table_name(table_name)}")
46
+ end
47
+
48
+ end
@@ -0,0 +1,39 @@
1
+ class ActiveRecord::FixtureBuilder::Fixture
2
+
3
+ def initialize fixture_builder, path
4
+ @fixture_builder, @path = fixture_builder, path
5
+ end
6
+ attr_reader :path
7
+
8
+ def table_name
9
+ @table_name ||= File.basename(@path, '.yml')
10
+ end
11
+
12
+ def records_from_file
13
+ Array YAML.load_file @path
14
+ end
15
+
16
+ # def records_from_database
17
+ # @fixture_builder.select_all(table_name)
18
+ # end
19
+
20
+ # def write!
21
+ # path.open('w') do |file|
22
+ # file.write records_from_database.to_yaml
23
+ # end
24
+ # end
25
+
26
+ def load!
27
+ @fixture_builder.database.truncate table_name
28
+ records_from_file.each do |fixture_name, record|
29
+ @fixture_builder.database.insert(table_name, fixture_name, record)
30
+ end
31
+ @fixture_builder.database.reset_pk_sequence table_name
32
+ end
33
+
34
+ def inspect
35
+ path = @path.relative_path_from(@fixture_builder.config.fixtures_path)
36
+ %[#<#{self.class} #{path.to_s}>]
37
+ end
38
+
39
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveRecord
2
+ class FixtureBuilder
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'active_record/fixture_builder'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-fixture_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jared Grippe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A simple fixture builder for active record
70
+ email:
71
+ - jared@deadlyicon.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - activerecord-fixture_builder.gemspec
82
+ - lib/active_record/fixture_builder.rb
83
+ - lib/active_record/fixture_builder/builder.rb
84
+ - lib/active_record/fixture_builder/configuration.rb
85
+ - lib/active_record/fixture_builder/database.rb
86
+ - lib/active_record/fixture_builder/fixture.rb
87
+ - lib/active_record/fixture_builder/version.rb
88
+ - lib/activerecord/fixture_builder.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A simple fixture builder for active record
113
+ test_files: []