fastory 0.1.0

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ryan Angilly
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = fastory
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2010 Punchbowl Software. See LICENSE for details.
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "fastory"
8
+ gem.summary = %Q{Make FactoryGirl faster by caching SQL on frequently used factories}
9
+ gem.description = %Q{Sits on top of FactoryGirl and, when appropriate, caches SQL for later playback}
10
+ gem.email = "ryan@angilly.com"
11
+ gem.homepage = "http://github.com/mypunchbowl/fastory"
12
+ gem.authors = ["Ryan Angilly"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.add_dependency "activesupport", '>=2.3.5'
15
+ gem.add_dependency "activerecord", '>=2.3.5'
16
+ gem.add_dependency "factory_girl", '>=1.2.3'
17
+
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ Jeweler::GemcutterTasks.new
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
24
+
25
+ require 'rake/testtask'
26
+ Rake::TestTask.new(:test) do |test|
27
+ test.libs << 'lib' << 'test'
28
+ test.pattern = 'test/**/*_test.rb'
29
+ test.verbose = true
30
+ end
31
+
32
+ begin
33
+ require 'rcov/rcovtask'
34
+ Rcov::RcovTask.new do |test|
35
+ test.libs << 'test'
36
+ test.pattern = 'test/**/*_test.rb'
37
+ test.verbose = true
38
+ end
39
+ rescue LoadError
40
+ task :rcov do
41
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+ task :default => :test
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ if File.exist?('VERSION')
52
+ version = File.read('VERSION')
53
+ else
54
+ version = ""
55
+ end
56
+
57
+ rdoc.rdoc_dir = 'rdoc'
58
+ rdoc.title = "fastory #{version}"
59
+ rdoc.rdoc_files.include('README*')
60
+ rdoc.rdoc_files.include('lib/**/*.rb')
61
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{fastory}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Angilly"]
12
+ s.date = %q{2010-01-27}
13
+ s.description = %q{Sits on top of FactoryGirl and, when appropriate, caches SQL for later playback}
14
+ s.email = %q{ryan@angilly.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "fastory.gemspec",
27
+ "lib/fastory.rb",
28
+ "test/fastory_test.rb",
29
+ "test/test.db",
30
+ "test/test_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/mypunchbowl/fastory}
33
+ s.rdoc_options = ["--charset=UTF-8"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.5}
36
+ s.summary = %q{Make FactoryGirl faster by caching SQL on frequently used factories}
37
+ s.test_files = [
38
+ "test/fastory_test.rb",
39
+ "test/test_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
48
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.5"])
49
+ s.add_runtime_dependency(%q<activerecord>, [">= 2.3.5"])
50
+ s.add_runtime_dependency(%q<factory_girl>, [">= 1.2.3"])
51
+ else
52
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ s.add_dependency(%q<activesupport>, [">= 2.3.5"])
54
+ s.add_dependency(%q<activerecord>, [">= 2.3.5"])
55
+ s.add_dependency(%q<factory_girl>, [">= 1.2.3"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
59
+ s.add_dependency(%q<activesupport>, [">= 2.3.5"])
60
+ s.add_dependency(%q<activerecord>, [">= 2.3.5"])
61
+ s.add_dependency(%q<factory_girl>, [">= 1.2.3"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,139 @@
1
+ require 'active_support'
2
+ require 'active_record'
3
+
4
+ module Punchbowl
5
+ module InstanceMethods
6
+ def self.included(receiver)
7
+ Fastory.sql_inserts = []
8
+ Fastory.fastories = {}
9
+
10
+ receiver.instance_eval do
11
+ alias_method_chain :teardown, :fastory_refresh
12
+ end
13
+ end
14
+
15
+ def teardown_with_fastory_refresh
16
+ Fastory.fastories.each do |k,v|
17
+ Fastory.fastories[k][:templates_used] = 0
18
+ end
19
+
20
+ teardown_without_fastory_refresh
21
+ end
22
+ end
23
+
24
+ class Fastory
25
+ attr_accessor :name, :options
26
+ cattr_accessor :fastories, :sql_inserts, :record_sql
27
+
28
+ @@fastories = {}
29
+ @@sql_inserts = []
30
+ @@record_sql = false
31
+
32
+ def initialize(name, options = {})
33
+ self.name = name
34
+ self.options = options
35
+ end
36
+
37
+ def process!
38
+ options = transform_associations
39
+
40
+ if template_available?
41
+ cache = @@fastories[cache_key]
42
+ cnt = cache[:templates_used] ||= 0
43
+ cache[:templates_used] += 1
44
+
45
+ fast_gen(cache[:templates][cnt][:sql])
46
+
47
+ obj = build_class.find cache[:templates][cnt][:id]
48
+ else
49
+ @@sql_inserts = []
50
+ @@record_sql = true
51
+ obj = fg.build name, options
52
+ obj.id = fg.next(:fastory_id_generator) if obj.id.blank?
53
+ obj.save!
54
+ @@record_sql = false
55
+
56
+ @@fastories[cache_key] ||= {}
57
+ @@fastories[cache_key][:templates_available] ||= 0
58
+ @@fastories[cache_key][:templates_available] += 1
59
+ @@fastories[cache_key][:templates_used] = @@fastories[cache_key][:templates_available]
60
+
61
+ res = @@fastories[cache_key][:templates] ||= []
62
+ obj_meta = {:sql => @@sql_inserts.dup, :id => obj.id }
63
+ res = @@fastories[cache_key][:templates] << obj_meta
64
+ end
65
+
66
+ obj
67
+ end
68
+
69
+ private
70
+
71
+ def transform_associations
72
+ output_options = {}
73
+ options.each do |k,v|
74
+ unless v.is_a?(ActiveRecord::Base)
75
+ output_options[k] = v
76
+ next
77
+ end
78
+
79
+ raise 'Cannot deal with new records either... sorry' if v.new_record?
80
+ assoc = build_class.reflect_on_all_associations.find {|a| a.name.to_s == k.to_s }
81
+ raise "No association #{name} on #{k}" if assoc.blank?
82
+ raise 'Can only deal with belongs_to right now' unless assoc.belongs_to?
83
+
84
+ output_options[assoc.primary_key_name] = v.id
85
+ end
86
+
87
+ output_options
88
+ end
89
+
90
+ def build_class
91
+ fg.factories[name].build_class
92
+ end
93
+
94
+ def template_available?
95
+ @@fastories[cache_key].present? && (@@fastories[cache_key][:templates_available].to_i > @@fastories[cache_key][:templates_used].to_i)
96
+ end
97
+
98
+ def fg
99
+ ::Factory
100
+ end
101
+
102
+ def fast_gen(sql)
103
+ sql.each {|s| ActiveRecord::Base.connection.execute_without_query_capture(s) }
104
+ end
105
+
106
+ def cache_key
107
+ options.to_a.map {|a,b| [a.to_s, b.to_s]}.sort {|a,b| a[0] <=> b[0]}.unshift([:name, name])
108
+ end
109
+ end
110
+ end
111
+
112
+ ActiveRecord::Base.connection.class_eval do
113
+ define_method :execute_with_query_capture do |*args|
114
+ sql = args[0]
115
+ name = args[1]
116
+
117
+ if Punchbowl::Fastory.record_sql
118
+ case sql
119
+ when /\A(DELETE|UPDATE|INSERT)/
120
+ Punchbowl::Fastory.sql_inserts << sql
121
+ end
122
+ end
123
+
124
+ execute_without_query_capture(sql, name)
125
+ end
126
+
127
+ alias_method_chain :execute, :query_capture
128
+ end
129
+
130
+ if defined?(ActiveSupport) && defined?(ActiveSupport::TestCase)
131
+ ActiveSupport::TestCase.send :include, Punchbowl::InstanceMethods
132
+ end
133
+
134
+ Factory.sequence(:fastory_id_generator) {|n| n.to_i}
135
+
136
+ def Fastory(name, options = {})
137
+ Punchbowl::Fastory.new(name, options).process!
138
+ end
139
+
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class FastoryTest < Test::Unit::TestCase
4
+ include Punchbowl::InstanceMethods
5
+
6
+ should "define Fastory" do
7
+ assert Fastory(:user).is_a?(User)
8
+ end
9
+
10
+ should "handle associations" do
11
+ @user = Fastory :user
12
+ @event = Fastory :event, :user => @user
13
+
14
+ @user.reload
15
+ @event.reload
16
+
17
+ assert_equal @user, @event.user
18
+ end
19
+
20
+ def teardown
21
+ ActiveRecord::Base.connection.execute 'delete from users'
22
+ ActiveRecord::Base.connection.execute 'delete from events'
23
+ end
24
+ end
Binary file
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_record'
4
+ require 'shoulda'
5
+ require 'factory_girl'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
9
+
10
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
11
+ :database => File.join(File.dirname(__FILE__), 'test.db'))
12
+
13
+ ActiveRecord::Schema.define do
14
+ execute 'DROP TABLE IF EXISTS users'
15
+ execute 'DROP TABLE IF EXISTS events'
16
+
17
+ create_table :users, :force => true do |t|
18
+ t.string :name
19
+ t.string :email
20
+ end
21
+
22
+ create_table :events, :force => true do |t|
23
+ t.string :title
24
+ t.integer :user_id
25
+ end
26
+ end
27
+
28
+ require 'fastory'
29
+
30
+ class User < ActiveRecord::Base
31
+ has_many :events
32
+ end
33
+
34
+ class Event < ActiveRecord::Base
35
+ belongs_to :user
36
+ end
37
+
38
+ Factory.define(:user) do |u|
39
+ u.name 'Ryan'
40
+ u.email 'ryan@angilly.com'
41
+ end
42
+
43
+ Factory.define(:event) do |e|
44
+ e.user { Factory :user }
45
+ e.title 'My sweet party'
46
+ end
47
+
48
+ class Test::Unit::TestCase
49
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Angilly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-27 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.5
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.3.5
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: factory_girl
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.3
54
+ version:
55
+ description: Sits on top of FactoryGirl and, when appropriate, caches SQL for later playback
56
+ email: ryan@angilly.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.rdoc
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.rdoc
69
+ - Rakefile
70
+ - VERSION
71
+ - fastory.gemspec
72
+ - lib/fastory.rb
73
+ - test/fastory_test.rb
74
+ - test/test.db
75
+ - test/test_helper.rb
76
+ has_rdoc: true
77
+ homepage: http://github.com/mypunchbowl/fastory
78
+ licenses: []
79
+
80
+ post_install_message:
81
+ rdoc_options:
82
+ - --charset=UTF-8
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: "0"
90
+ version:
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: "0"
96
+ version:
97
+ requirements: []
98
+
99
+ rubyforge_project:
100
+ rubygems_version: 1.3.5
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Make FactoryGirl faster by caching SQL on frequently used factories
104
+ test_files:
105
+ - test/fastory_test.rb
106
+ - test/test_helper.rb