makers 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +64 -70
  4. data/Rakefile +1 -14
  5. data/lib/generators/makers/install_generator.rb +20 -0
  6. data/lib/generators/makers/templates/definitions.rb +2 -0
  7. data/lib/makers.rb +8 -35
  8. data/lib/makers/definitions.rb +18 -27
  9. data/lib/makers/dsl/maker.rb +74 -0
  10. data/lib/makers/extensions/active_support/test_case.rb +15 -0
  11. data/lib/makers/maker.rb +32 -81
  12. data/lib/makers/proxy.rb +3 -42
  13. data/lib/makers/railtie.rb +10 -21
  14. data/lib/makers/version.rb +1 -1
  15. data/test/dummy/Rakefile +0 -1
  16. data/test/dummy/app/views/layouts/application.html.erb +9 -11
  17. data/test/dummy/bin/bundle +1 -0
  18. data/test/dummy/bin/rails +1 -0
  19. data/test/dummy/bin/rake +1 -0
  20. data/test/dummy/bin/setup +30 -0
  21. data/test/dummy/config/application.rb +3 -0
  22. data/test/dummy/config/database.yml +4 -22
  23. data/test/dummy/config/database.yml.travis +3 -0
  24. data/test/dummy/config/environments/development.rb +6 -2
  25. data/test/dummy/config/environments/production.rb +16 -24
  26. data/test/dummy/config/environments/test.rb +7 -12
  27. data/test/dummy/config/initializers/assets.rb +11 -0
  28. data/test/dummy/config/secrets.yml +1 -1
  29. data/test/dummy/db/migrate/20140613221835_create_users.rb +0 -2
  30. data/test/dummy/db/migrate/20140615180954_create_posts.rb +0 -3
  31. data/test/dummy/db/schema.rb +38 -14
  32. data/test/dummy/log/development.log +176 -0
  33. data/test/dummy/log/test.log +1529 -0
  34. data/test/dummy/test/makers.rb +20 -0
  35. data/test/generator_test.rb +18 -0
  36. data/test/makers_test.rb +66 -0
  37. data/test/test_helper.rb +5 -18
  38. metadata +31 -48
  39. data/lib/generators/makers/model/model_generator.rb +0 -15
  40. data/lib/generators/makers/model/templates/maker.rb +0 -2
  41. data/lib/makers/callbacks.rb +0 -23
  42. data/lib/makers/configuration.rb +0 -10
  43. data/lib/makers/fetcher.rb +0 -18
  44. data/lib/makers/methods.rb +0 -11
  45. data/lib/makers/sequence.rb +0 -19
  46. data/test/aliases_test.rb +0 -20
  47. data/test/associations_test.rb +0 -50
  48. data/test/attributes_test.rb +0 -26
  49. data/test/callbacks_test.rb +0 -38
  50. data/test/clean_test.rb +0 -18
  51. data/test/dependent_test.rb +0 -30
  52. data/test/dummy/README.rdoc +0 -28
  53. data/test/dummy/test/makers/people.rb +0 -2
  54. data/test/dummy/test/makers/users.rb +0 -3
  55. data/test/fabricators_test.rb +0 -33
  56. data/test/generators_test.rb +0 -13
  57. data/test/inheritance_test.rb +0 -49
  58. data/test/lists_test.rb +0 -33
  59. data/test/load_test.rb +0 -13
  60. data/test/merges_test.rb +0 -31
@@ -0,0 +1,15 @@
1
+ module Makers
2
+ module Extensions
3
+ module ActiveSupport
4
+ module TestCase
5
+
6
+ %w(build create).each do |method|
7
+ define_method method do |name, *args|
8
+ Makers.definitions.find(name).send method, *args
9
+ end
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,112 +1,63 @@
1
1
  module Makers
2
2
  class Maker
3
3
 
4
- attr_reader :options, :proxy
4
+ attr_reader :options, :assignments, :object
5
5
 
6
- def initialize(name, options={}, &block)
7
- @name = name
6
+ def initialize(options, assignments)
8
7
  @options = options
9
- @block = block
10
- @loaded = false
11
- Fetcher.new(name, options, &block)
12
- end
13
-
14
- def parent
15
- @parent ||= begin
16
- if @options[:parent]
17
- Makers.definitions.find(@options[:parent])
18
- end
19
- end
20
- end
21
-
22
- def attributes_for(options={})
23
- {}.tap do |hash|
24
- iterate_attributes(options, proxy) do |name, value|
25
- hash[name] = value
26
- end
27
- end
8
+ @assignments = assignments
9
+ @object = build_object
28
10
  end
