makers 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +149 -0
  4. data/Rakefile +32 -0
  5. data/lib/generators/makers/model/model_generator.rb +15 -0
  6. data/lib/generators/makers/model/templates/maker.rb +2 -0
  7. data/lib/makers.rb +57 -0
  8. data/lib/makers/callbacks.rb +23 -0
  9. data/lib/makers/configuration.rb +10 -0
  10. data/lib/makers/definitions.rb +43 -0
  11. data/lib/makers/fetcher.rb +18 -0
  12. data/lib/makers/maker.rb +113 -0
  13. data/lib/makers/methods.rb +11 -0
  14. data/lib/makers/proxy.rb +52 -0
  15. data/lib/makers/railtie.rb +33 -0
  16. data/lib/makers/sequence.rb +19 -0
  17. data/lib/makers/version.rb +5 -0
  18. data/test/aliases_test.rb +20 -0
  19. data/test/associations_test.rb +50 -0
  20. data/test/attributes_test.rb +26 -0
  21. data/test/callbacks_test.rb +38 -0
  22. data/test/clean_test.rb +18 -0
  23. data/test/dependent_test.rb +30 -0
  24. data/test/dummy/README.rdoc +28 -0
  25. data/test/dummy/Rakefile +6 -0
  26. data/test/dummy/app/assets/javascripts/application.js +13 -0
  27. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  28. data/test/dummy/app/controllers/application_controller.rb +5 -0
  29. data/test/dummy/app/helpers/application_helper.rb +2 -0
  30. data/test/dummy/app/models/group.rb +2 -0
  31. data/test/dummy/app/models/post.rb +3 -0
  32. data/test/dummy/app/models/user.rb +3 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/test/dummy/bin/bundle +3 -0
  35. data/test/dummy/bin/rails +4 -0
  36. data/test/dummy/bin/rake +4 -0
  37. data/test/dummy/config.ru +4 -0
  38. data/test/dummy/config/application.rb +22 -0
  39. data/test/dummy/config/boot.rb +5 -0
  40. data/test/dummy/config/database.yml +25 -0
  41. data/test/dummy/config/environment.rb +5 -0
  42. data/test/dummy/config/environments/development.rb +37 -0
  43. data/test/dummy/config/environments/production.rb +87 -0
  44. data/test/dummy/config/environments/test.rb +47 -0
  45. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  46. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  47. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  48. data/test/dummy/config/initializers/inflections.rb +16 -0
  49. data/test/dummy/config/initializers/mime_types.rb +4 -0
  50. data/test/dummy/config/initializers/session_store.rb +3 -0
  51. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  52. data/test/dummy/config/locales/en.yml +23 -0
  53. data/test/dummy/config/routes.rb +56 -0
  54. data/test/dummy/config/secrets.yml +22 -0
  55. data/test/dummy/db/migrate/20140613221835_create_users.rb +13 -0
  56. data/test/dummy/db/migrate/20140615180954_create_posts.rb +10 -0
  57. data/test/dummy/db/schema.rb +33 -0
  58. data/test/dummy/log/test.log +273 -0
  59. data/test/dummy/public/404.html +67 -0
  60. data/test/dummy/public/422.html +67 -0
  61. data/test/dummy/public/500.html +66 -0
  62. data/test/dummy/public/favicon.ico +0 -0
  63. data/test/dummy/test/makers/people.rb +2 -0
  64. data/test/dummy/test/makers/users.rb +3 -0
  65. data/test/fabricators_test.rb +33 -0
  66. data/test/generators_test.rb +13 -0
  67. data/test/inheritance_test.rb +49 -0
  68. data/test/lists_test.rb +33 -0
  69. data/test/load_test.rb +13 -0
  70. data/test/merges_test.rb +31 -0
  71. data/test/test_helper.rb +26 -0
  72. metadata +202 -0
@@ -0,0 +1,11 @@
1
+ module Makers
2
+ module Methods
3
+
4
+ %w(build create attributes_for).each do |method|
5
+ define_method method do |name, *args|
6
+ Makers.definitions.find(name).send(method, *args)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -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,19 @@
1
+ module Makers
2
+ class Sequence
3
+
4
+ def initialize(&block)
5
+ @index = 0
6
+ @block = block
7
+ end
8
+
9
+ def generate(context)
10
+ @index += 1
11
+ if @block
12
+ context.instance_exec @index, &@block
13
+ else
14
+ @index
15
+ end
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ module Makers
2
+
3
+ VERSION = '0.1.2'
4
+
5
+ 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
@@ -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>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -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,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class Group < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class Post < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :posts
3
+ end
@@ -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>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'