makers 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +149 -0
- data/Rakefile +32 -0
- data/lib/generators/makers/model/model_generator.rb +15 -0
- data/lib/generators/makers/model/templates/maker.rb +2 -0
- data/lib/makers.rb +57 -0
- data/lib/makers/callbacks.rb +23 -0
- data/lib/makers/configuration.rb +10 -0
- data/lib/makers/definitions.rb +43 -0
- data/lib/makers/fetcher.rb +18 -0
- data/lib/makers/maker.rb +113 -0
- data/lib/makers/methods.rb +11 -0
- data/lib/makers/proxy.rb +52 -0
- data/lib/makers/railtie.rb +33 -0
- data/lib/makers/sequence.rb +19 -0
- data/lib/makers/version.rb +5 -0
- data/test/aliases_test.rb +20 -0
- data/test/associations_test.rb +50 -0
- data/test/attributes_test.rb +26 -0
- data/test/callbacks_test.rb +38 -0
- data/test/clean_test.rb +18 -0
- data/test/dependent_test.rb +30 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/group.rb +2 -0
- data/test/dummy/app/models/post.rb +3 -0
- data/test/dummy/app/models/user.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +22 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +37 -0
- data/test/dummy/config/environments/production.rb +87 -0
- data/test/dummy/config/environments/test.rb +47 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/db/migrate/20140613221835_create_users.rb +13 -0
- data/test/dummy/db/migrate/20140615180954_create_posts.rb +10 -0
- data/test/dummy/db/schema.rb +33 -0
- data/test/dummy/log/test.log +273 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/test/makers/people.rb +2 -0
- data/test/dummy/test/makers/users.rb +3 -0
- data/test/fabricators_test.rb +33 -0
- data/test/generators_test.rb +13 -0
- data/test/inheritance_test.rb +49 -0
- data/test/lists_test.rb +33 -0
- data/test/load_test.rb +13 -0
- data/test/merges_test.rb +31 -0
- data/test/test_helper.rb +26 -0
- metadata +202 -0
data/lib/makers/proxy.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
module Makers
|
2
|
+
class Proxy
|
3
|
+
include Callbacks
|
4
|
+
|
5
|
+
def initialize(maker, &block)
|
6
|
+
@maker = maker
|
7
|
+
@attributes = []
|
8
|
+
@sequences = {}
|
9
|
+
instance_eval &block
|
10
|
+
end
|
11
|
+
|
12
|
+
def attributes
|
13
|
+
{}.tap do |hash|
|
14
|
+
if @maker.parent
|
15
|
+
hash.merge! @maker.parent.proxy.attributes
|
16
|
+
end
|
17
|
+
@attributes.each do |name|
|
18
|
+
hash[name] = send(name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def sequence(name, &block)
|
24
|
+
@attributes << name
|
25
|
+
@sequences[name] = Sequence.new(&block)
|
26
|
+
class_eval do
|
27
|
+
define_method(name) { @sequences[name] }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(name, *args, &block)
|
32
|
+
unless name == :maker
|
33
|
+
options = args.extract_options!
|
34
|
+
strategy = options.delete(:strategy) || :build
|
35
|
+
if block_given?
|
36
|
+
logic = block
|
37
|
+
elsif maker = Makers.definitions.find(name) rescue nil
|
38
|
+
logic = -> { maker.send(strategy, options) }
|
39
|
+
elsif maker = Makers.definitions.find(name.to_s.singularize.to_sym) rescue nil
|
40
|
+
logic = -> { maker.send(strategy, (args.first || 1), options) }
|
41
|
+
elsif args.any?
|
42
|
+
logic = -> { args.first }
|
43
|
+
end
|
44
|
+
if defined? logic
|
45
|
+
@attributes.send (block_given? ? :push : :unshift), name
|
46
|
+
class_eval { define_method(name, logic) }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Makers
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
initializer 'makers' do
|
5
|
+
config.app_generators.test_framework(
|
6
|
+
config.app_generators.options[:rails][:test_framework],
|
7
|
+
fixture: false,
|
8
|
+
fixture_replacement: :makers
|
9
|
+
)
|
10
|
+
if defined? RSpec
|
11
|
+
require 'rspec/rails'
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.include Makers::Methods
|
14
|
+
config.after(:each) do
|
15
|
+
Makers.clean
|
16
|
+
end
|
17
|
+
end
|
18
|
+
else
|
19
|
+
class ActiveSupport::TestCase
|
20
|
+
include Makers::Methods
|
21
|
+
teardown do
|
22
|
+
Makers.clean
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
config.after_initialize do
|
29
|
+
Makers.load
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AliasesTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Makers.define do
|
7
|
+
maker :user, aliases: [:owner, :author] do
|
8
|
+
name 'name'
|
9
|
+
phone
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'aliases' do
|
15
|
+
assert_kind_of Makers::Maker, Makers.definitions.find(:user)
|
16
|
+
assert_kind_of Makers::Maker, Makers.definitions.find(:owner)
|
17
|
+
assert_kind_of Makers::Maker, Makers.definitions.find(:author)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AssociationsTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Makers.define do
|
7
|
+
maker :user do
|
8
|
+
maker :user_with_built_posts do
|
9
|
+
posts
|
10
|
+
end
|
11
|
+
maker :user_with_created_posts do
|
12
|
+
posts 2, strategy: :create
|
13
|
+
end
|
14
|
+
end
|
15
|
+
maker :post do
|
16
|
+
maker :post_with_built_user do
|
17
|
+
user
|
18
|
+
end
|
19
|
+
maker :post_with_created_user do
|
20
|
+
user strategy: :create
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
test 'belongs to association' do
|
27
|
+
user = build(:post_with_built_user).user
|
28
|
+
assert_kind_of User, user
|
29
|
+
assert user.new_record?
|
30
|
+
|
31
|
+
user = build(:post_with_created_user).user
|
32
|
+
assert_kind_of User, user
|
33
|
+
assert user.persisted?
|
34
|
+
end
|
35
|
+
|
36
|
+
test 'has many association' do
|
37
|
+
posts = build(:user_with_built_posts).posts
|
38
|
+
assert_equal 1, posts.size
|
39
|
+
post = posts.first
|
40
|
+
assert_kind_of Post, post
|
41
|
+
assert post.new_record?
|
42
|
+
|
43
|
+
posts = build(:user_with_created_posts).posts
|
44
|
+
assert_equal 2, posts.size
|
45
|
+
post = posts.first
|
46
|
+
assert_kind_of Post, post
|
47
|
+
assert post.persisted?
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AttributesTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Makers.define do
|
7
|
+
maker :user do
|
8
|
+
sequence(:email) { |n| "mail#{n}@example.com" }
|
9
|
+
sequence(:age)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'block sequence' do
|
15
|
+
assert_equal 'mail1@example.com', attributes_for(:user)[:email]
|
16
|
+
assert_equal 'mail2@example.com', build(:user).email
|
17
|
+
assert_equal 'mail3@example.com', create(:user).email
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'numeric sequence' do
|
21
|
+
assert_equal 1, attributes_for(:user)[:age]
|
22
|
+
assert_equal 2, build(:user).age
|
23
|
+
assert_equal 3, create(:user).age
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CallbacksTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Makers.configure do
|
7
|
+
before(:build) { |u| u.email = 'build@example.com' }
|
8
|
+
after(:build) { |u| u.phone = 1 }
|
9
|
+
before(:create) { |u| u.email = 'create@example.com' }
|
10
|
+
after(:create) { |u| u.phone = 2 }
|
11
|
+
end
|
12
|
+
Makers.define do
|
13
|
+
maker :user do
|
14
|
+
before(:build) { |u| u.name = 'build' }
|
15
|
+
after(:build) { |u| u.age = 1 }
|
16
|
+
before(:create) { |u| u.name = 'create' }
|
17
|
+
after(:create) { |u| u.age = 2 }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
test 'build callbacks' do
|
23
|
+
user = build(:user)
|
24
|
+
assert_equal 'build@example.com', user.email
|
25
|
+
assert_equal 1, user.phone
|
26
|
+
assert_equal 'build', user.name
|
27
|
+
assert_equal 1, user.age
|
28
|
+
end
|
29
|
+
|
30
|
+
test 'create callbacks' do
|
31
|
+
user = create(:user)
|
32
|
+
assert_equal 'create@example.com', user.email
|
33
|
+
assert_equal 2, user.phone
|
34
|
+
assert_equal 'create', user.name
|
35
|
+
assert_equal 2, user.age
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/test/clean_test.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CleanTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Makers.define do
|
7
|
+
maker :user do
|
8
|
+
end
|
9
|
+
end
|
10
|
+
create :user, 5
|
11
|
+
Makers.clean
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'clean records' do
|
15
|
+
assert_equal 0, User.count
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DependentTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Makers.define do
|
7
|
+
maker :user do
|
8
|
+
email { "#{name}@example.com" }
|
9
|
+
sequence(:username) { |n| "#{name}-#{n}" }
|
10
|
+
name 'name'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
test 'return attributes' do
|
16
|
+
assert_equal 'name-1', attributes_for(:user)[:username]
|
17
|
+
assert_equal 'name@example.com', attributes_for(:user)[:email]
|
18
|
+
end
|
19
|
+
|
20
|
+
test 'build instance' do
|
21
|
+
assert_equal 'name-1', build(:user).username
|
22
|
+
assert_equal 'name@example.com', build(:user).email
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'create instance' do
|
26
|
+
assert_equal 'name-1', create(:user).username
|
27
|
+
assert_equal 'name@example.com', create(:user).email
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|