29
11
 
30
12
  %w(build create).each do |name|
31
13
  define_method name do |*args|
32
14
  options = args.extract_options!
33
- single_name = :"#{name}_one"
15
+ action = :"#{name}_one"
34
16
  if args.any?
35
- [].tap do |list|
36
- args.first.times do
37
- list << send(single_name, options)
38
- end
17
+ collection = []
18
+ args.first.times do
19
+ collection << send(action, options)
39
20
  end
21
+ collection
40
22
  else
41
- send(single_name, options)
23
+ send action, options
42
24
  end
43
25
  end
44
26
  end
45
27
 
46
- %w(options attributes_for build create).each do |name|
47
- define_method :"#{name}_with_load" do |*args|
48
- load
49
- send :"#{name}_without_load", *args
28
+ def attributes
29
+ hash = {}
30
+ if options.has_key?(:parent)
31
+ hash.merge! Makers.definitions.find(options[:parent]).attributes
50
32
  end
51
- alias_method_chain name, :load
52
- end
53
-
54
- protected
55
-
56
- def load
57
- unless @loaded
58
- if parent
59
- @options = parent.options.merge(@options)
60
- end
61
- unless @options[:class] ||= @name.to_s.classify.constantize
62
- raise "Class not found for maker #{@name}"
63
- end
64
- @proxy = Proxy.new(self, &@block)
65
- @loaded = true
33
+ assignments.keys.each do |name|
34
+ hash[name] = object.send(name)
66
35
  end
36
+ hash
67
37
  end
68
38
 
69
- def build_one(options={})
70
- @options[:class].new.tap do |instance|
71
- trigger :before_build, instance
72
- iterate_attributes(options, instance) do |name, value|
73
- instance.send :"#{name}=", value
74
- end
75
- trigger :after_build, instance
76
- end
77
- end
39
+ private
78
40
 
79
- def create_one(options={})
80
- build_one(options).tap do |instance|
81
- trigger :before_create, instance
82
- if instance.save
83
- Makers.records << instance
84
- end
85
- trigger :after_create, instance
41
+ def build_one(overrides={})
42
+ instance = options[:class_name].constantize.new
43
+ attributes.merge(overrides).each do |name, value|
44
+ instance.send "#{name}=", value
86
45
  end
46
+ instance
87
47
  end
88
48
 
89
- def trigger(name, instance)
90
- globals = (Makers.configuration.callbacks[name] || [])
91
- locals = (proxy.callbacks[name] || [])
92
- (globals + locals).each do |callback|
93
- callback.call instance
94
- end
49
+ def create_one(overrides={})
50
+ instance = build_one(overrides)
51
+ instance.save
52
+ instance
95
53
  end
96
54
 
97
- def iterate_attributes(options={}, context)
98
- proxy.attributes.merge(options).each do |name, value|
99
- case value
100
- when Proc
101
- context.instance_eval &value
102
- when Sequence
103
- value.generate context
104
- else
105
- value
106
- end.tap do |value|
107
- yield name, value
108
- end
55
+ def build_object
56
+ klass = Class.new
57
+ assignments.each do |name, logic|
58
+ klass.send :define_method, name, &logic
109
59
  end
60
+ klass.new
110
61
  end
111
62
 
112
63
  end
@@ -1,51 +1,12 @@
1
1
  module Makers
2
2
  class Proxy
3
- include Callbacks
4
3
 
5
- def initialize(maker, &block)
6
- @maker = maker
7
- @attributes = []
8
- @sequences = {}
4
+ def initialize(&block)
9
5
  instance_eval &block
10
6
  end
11
7
 
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
8
+ def maker(*args, &block)
9
+ DSL::Maker.new *args, &block
49
10
  end
50
11
 
51
12
  end
@@ -1,32 +1,21 @@
1
1
  module Makers
2
2
  class Railtie < Rails::Railtie
3
3
 
