kathy_lee 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2010 markbates
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,150 @@
1
+ =Kathy Lee
2
+
3
+ An easy to use factory framework for generating test data for ActiveRecord models.
4
+
5
+ ==Setup Examples
6
+
7
+ KathyLee::User.define do
8
+ username(fake(:user_name))
9
+ email(fake(:email))
10
+ home_page_url(fake(:url))
11
+ fullname(fake(:full_name))
12
+ login_count(0)
13
+
14
+ has_many :posts, :size => 2
15
+ has_many :comments, :size => 1
16
+ end
17
+
18
+ KathyLee::User::Marge.define do
19
+ fullname('Marge Simpson')
20
+ login_count(1000)
21
+ end
22
+
23
+ KathyLee::User::Marge::Other.define do
24
+ home_page_url('http://www.simpsons.com')
25
+
26
+ has_many :posts, :size => 5
27
+ has_many :comments, :size => 1
28
+ end
29
+
30
+ KathyLee::Post.define do
31
+ title(fake(:title))
32
+ body(fake(:body))
33
+
34
+ belongs_to :user
35
+ has_many :comments, :size => 1
36
+ end
37
+
38
+ KathyLee::Comment.define do
39
+ body(fake(:body))
40
+
41
+ belongs_to :user
42
+ belongs_to :post
43
+ end
44
+
45
+ KathyLee::Movie.define(:title => 'Rocky II', :director => 'Sly Stallone')
46
+
47
+
48
+ # Build a new unsaved model:
49
+ user = KathyLee::User.new
50
+ user.username.should == 'bill.smith'
51
+ user.email.should == 'bill.smith@example.org'
52
+ user.home_page_url.should == 'http://www.example.com'
53
+ user.fullname.should == 'Bill Smith'
54
+ user.login_count.should == 0
55
+ user.new_record?.should be_true
56
+
57
+ # Build a new unsaved model with a few overrides:
58
+ user = KathyLee::User.new(:username => 'mark.bates', :login_count => 5)
59
+ user.username.should == 'mark.bates'
60
+ user.email.should == 'bill.smith@example.org'
61
+ user.home_page_url.should == 'http://www.example.com'
62
+ user.fullname.should == 'Bill Smith'
63
+ user.login_count.should == 5
64
+ user.new_record?.should be_true
65
+
66
+ # Build and save a new model:
67
+ user = KathyLee::User.create
68
+ user.username.should == 'bill.smith'
69
+ user.email.should == 'bill.smith@example.org'
70
+ user.home_page_url.should == 'http://www.example.com'
71
+ user.fullname.should == 'Bill Smith'
72
+ user.login_count.should == 0
73
+ user.new_record?.should be_false
74
+
75
+ # Build and save a new model with a few overrides:
76
+ user = KathyLee::User.create(:username => 'mark.bates')
77
+ user.username.should == 'mark.bates'
78
+ user.email.should == 'bill.smith@example.org'
79
+ user.home_page_url.should == 'http://www.example.com'
80
+ user.fullname.should == 'Bill Smith'
81
+ user.login_count.should == 0
82
+ user.new_record?.should be_false
83
+
84
+ # Build many unsaved models (can be used with/without block):
85
+ users = KathyLee::User.sweatshop(10) do |user, index|
86
+ user.login_count = index
87
+ end
88
+ users.size.should == 10
89
+ users.each_with_index do |user, index|
90
+ user.login_count.should == index
91
+ user.new_record?.should be_true
92
+ end
93
+
94
+ # Build and save many models (can be used with/without block):
95
+ users = KathyLee::User.sweatshop!(10) do |user, index|
96
+ user.login_count = index
97
+ end
98
+ users.size.should == 10
99
+ users.each_with_index do |user, index|
100
+ user.login_count.should == index
101
+ user.new_record?.should be_false
102
+ end
103
+
104
+ # Build belongs_to associations (change KathyLee::Post.new to KathyLee::Post.create to save the whole thing):
105
+ post = KathyLee::Post.new
106
+ post.title.should_not be_nil
107
+ post.user.should_not be_nil
108
+ post.user.should be_kind_of(User)
109
+ post.user.email.should == 'bill.smith@example.org'
110
+
111
+ # Build has_many associations (change KathyLee::User.new to KathyLee::User.create to save the whole thing):
112
+ user = KathyLee::User.new
113
+ user.should_not be_nil
114
+ user.posts.should_not be_nil
115
+ user.posts.size.should == 2
116
+ user.posts.each do |post|
117
+ post.should be_kind_of(Post)
118
+ post.title.should_not be_nil
119
+ end
120
+ user.comments.should_not be_nil
121
+ user.comments.size.should == 1
122
+ user.comments.each do |comment|
123
+ comment.should be_kind_of(Comment)
124
+ end
125
+
126
+ # Build a variant of a model using subclasses:
127
+ user = KathyLee::User::Marge.new
128
+ user.username.should == 'bill.smith'
129
+ user.email.should == 'bill.smith@example.org'
130
+ user.home_page_url.should == 'http://www.example.com'
131
+ user.fullname.should == 'Marge Simpson'
132
+ user.login_count.should == 1000
133
+ user.new_record?.should be_true
134
+ user.posts.size.should == 2
135
+
136
+ # Subclass your subclass for further customization:
137
+ user = KathyLee::User::Marge::Other.new
138
+ user.username.should == 'bill.smith'
139
+ user.email.should == 'bill.smith@example.org'
140
+ user.home_page_url.should == 'http://www.simpsons.com'
141
+ user.fullname.should == 'Marge Simpson'
142
+ user.login_count.should == 1000
143
+ user.new_record?.should be_true
144
+ user.posts.size.should == 5
145
+
146
+ ==Contact
147
+
148
+ Please mail bugs, suggestions and patches to "development@metabates.com":mailto:development@metabates.com
149
+
150
+ On the web at: "http://www.metabates.com":http://www.metabates.
data/lib/kathy_lee.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'singleton'
2
+ require 'faker'
3
+ require 'benchmark'
4
+ require 'erb'
5
+
6
+ class Symbol
7
+
8
+ def classify
9
+ self.to_s.classify
10
+ end
11
+
12
+ end
13
+
14
+ Dir.glob(File.join(File.dirname(__FILE__), 'kathy_lee', '**/*.rb')).each do |f|
15
+ require File.expand_path(f)
16
+ end
@@ -0,0 +1,11 @@
1
+ module KathyLee
2
+ module Errors
3
+ class NoFakeRegistered < StandardError
4
+
5
+ def initialize(name)
6
+ super("No fake has been registered for '#{name}'!")
7
+ end
8
+
9
+ end # NoFakeRegistered
10
+ end # Errors
11
+ end # KathyLee
@@ -0,0 +1,81 @@
1
+ module KathyLee
2
+ class Fakes
3
+ include Singleton
4
+
5
+ attr_accessor :list
6
+
7
+ def initialize
8
+ self.reset!
9
+ end
10
+
11
+ def reset!
12
+ self.list = {}
13
+ end
14
+
15
+ def add(name, &block)
16
+ self.list[name.to_sym] = block
17
+ end
18
+
19
+ def alias(from, to)
20
+ self.list[from.to_sym] = KathyLee::Fakes::Alias.new(to)
21
+ end
22
+
23
+ def execute(name, *args)
24
+ block = self.list[name.to_sym]
25
+ if block.is_a?(KathyLee::Fakes::Alias)
26
+ return execute(block.to, *args)
27
+ end
28
+ if block
29
+ return block.call(*args)
30
+ end
31
+ raise KathyLee::Errors::NoFakeRegistered.new(name)
32
+ end
33
+
34
+ class << self
35
+ def method_missing(sym, *args, &block)
36
+ KathyLee::Fakes.instance.send(sym, *args, &block)
37
+ end
38
+ end # class << self
39
+
40
+ private
41
+ class Alias
42
+ attr_accessor :to
43
+ def initialize(to)
44
+ self.to = to
45
+ end
46
+ end
47
+
48
+ end # Fakes
49
+ end # KathyLee
50
+
51
+ %w{first_name last_name name prefix suffix}.each do |m|
52
+ eval("KathyLee::Fakes.add(:#{m}) {|*args| Faker::Name.#{m}(*args)}")
53
+ end
54
+
55
+ %w{city city_prefix city_suffix secondary_address street_address street_name
56
+ street_suffix uk_country uk_county uk_postcode us_state us_state_abbr zip_code}.each do |m|
57
+ eval("KathyLee::Fakes.add(:#{m}) {|*args| Faker::Address.#{m}(*args)}")
58
+ end
59
+
60
+ %w{bs catch_phrase name suffix}.each do |m|
61
+ eval("KathyLee::Fakes.add(:#{m}) {|*args| Faker::Company.#{m}(*args)}")
62
+ end
63
+
64
+ %w{domain_name domain_suffix domain_word email free_email user_name}.each do |m|
65
+ eval("KathyLee::Fakes.add(:#{m}) {|*args| Faker::Internet.#{m}(*args)}")
66
+ end
67
+
68
+ %w{paragraph paragraphs sentence sentences words}.each do |m|
69
+ eval("KathyLee::Fakes.add(:#{m}) {|*args| Faker::Lorem.#{m}(*args)}")
70
+ end
71
+
72
+ %w{phone_number}.each do |m|
73
+ eval("KathyLee::Fakes.add(:#{m}) {|*args| Faker::PhoneNumber.#{m}(*args)}")
74
+ end
75
+
76
+ KathyLee::Fakes.add(:url) {|*args| 'http://' + Faker::Internet.domain_name(*args)}
77
+ KathyLee::Fakes.add(:lorem) {|*args| Faker::Lorem.paragraphs(*args).join("\n")}
78
+ KathyLee::Fakes.add(:name) {|*args| Faker::Name.first_name + ' ' + Faker::Name.last_name}
79
+ KathyLee::Fakes.alias(:body, :lorem)
80
+ KathyLee::Fakes.alias(:full_name, :name)
81
+ KathyLee::Fakes.alias(:title, :sentence)
@@ -0,0 +1,15 @@
1
+ module KathyLee
2
+
3
+ class << self
4
+
5
+ def const_missing(klass, for_klass = klass)
6
+ @klass = klass
7
+ @for_klass = for_klass
8
+ result = ERB.new(File.read(File.join(File.dirname(__FILE__), 'templates', 'proxy.html.erb'))).result(binding)
9
+ eval(result)
10
+ return "KathyLee::#{klass}".constantize
11
+ end
12
+
13
+ end # class << self
14
+
15
+ end
@@ -0,0 +1,113 @@
1
+ module KathyLee
2
+ class Proxy
3
+ attr_accessor :for
4
+ attr_accessor :attributes
5
+ attr_accessor :belongs_tos
6
+ attr_accessor :has_manys
7
+
8
+ class << self
9
+
10
+ def define(options = {}, &block)
11
+ KathyLee::ProxyManager.set_attributes(self, options, &block)
12
+ end
13
+
14
+ def create(options = {})
15
+ model = self.new(options)
16
+ model.save!
17
+ return model
18
+ end
19
+
20
+ def new(options = {})
21
+ proxy = super(options)
22
+ KathyLee::Transaction.execute do
23
+ proxy.build_new
24
+ end
25
+ end
26
+
27
+ def sweatshop(count, options = {}, &block)
28
+ models = []
29
+ count.times do |i|
30
+ model = self.new(options)
31
+ yield model, i if block_given?
32
+ models << model
33
+ end
34
+ return models
35
+ end
36
+
37
+ def sweatshop!(count, &block)
38
+ nmodels = []
39
+ models = self.sweatshop(count, &block)
40
+ models.each do |m|
41
+ m.save!
42
+ nmodels << m
43
+ end
44
+ return models
45
+ end
46
+
47
+ end # class << self
48
+
49
+ def initialize(options = {})
50
+ self.for = self.class.for
51
+ self.belongs_tos = {}
52
+ self.has_manys = {}
53
+
54
+ opts = KathyLee::ProxyManager.get_attributes(self.class)
55
+ self.attributes = opts[:attributes]
56
+ instance_eval(&opts[:definition_block]) if opts[:definition_block]
57
+ self.attributes.merge!(options) if options.is_a?(Hash)
58
+ end
59
+
60
+ def build_new
61
+ model = self.for.send(:new, self.attributes)
62
+ KathyLee::Transaction.register(model)
63
+ model = build_has_manys(model)
64
+ model = build_belongs_to(model)
65
+ return model
66
+ end
67
+
68
+ def method_missing(sym, *args)
69
+ self.attributes[sym] = *args
70
+ end
71
+
72
+ def belongs_to(owner, options = {})
73
+ self.belongs_tos[owner.to_sym] = options
74
+ end
75
+
76
+ def has_many(things, options = {})
77
+ self.has_manys[things.to_sym] = options
78
+ end
79
+
80
+ def fake(sym, *args)
81
+ KathyLee::Fakes.execute(sym, *args)
82
+ end
83
+
84
+ protected
85
+ def build_has_manys(model)
86
+ hm = self.has_manys.dup
87
+ hm.each do |things, opts|
88
+ options = opts.dup
89
+ size = options.delete(:size) || 2
90
+ thing_klass = "::KathyLee::#{things.classify}".constantize
91
+ model.send("#{things}=", []) # clear out any parents versions of this
92
+ model.send(things) << thing_klass.sweatshop(size)
93
+ end
94
+
95
+ return model
96
+ end
97
+
98
+ def build_belongs_to(model)
99
+ bt = self.belongs_tos.dup
100
+
101
+ bt.each do |owner, opts|
102
+ thing_klass = "::KathyLee::#{owner.classify}".constantize
103
+ thing = KathyLee::Transaction.get_model(thing_klass.for) do
104
+ thing_klass.new(opts)
105
+ end
106
+ model.send("#{owner}=", thing)
107
+ end
108
+
109
+ return model
110
+ end
111
+
112
+ end # Proxy
113
+ end # KathyLee
@@ -0,0 +1,34 @@
1
+ module KathyLee
2
+ class ProxyManager
3
+ include Singleton
4
+
5
+ attr_accessor :proxies
6
+
7
+ def initialize
8
+ self.reset!
9
+ end
10
+
11
+ def reset!
12
+ self.proxies = {}
13
+ end
14
+
15
+ def get_attributes(klass)
16
+ proxy = self.proxies[klass]
17
+ return proxy if proxy
18
+ return {:attributes => {}}
19
+ end
20
+
21
+ def set_attributes(klass, attributes = {}, &block)
22
+ options = {:attributes => attributes}
23
+ options[:definition_block] = block if block_given?
24
+ self.proxies[klass] = options
25
+ end
26
+
27
+ class << self
28
+ def method_missing(sym, *args, &block)
29
+ KathyLee::ProxyManager.instance.send(sym, *args, &block)
30
+ end
31
+ end # class << self
32
+
33
+ end # ProxyManager
34
+ end # KathyLee
@@ -0,0 +1,15 @@
1
+ class KathyLee::<%= @klass %> < KathyLee::Proxy
2
+
3
+ class << self
4
+
5
+ def for
6
+ ::<%= @for_klass %>
7
+ end
8
+
9
+ def const_missing(klass, for_klass = <%= @klass %>)
10
+ KathyLee.const_missing(klass, for_klass)
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -0,0 +1,51 @@
1
+ module KathyLee
2
+ class Transaction
3
+ include Singleton
4
+
5
+ attr_accessor :started
6
+ attr_accessor :models
7
+ attr_accessor :transaction_id
8
+
9
+ class << self
10
+ def method_missing(sym, *args, &block)
11
+ KathyLee::Transaction.instance.send(sym, *args, &block)
12
+ end
13
+ end
14
+
15
+ def initialize
16
+ self.reset!
17
+ end
18
+
19
+ def reset!
20
+ self.started = false
21
+ self.models = {}
22
+ end
23
+
24
+ def register(model)
25
+ self.models[model.class.name.to_sym] = model
26
+ end
27
+
28
+ def get_model(klass, &block)
29
+ model = self.models[klass.name.to_sym]
30
+ if block_given? && model.nil?
31
+ model = yield
32
+ self.register(model)
33
+ end
34
+ return model
35
+ end
36
+
37
+ def execute(&block)
38
+ unless self.started
39
+ self.transaction_id = rand.to_s.gsub('0.', '')
40
+ self.started = true
41
+ results = yield if block_given?
42
+ self.reset!
43
+ return results
44
+ else
45
+ results = yield if block_given?
46
+ return results
47
+ end
48
+ end
49
+
50
+ end # Transaction
51
+ end # KathyLee
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kathy_lee
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - markbates
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-04 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: faker
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.3.1
24
+ version:
25
+ description: "kathy_lee was developed by: markbates"
26
+ email: ""
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README
33
+ - LICENSE
34
+ files:
35
+ - lib/kathy_lee/errors/no_fake_registered.rb
36
+ - lib/kathy_lee/fakes.rb
37
+ - lib/kathy_lee/kathy_lee.rb
38
+ - lib/kathy_lee/proxy.rb
39
+ - lib/kathy_lee/proxy_manager.rb
40
+ - lib/kathy_lee/templates/proxy.html.erb
41
+ - lib/kathy_lee/transaction.rb
42
+ - lib/kathy_lee.rb
43
+ - README
44
+ - LICENSE
45
+ has_rdoc: true
46
+ homepage: ""
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: magrathea
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: kathy_lee
73
+ test_files: []
74
+