4
- initializer 'makers' do
4
+ initializer :makers do
5
5
  config.app_generators.test_framework(
6
6
  config.app_generators.options[:rails][:test_framework],
7
- fixture: false,
8
- fixture_replacement: :makers
7
+ fixture: false
9
8
  )
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
9
+ if Dir.exist?(Rails.root.join('spec'))
10
+ directory = 'spec'
18
11
  else
19
- class ActiveSupport::TestCase
20
- include Makers::Methods
21
- teardown do
22
- Makers.clean
23
- end
24
- end
12
+ directory = 'test'
25
13
  end
26
- end
27
-
28
- config.after_initialize do
29
- Makers.load
14
+ path = Rails.root.join("#{directory}/makers.rb")
15
+ if File.exist?(path)
16
+ load path
17
+ end
18
+ ActiveSupport::TestCase.include Makers::Extensions::ActiveSupport::TestCase
30
19
  end
31
20
 
32
21
  end
@@ -1,5 +1,5 @@
1
1
  module Makers
2
2
 
3
- VERSION = '0.1.3'
3
+ VERSION = '0.2.0'
4
4
 
5
5
  end
@@ -2,5 +2,4 @@
2
2
  # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
3
 
4
4
  require File.expand_path('../config/application', __FILE__)
5
-
6
5
  Rails.application.load_tasks
@@ -1,14 +1,12 @@
1
1
  <!DOCTYPE html>
2
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>
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
+ <%= yield %>
11
+ </body>
14
12
  </html>
@@ -1,3 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
4
  load Gem.bin_path('bundler', 'bundle')
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  APP_PATH = File.expand_path('../../config/application', __FILE__)
3
4
  require_relative '../config/boot'
4
5
  require 'rails/commands'
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+
2
3
  require_relative '../config/boot'
3
4
  require 'rake'
4
5
  Rake.application.run
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pathname'
4
+
5
+ # path to your application root.
6
+ APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
7
+
8
+ Dir.chdir APP_ROOT do
9
+ # This script is a starting point to setup your application.
10
+ # Add necessary setup steps to this file:
11
+
12
+ puts "== Installing dependencies =="
13
+ system 'gem install bundler --conservative'
14
+ system 'bundle check || bundle install'
15
+
16
+ # puts "\n== Copying sample files =="
17
+ # unless File.exist?('config/database.yml')
18
+ # system 'cp config/database.yml.sample config/database.yml'
19
+ # end
20
+
21
+ puts "\n== Preparing database =="
22
+ system 'bin/rake db:setup'
23
+
24
+ puts "\n== Removing old logs and tempfiles =="
25
+ system 'rm -f log/*'
26
+ system 'rm -rf tmp/cache'
27
+
28
+ puts "\n== Restarting application server =="
29
+ system 'touch tmp/restart.txt'
30
+ end
@@ -18,5 +18,8 @@ module Dummy
18
18
  # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
19
19
  # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
20
20
  # config.i18n.default_locale = :de
21
+
22
+ # Do not swallow errors in after_commit/after_rollback callbacks.
23
+ config.active_record.raise_in_transactional_callbacks = true
21
24
  end
22
25
  end
@@ -1,25 +1,7 @@
1
- # SQLite version 3.x
2
- # gem install sqlite3
3
- #
4
- # Ensure the SQLite 3 gem is defined in your Gemfile
5
- # gem 'sqlite3'
6
- #
7
- default: &default
8
- adapter: sqlite3
9
- pool: 5
10
- timeout: 5000
11
-
12
1
  development:
13
- <<: *default
14
- database: db/development.sqlite3
2
+ adapter: postgresql
3
+ database: makers_development
15
4
 
16
- # Warning: The database defined as "test" will be erased and
17
- # re-generated from your development database when you run "rake".
18
- # Do not set this db to the same as development or production.
19
5
  test:
20
- <<: *default
21
- database: ":memory:"
22
-
23
- production:
24
- <<: *default
25
- database: db/production.sqlite3
6
+ adapter: postgresql
7
+ database: makers_test
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: <%= 'jdbc' if RUBY_ENGINE == 'jruby' %>postgresql
3
+ database: travis
@@ -1,4 +1,4 @@
1
- Dummy::Application.configure do
1
+ Rails.application.configure do
2
2
  # Settings specified here will take precedence over those in config/application.rb.
3
3
 
4
4
  # In the development environment your application's code is reloaded on
@@ -10,7 +10,7 @@ Dummy::Application.configure do
10
10
  config.eager_load = false
11
11
 
12
12
  # Show full error reports and disable caching.
13
- config.consider_all_requests_local = true
13
+ config.consider_all_requests_local = true
14
14
  config.action_controller.perform_caching = false
15
15
 
16
16
  # Don't care if the mailer can't send.
@@ -27,6 +27,10 @@ Dummy::Application.configure do
27
27
  # number of complex assets.
28
28
  config.assets.debug = true
29
29
 
30
+ # Asset digests allow you to set far-future HTTP expiration dates on all assets,
31
+ # yet still be able to expire them through the digest params.
32
+ config.assets.digest = true
33
+
30
34
  # Adds additional error checking when serving assets at runtime.
31
35
  # Checks for improperly declared sprockets dependencies.
32
36
  # Raises helpful error messages.
@@ -1,4 +1,4 @@
1
- Dummy::Application.configure do
1
+ Rails.application.configure do
2
2
  # Settings specified here will take precedence over those in config/application.rb.
3
3
 
4
4
  # Code is not reloaded between requests.
@@ -11,20 +11,18 @@ Dummy::Application.configure do
11
11
  config.eager_load = true
12
12
 
13
13
  # Full error reports are disabled and caching is turned on.
14
- config.consider_all_requests_local = false
14
+ config.consider_all_requests_local = false
15
15
  config.action_controller.perform_caching = true
16
16
 
17
17
  # Enable Rack::Cache to put a simple HTTP cache in front of your application
18
18
  # Add `rack-cache` to your Gemfile before enabling this.
19
- # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
19
+ # For large-scale production use, consider using a caching reverse proxy like
20
+ # NGINX, varnish or squid.
20
21
  # config.action_dispatch.rack_cache = true
21
22
 
22
- # Disable Rails's static asset server (Apache or nginx will already do this).
23
- if Rails::VERSION::MAJOR == 4 && Rails::VERSION::MINOR >= 2
24
- config.serve_static_files = false
25
- else
26
- config.serve_static_assets = false
27
- end
23
+ # Disable serving static files from the `/public` folder by default since
24
+ # Apache or NGINX already handles this.
25
+ config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
28
26
 
29
27
  # Compress JavaScripts and CSS.
30
28
  config.assets.js_compressor = :uglifier
@@ -33,21 +31,22 @@ Dummy::Application.configure do
33
31
  # Do not fallback to assets pipeline if a precompiled asset is missed.
34
32
  config.assets.compile = false
35
33
 
36
- # Generate digests for assets URLs.
34
+ # Asset digests allow you to set far-future HTTP expiration dates on all assets,
35
+ # yet still be able to expire them through the digest params.
37
36
  config.assets.digest = true
38
37
 
39
- # Version of your assets, change this if you want to expire all your assets.
40
- config.assets.version = '1.0'
38
+ # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb
41
39
 
42
40
  # Specifies the header that your server uses for sending files.
43
- # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
44
- # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
41
+ # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache
42
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
45
43
 
46
44
  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
47
45
  # config.force_ssl = true
48
46
 
49
- # Set to :debug to see everything in the log.
50
- config.log_level = :info
47
+ # Use the lowest log level to ensure availability of diagnostic information
48
+ # when problems arise.
49
+ config.log_level = :debug
51
50
 
52
51
  # Prepend all log lines with the following tags.
53
52
  # config.log_tags = [ :subdomain, :uuid ]
@@ -59,11 +58,7 @@ Dummy::Application.configure do
59
58
  # config.cache_store = :mem_cache_store
60
59
 
61
60
  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
62
- # config.action_controller.asset_host = "http://assets.example.com"
63
-
64
- # Precompile additional assets.
65
- # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
66
- # config.assets.precompile += %w( search.js )
61
+ # config.action_controller.asset_host = 'http://assets.example.com'
67
62
 
68
63
  # Ignore bad email addresses and do not raise email delivery errors.
69
64
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
@@ -76,9 +71,6 @@ Dummy::Application.configure do
76
71
  # Send deprecation notices to registered listeners.
77
72
  config.active_support.deprecation = :notify
78
73
 
79
- # Disable automatic flushing of the log to improve performance.
80
- # config.autoflush_log = false
81
-
82
74
  # Use default logging formatter so that PID and timestamp are not suppressed.
83
75
  config.log_formatter = ::Logger::Formatter.new
84
76