drailties 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +1 -0
  4. data/bin/drails +46 -0
  5. data/lib/drails/application.rb +8 -0
  6. data/lib/drails/domainserver.rb +5 -0
  7. data/lib/drails/generate.rb +21 -0
  8. data/lib/drails/projectionserver.rb +8 -0
  9. data/lib/drails/rake.rb +17 -0
  10. data/lib/drails/server/domain_server.rb +12 -0
  11. data/lib/drails/server/projection_servers.rb +15 -0
  12. data/lib/drails/server.rb +7 -0
  13. data/lib/drailties.rb +3 -0
  14. data/lib/generators/drails/app/USAGE +10 -0
  15. data/lib/generators/drails/app/app_generator.rb +24 -0
  16. data/lib/generators/drails/app/templates/build_validations_registry.rb +5 -0
  17. data/lib/generators/drails/app/templates/create_domain.rb +5 -0
  18. data/lib/generators/drails/app/templates/disco.yml +54 -0
  19. data/lib/generators/drails/command/USAGE +11 -0
  20. data/lib/generators/drails/command/command_generator.rb +44 -0
  21. data/lib/generators/drails/command/templates/command.rb +5 -0
  22. data/lib/generators/drails/command/templates/event.rb +9 -0
  23. data/lib/generators/drails/command_processor/USAGE +1 -0
  24. data/lib/generators/drails/command_processor/command_processor_generator.rb +23 -0
  25. data/lib/generators/drails/command_processor/templates/command_processor.rb +5 -0
  26. data/lib/generators/drails/controller/USAGE +1 -0
  27. data/lib/generators/drails/controller/controller_generator.rb +14 -0
  28. data/lib/generators/drails/controller/templates/controller.rb +58 -0
  29. data/lib/generators/drails/migration/USAGE +1 -0
  30. data/lib/generators/drails/migration/migration_generator.rb +27 -0
  31. data/lib/generators/drails/migration/templates/create_table_migration.rb +19 -0
  32. data/lib/generators/drails/projection/USAGE +15 -0
  33. data/lib/generators/drails/projection/projection_generator.rb +43 -0
  34. data/lib/generators/drails/projection/templates/domain_model.rb +5 -0
  35. data/lib/generators/drails/projection/templates/domain_projection.rb +5 -0
  36. data/lib/generators/drails/projection/templates/model.rb +5 -0
  37. data/lib/generators/drails/projection/templates/projection.rb +5 -0
  38. data/lib/generators/drails/scaffold/USAGE +29 -0
  39. data/lib/generators/drails/scaffold/scaffold_generator.rb +63 -0
  40. data/lib/rails-disco/version.rb +10 -0
  41. data/lib/tasks/db.rake +108 -0
  42. data/lib/tasks/split.rake +94 -0
  43. metadata +141 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 65530ec8ee4b4e0bb596565ebc246edd1cf6f098
4
+ data.tar.gz: 88187f39270abd6e9b3fa911f5e737ee5cc1196e
5
+ SHA512:
6
+ metadata.gz: 6069343b46957d55dfaf50b7ebddae0ff364c2a994ad9af6e2a54315ad6ca10e333bbe6047fd2669c66e813738fc8eeb9a668c080935f6ac6bc88147591f9f70
7
+ data.tar.gz: 88923b01ba585b115ec286b7def5c8c39994765b23bc73745fe770dd2564671507ee95a69a47f61f82a6f87cf730bab67d9daa0fb9ad9b439cb25068dc9db2c2
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Robert Kranz
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.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ Dir.glob('lib/tasks/*.rake').each {|r| import r}
data/bin/drails ADDED
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ unless Gem::Specification.find_by_name('drailties').gem_dir == File.expand_path("../..", __FILE__)
3
+ $:.push File.expand_path("../../lib", __FILE__)
4
+ $:.delete "#{Gem::Specification.find_by_name('drailties').gem_dir}/lib"
5
+ end
6
+
7
+ ARGV << '--help' if ARGV.empty?
8
+
9
+ aliases = {
10
+ 'ds' => 'domainserver',
11
+ 's' => 'server',
12
+ 'ps' => 'projectionserver',
13
+ 'g' => 'generate'
14
+ }
15
+
16
+
17
+ help_message = <<-EOT
18
+
19
+ Usage: drails COMMAND
20
+
21
+ where COMMAND is one of:
22
+
23
+ new creates a new drails application
24
+ generate(g) run a rails generator
25
+ domainserver(ds) start the domain server
26
+ projectionserver(ps) start the projection server
27
+ server(s) start both server
28
+ rake start a rake task
29
+ EOT
30
+
31
+ command = ARGV.shift
32
+ command = aliases[command] || command
33
+
34
+ case command
35
+ when 'new'
36
+ require 'drails/application'
37
+ when 'generate','rake','server', 'domainserver', 'projectionserver'
38
+ ROOT_DIR ||= Dir.pwd
39
+ require "drails/#{command}"
40
+ when '-h', '--help'
41
+ puts help_message
42
+ else
43
+ puts "Error: Command '#{command}' not recognized"
44
+ puts help_message
45
+ exit(1)
46
+ end
@@ -0,0 +1,8 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/rails/app/app_generator'
3
+
4
+ Rails::Generators::AppGenerator.start
5
+
6
+ ROOT_DIR = Dir.pwd
7
+ ARGV.unshift 'app'
8
+ require 'drails/generate'
@@ -0,0 +1,5 @@
1
+ puts 'Starting Domain Command Server'
2
+ puts '=> Ctrl-C to shutdown domain server'
3
+ trap(:INT) { exit }
4
+ Process.spawn({"ROOT_DIR" => ROOT_DIR}, "ruby #{File.expand_path '../server', __FILE__}/domain_server.rb")
5
+ Process.waitall
@@ -0,0 +1,21 @@
1
+ require 'rails/generators'
2
+
3
+ if [nil, "-h", "--help"].include?(ARGV.first)
4
+ puts "Usage: drails generate GENERATOR [args] [options]"
5
+ puts
6
+ puts "General options:"
7
+ puts " -h, [--help] # Print generator's options and usage"
8
+ puts " -p, [--pretend] # Run but do not make any changes"
9
+ puts " -f, [--force] # Overwrite files that already exist"
10
+ puts " -s, [--skip] # Skip files that already exist"
11
+ puts " -q, [--quiet] # Suppress status output"
12
+ puts
13
+ puts "Please choose a generator below."
14
+ puts
15
+ puts "Scaffold"
16
+ puts "Command"
17
+ puts "Projection"
18
+ exit(1)
19
+ end
20
+
21
+ Rails::Generators.invoke "drails:#{ARGV.shift}", ARGV, behavior: :invoke, destination_root: ROOT_DIR
@@ -0,0 +1,8 @@
1
+ puts 'Starting Projection Server'
2
+ puts '=> Ctrl-C to shutdown projections server'
3
+ trap(:INT) { exit }
4
+ count = [ARGV.shift.to_i, 1].max
5
+ count.times do |i|
6
+ Process.spawn({"ROOT_DIR" => ROOT_DIR, "WORKER_COUNT" => count.to_s, "WORKER_NUMBER" => i.to_s}, "ruby #{File.expand_path '../server', __FILE__}/projection_servers.rb")
7
+ end
8
+ Process.waitall
@@ -0,0 +1,17 @@
1
+ # Run rake tasks from my_gem once it's installed.
2
+ #
3
+ # Example:
4
+ # my_gem rake some-task
5
+ # my_gem rake some-task[args]
6
+ #
7
+ # Author:: N David Brown
8
+ gem_dir = File.expand_path("..", File.dirname(__FILE__))
9
+ $LOAD_PATH.unshift gem_dir # Look in gem directory for resources first.
10
+ require 'rake'
11
+ require 'pp'
12
+ pwd = Dir.pwd
13
+ Dir.chdir(gem_dir) # We'll load rakefile from the gem's dir.
14
+ Rake.application.init
15
+ Rake.application.load_rakefile
16
+ Dir.chdir(pwd) # Revert to original pwd for any path args passed to task.
17
+ Rake.application.invoke_task(ARGV.first)
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby.exe
2
+ require 'active_event'
3
+ require 'active_domain'
4
+ require 'active_record'
5
+
6
+ ROOT_DIR ||= ENV['ROOT_DIR']
7
+
8
+ ActiveEvent::Autoload.app_path = File.join ROOT_DIR, 'app'
9
+ ActiveDomain::Autoload.app_path = File.join ROOT_DIR, 'domain'
10
+
11
+ ActiveDomain::Server.base_path = ROOT_DIR
12
+ ActiveDomain::Server.run
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby.exe
2
+ require 'active_event'
3
+ require 'active_projection'
4
+ require 'active_record'
5
+
6
+ ROOT_DIR ||= ENV['ROOT_DIR']
7
+ WORKER_COUNT = [ENV['WORKER_COUNT'].to_i, 1].max
8
+ WORKER_NUMBER = ENV['WORKER_NUMBER'].to_i
9
+
10
+ ActiveEvent::Autoload.app_path = File.join ROOT_DIR, 'app'
11
+ ActiveProjection::Autoload.app_path = File.join(ROOT_DIR, 'app')
12
+ ActiveProjection::Autoload.worker_config = {path: File.join(ROOT_DIR, 'app', 'projections'), count: WORKER_COUNT, number: WORKER_NUMBER}
13
+
14
+ ActiveProjection::Server.base_path = ROOT_DIR
15
+ ActiveProjection::Server.run
@@ -0,0 +1,7 @@
1
+ puts 'Domain and Projection Server started'
2
+ puts '=> Ctrl-C to shutdown both server'
3
+ trap(:INT) { exit }
4
+ Process.spawn({"ROOT_DIR" => ROOT_DIR}, "ruby #{File.expand_path '../server', __FILE__}/domain_server.rb")
5
+ Process.spawn({"ROOT_DIR" => ROOT_DIR}, "ruby #{File.expand_path '../server', __FILE__}/projection_servers.rb")
6
+ Process.spawn({"ROOT_DIR" => ROOT_DIR}, "rails s")
7
+ Process.waitall
data/lib/drailties.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Drailties
2
+ # Your code goes here...
3
+ end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Add drails to your project.
3
+
4
+ Example:
5
+ rails generate drails:app
6
+
7
+ This will create:
8
+ config/build_validations_registry.rb
9
+ config/initializers/create_domain.rb
10
+ config/disco.yml
@@ -0,0 +1,24 @@
1
+ module Drails
2
+ module Generators
3
+ class AppGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :name, type: :string, default: '', banner: "[name]"
6
+
7
+ def copy_config
8
+ copy_file 'disco.yml', File.join('config', 'disco.yml')
9
+ end
10
+
11
+ def create_domain_initializer
12
+ template 'create_domain.rb', File.join('config', 'initializers', 'create_domain.rb')
13
+ end
14
+
15
+ def copy_validation_registry_initializer
16
+ copy_file 'build_validations_registry.rb', File.join('config', 'initializers', 'build_validations_registry.rb')
17
+ end
18
+
19
+ def add_drails_to_gemfile
20
+ gem "rails-disco"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_event'
2
+ require 'active_projection'
3
+ ActiveEvent::Autoload.app_path = File.expand_path('../../../app', __FILE__)
4
+ ActiveProjection::Autoload.app_path = File.expand_path('../../../app', __FILE__)
5
+ ActiveEvent::ValidationsRegistry.build
@@ -0,0 +1,5 @@
1
+ <% module_namespacing do -%>
2
+ class Domain
3
+ include ::ActiveEvent::Domain
4
+ end
5
+ <% end -%>
@@ -0,0 +1,54 @@
1
+ development:
2
+ domain_database:
3
+ adapter: sqlite3
4
+ database: db/domain_dev.sqlite3
5
+ pool: 5
6
+ timeout: 5000
7
+ projection_database:
8
+ adapter: sqlite3
9
+ database: db/projection_dev.sqlite3
10
+ pool: 5
11
+ timeout: 5000
12
+ drb_server:
13
+ scheme: druby
14
+ host: 127.0.0.1
15
+ port: 8787
16
+ event_connection:
17
+ scheme: amqp
18
+ host: 127.0.0.1
19
+ port: 5672
20
+ event_exchange: events
21
+
22
+ test:
23
+ domain_database:
24
+ adapter: sqlite3
25
+ database: db/domain_test.sqlite3
26
+ pool: 5
27
+ timeout: 5000
28
+ projection_database:
29
+ adapter: sqlite3
30
+ database: db/projection_test.sqlite3
31
+ pool: 5
32
+ timeout: 5000
33
+
34
+ production:
35
+ domain_database:
36
+ adapter: sqlite3
37
+ database: db/domain.sqlite3
38
+ pool: 5
39
+ timeout: 5000
40
+ projection_database:
41
+ adapter: sqlite3
42
+ database: db/projection.sqlite3
43
+ pool: 5
44
+ timeout: 5000
45
+ drb_server:
46
+ protocol: druby
47
+ host: 127.0.0.1
48
+ port: 8787
49
+ event_connection:
50
+ scheme: amqp
51
+ userinfo: amqp:password
52
+ host: 127.0.0.1
53
+ port: 9797
54
+ event_exchange: events
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Generates a Command, an Event and adds it to the CommandProcessor or creates one.
3
+
4
+ Example:
5
+ rails generate drails:command ThingCreate
6
+
7
+ This will create:
8
+ app/commands/thing_create_command.rb
9
+ app/events/thing_create_event.rb
10
+ domain/command_processors/thing_processor.rb if it not exists
11
+ Adds a method to this or the existing processor
@@ -0,0 +1,44 @@
1
+ module Drails
2
+ module Generators
3
+ class CommandGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :attributes, type: :array, default: [], banner: "attribute attribute"
6
+
7
+ def create_command_file
8
+ template 'command.rb', File.join('app/commands', class_path, "#{file_name}_command.rb")
9
+ end
10
+
11
+ def create_related_event_file
12
+ template 'event.rb', File.join('app/events', class_path, "#{file_name}_event.rb")
13
+ end
14
+
15
+ def create_command_processor
16
+ generate 'drails:command_processor', processor_file_name, '-s'
17
+ end
18
+
19
+ def add_to_command_processor
20
+ content = "
21
+
22
+ process #{class_name}Command do |command|
23
+ command.is_valid_do { event #{class_name}Event.new command.to_hash }
24
+ end"
25
+ inject_into_file File.join('domain/command_processors', domain_ns, "#{processor_file_name}_processor.rb"), content, after: /(\s)*include(\s)*ActiveDomain::CommandProcessor/
26
+ end
27
+
28
+ private
29
+ def processor_file_name
30
+ file_name.split('_').first
31
+ end
32
+
33
+ def domain_ns
34
+ namespace_path = Array.new class_path
35
+ if namespace_path.empty?
36
+ namespace_path[0] = 'domain'
37
+ else
38
+ namespace_path[-1] += '_domain'
39
+ end
40
+ namespace_path
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ <% module_namespacing do -%>class <%= class_name %>Command
2
+ include ActiveEvent::Command
3
+ attributes :id<%=',' unless attributes_names.empty? %> <%=attributes_names.map{|x| ":#{x}"}.join(', ')%>
4
+ end
5
+ <% end -%>
@@ -0,0 +1,9 @@
1
+ <% module_namespacing do -%>class <%= class_name %>Event
2
+ include ActiveEvent::EventType
3
+ attributes :id<%=',' unless attributes_names.empty? %> <%=attributes_names.map{|x| ":#{x}"}.join(', ')%>
4
+
5
+ def values
6
+ attributes_except :id
7
+ end
8
+ end
9
+ <% end -%>
@@ -0,0 +1 @@
1
+ Not intended for extern use!
@@ -0,0 +1,23 @@
1
+ module Drails
2
+ module Generators
3
+ class CommandProcessorGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ hide!
6
+
7
+ def create_command_processor_file
8
+ template 'command_processor.rb', File.join('domain/command_processors', domain_ns, "#{file_name}_processor.rb")
9
+ end
10
+
11
+ private
12
+ def domain_ns
13
+ namespace_path = Array.new class_path
14
+ if namespace_path.empty?
15
+ namespace_path[0] = 'domain'
16
+ else
17
+ namespace_path[-1] += '_domain'
18
+ end
19
+ namespace_path
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module <%=if Rails::Generators.namespace.present? then Rails::Generators.namespace.name end%>Domain
2
+ class <%= class_name %>Processor
3
+ include ActiveDomain::CommandProcessor
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ Not intended for extern use!
@@ -0,0 +1,14 @@
1
+ require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
2
+ module Drails
3
+ module Generators
4
+ class ControllerGenerator < Rails::Generators::ScaffoldControllerGenerator
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ hide!
7
+
8
+ private
9
+ def params_string
10
+ attributes.map{|x| "#{x.name}: params[:#{singular_table_name}][:#{x.name}]" }.join(', ')
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,58 @@
1
+ <% module_namespacing do -%>
2
+ class <%= controller_class_name %>Controller < ApplicationController
3
+ before_action :set_<%= singular_table_name %>, only: [:show, :edit]
4
+
5
+ def index
6
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
7
+ end
8
+
9
+ def show
10
+ end
11
+
12
+ def new
13
+ @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
14
+ end
15
+
16
+ def edit
17
+ end
18
+
19
+ def create
20
+ <%= singular_table_name %> = <%= "#{human_name}"%>CreateCommand.new({<%= params_string %>})
21
+ valid = <%= singular_table_name %>.valid?
22
+ if valid and Domain.run_command <%= singular_table_name %>
23
+ flash[:notice] = <%= "'#{human_name} was successfully created.'" %>
24
+ redirect_to action: :index
25
+ else
26
+ flash[:error] = <%= "'#{human_name} couldn\\'t be created.'" %>
27
+ redirect_to action: :new
28
+ end
29
+ end
30
+
31
+ def update
32
+ <%= singular_table_name %> = <%= "#{human_name}"%>UpdateCommand.new({id: params[:id]<%=',' unless params_string == ''%> <%= params_string %>})
33
+ valid = <%= singular_table_name %>.valid?
34
+ if valid and Domain.run_command <%= singular_table_name %>
35
+ flash[:notice] = <%= "'#{human_name} was successfully updated.'" %>
36
+ redirect_to action: :show, id: params[:id]
37
+ else
38
+ flash[:error] = <%= "'#{human_name} couldn\\'t be updated.'" %>
39
+ redirect_to action: :edit, id: params[:id]
40
+ end
41
+ end
42
+
43
+ def destroy
44
+ <%= singular_table_name %> = <%= "#{human_name}"%>DeleteCommand.new({id: params[:id]})
45
+ if Domain.run_command <%= singular_table_name %>
46
+ flash[:notice] = <%= "'#{human_name} was successfully deleted.'" %>
47
+ else
48
+ flash[:error] = <%= "'#{human_name} couldn\\'t be deleted.'" %>
49
+ end
50
+ redirect_to action: :index
51
+ end
52
+
53
+ private
54
+ def set_<%= singular_table_name %>
55
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
56
+ end
57
+ end
58
+ <% end -%>
@@ -0,0 +1 @@
1
+ Not intended for extern use!
@@ -0,0 +1,27 @@
1
+ require 'rails/generators/active_record/migration/migration_generator'
2
+ module Drails
3
+ module Generators
4
+ class MigrationGenerator < ActiveRecord::Generators::MigrationGenerator
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ hide!
7
+ remove_argument :attibutes
8
+ argument :use_domain, type: :string, default: 'false'
9
+ argument :attributes, type: :array, default: [], banner: "field[:type] field[:type]"
10
+
11
+ def create_migration_file
12
+ set_local_assigns!
13
+ validate_file_name!
14
+ migration_template @migration_template, "db/#{migration_dir}/#{file_name}.rb"
15
+ end
16
+
17
+ private
18
+ def migration_dir
19
+ if (/^(true|t|yes|y|1)$/i).match(use_domain)
20
+ 'migrate_domain'
21
+ else
22
+ 'migrate'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def change
3
+ create_table :<%= table_name %> do |t|
4
+ <% attributes.each do |attribute| -%>
5
+ <% if attribute.password_digest? -%>
6
+ t.string :password_digest<%= attribute.inject_options %>
7
+ <% else -%>
8
+ t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
9
+ <% end -%>
10
+ <% end -%>
11
+ <% if options[:timestamps] %>
12
+ t.timestamps
13
+ <% end -%>
14
+ end
15
+ <% attributes_with_index.each do |attribute| -%>
16
+ add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
17
+ <% end -%>
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ Description:
2
+ Generates a Projection and a Model.
3
+
4
+ Example:
5
+ rails generate drails:projection Thing [use_domain]
6
+
7
+ This will create if use_domain is false(standard):
8
+ app/projection/thing_projection.rb
9
+ app/models/thing.rb
10
+ db/migrate/create_thing.rb
11
+
12
+ This will create if use_domain is true:
13
+ domain/projection/thing_projection.rb
14
+ domain/models/thing.rb
15
+ db/migrate_domain/create_thing.rb
@@ -0,0 +1,43 @@
1
+ module Drails
2
+ module Generators
3
+ class ProjectionGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :use_domain, type: :string, default: 'false'
6
+ argument :attributes, type: :array, default: [], banner: "field[:type] field[:type]"
7
+
8
+ def create_projection_file
9
+ if domain?
10
+ template 'domain_projection.rb', File.join('domain', 'projections', domain_ns, "#{file_name}_projection.rb")
11
+ else
12
+ template 'projection.rb', File.join('app', 'projections', class_path, "#{file_name}_projection.rb")
13
+ end
14
+ end
15
+
16
+ def create_model_file
17
+ if domain?
18
+ template 'domain_model.rb', File.join('domain', 'models', domain_ns, "#{file_name}.rb")
19
+ else
20
+ template 'model.rb', File.join('app', 'models', class_path, "#{file_name}.rb")
21
+ end
22
+ end
23
+
24
+ def create_migration
25
+ generate 'drails:migration', "create_#{table_name}", use_domain, attributes.map { |x| "#{x.name}:#{x.type}" }.join(' ')
26
+ end
27
+
28
+ private
29
+ def domain?
30
+ @domain ||= !!(/^(true|t|yes|y|1)$/i).match(use_domain)
31
+ end
32
+
33
+ def domain_ns
34
+ namespace_path = Array.new class_path
35
+ if namespace_path.empty?
36
+ namespace_path[0] = 'domain'
37
+ else
38
+ namespace_path[-1] += '_domain'
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ module <%=if Rails::Generators.namespace.present? then Rails::Generators.namespace.name end%>Domain
2
+ class <%= class_name %> < ActiveRecord::Base
3
+ self.table_name = '<%=table_name %>'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module <%=if Rails::Generators.namespace.present? then Rails::Generators.namespace.name end%>Domain
2
+ class <%= class_name %>Projection
3
+ include ActiveDomain::Projection
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %> < ActiveRecord::Base
3
+ self.table_name = '<%=table_name %>'
4
+ end
5
+ <% end -%>
@@ -0,0 +1,5 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %>Projection
3
+ include ActiveProjection::ProjectionType
4
+ end
5
+ <% end -%>
@@ -0,0 +1,29 @@
1
+ Description:
2
+ Scaffolds a complete resource with models, migration, projections, controller, views
3
+ commands, events and command_processor.
4
+
5
+ Example:
6
+ rails generate drails:scaffold Thing
7
+
8
+ This will create:
9
+ app/commands/thing_create_command.rb
10
+ app/commands/thing_update_command.rb
11
+ app/commands/thing_delete_command.rb
12
+ app/controllers/things_controller
13
+ app/events/thing_create_event.rb
14
+ app/events/thing_update_event.rb
15
+ app/events/thing_delete_event.rb
16
+ app/helpers/thing_helper.rb
17
+ app/models/thing.rb
18
+ app/projections/thing_projection.rb
19
+ app/views/things/_form.html.erb
20
+ app/views/things/edit.html.erb
21
+ app/views/things/index.html.erb
22
+ app/views/things/new.html.erb
23
+ app/views/things/show.html.erb
24
+ db/migrate/create_thing.rb
25
+ db/migrate_domain/create_thing.rb
26
+ domain/command_processors/thing_processor.rb
27
+ domain/models/thing.rb
28
+ domain/projections/thing_projection.rb
29
+ a resource route
@@ -0,0 +1,63 @@
1
+ module Drails
2
+ module Generators
3
+ class ScaffoldGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+ argument :attributes, type: :array, default: [], banner: "field[:type] field[:type]"
6
+
7
+ def create_projections
8
+ generate 'drails:projection', name, 'false', attr_string
9
+ generate 'drails:projection', name, 'true', attr_string
10
+ end
11
+
12
+ def create_commands
13
+ %w(create update delete).each do |action|
14
+ attr_arg = attributes_names.join(' ') unless action == 'delete'
15
+ command = "#{name}_#{action}"
16
+ generate 'drails:command', command, attr_arg
17
+ add_to_projections(action)
18
+ end
19
+ end
20
+
21
+ def add_route
22
+ route "resources :#{plural_name}"
23
+ end
24
+
25
+ def create_controller
26
+ generate 'drails:controller', name, attr_string
27
+ end
28
+
29
+ private
30
+ def attr_string
31
+ @attr_string ||= attributes.map { |x| "#{x.name}:#{x.type}" }.join(' ')
32
+ end
33
+
34
+ def add_to_projections(action)
35
+ content = "
36
+
37
+ def #{name}_#{action}_event(event)
38
+ #{method_bodies[action]}
39
+ end"
40
+ inject_into_file File.join('app', 'projections', class_path, "#{file_name}_projection.rb"), content, after: /(\s)*include(\s)*ActiveProjection::ProjectionType/
41
+ inject_into_file File.join('domain', 'projections', domain_ns, "#{file_name}_projection.rb"), content, after: /(\s)*include(\s)*ActiveDomain::Projection/
42
+ end
43
+
44
+ def method_bodies
45
+ {
46
+ 'create' => "#{human_name}.create! event.values.merge(id: event.id)",
47
+ 'update' => "#{human_name}.find(event.id).update! event.values",
48
+ 'delete' => "#{human_name}.find(event.id).destroy!",
49
+ }
50
+ end
51
+
52
+ def domain_ns
53
+ namespace_path = Array.new class_path
54
+ if namespace_path.empty?
55
+ namespace_path[0] = 'domain'
56
+ else
57
+ namespace_path[-1] += '_domain'
58
+ end
59
+ namespace_path
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,10 @@
1
+ module RailsDisco
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+ PRE = nil
7
+
8
+ STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
9
+ end
10
+ end
data/lib/tasks/db.rake ADDED
@@ -0,0 +1,108 @@
1
+ require 'yaml'
2
+ require 'logger'
3
+ require 'active_record'
4
+
5
+ namespace :db do
6
+ def create_database config
7
+ options = {:charset => 'utf8', :collation => 'utf8_unicode_ci'}
8
+
9
+ create_db = lambda do |config|
10
+ ActiveRecord::Base.establish_connection config.merge('database' => nil)
11
+ ActiveRecord::Base.connection.create_database config['database'], options
12
+ ActiveRecord::Base.establish_connection config
13
+ end
14
+
15
+ begin
16
+ create_db.call config
17
+ rescue Mysql::Error => sqlerr
18
+ if sqlerr.errno == 1405
19
+ print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
20
+ root_password = $stdin.gets.strip
21
+
22
+ grant_statement = <<-SQL
23
+ GRANT ALL PRIVILEGES ON #{config['database']}.*
24
+ TO '#{config['username']}'@'localhost'
25
+ IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;
26
+ SQL
27
+
28
+ create_db.call config.merge('database' => nil, 'username' => 'root', 'password' => root_password)
29
+ else
30
+ $stderr.puts sqlerr.error
31
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: utf8, collation: utf8_unicode_ci"
32
+ $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
33
+ end
34
+ end
35
+ end
36
+
37
+ task :db_env do
38
+ DATABASE_ENV = ENV['DATABASE_ENV'] || 'development'
39
+ end
40
+
41
+ task :environment, [:file_name] => [:db_env] do |t, args|
42
+ if args.file_name == 'domain'
43
+ @migration_dir = ENV['MIGRATIONS_DIR'] || 'db/migrate_domain'
44
+ else
45
+ @migration_dir = ENV['MIGRATIONS_DIR'] || 'db/migrate'
46
+ end
47
+ end
48
+
49
+ task :configuration, [:file_name] => [:environment] do |t, args|
50
+ @config = YAML.load_file("config/disco.yml")[DATABASE_ENV]["#{args.file_name}_database"]
51
+ end
52
+
53
+ task :configure_connection => :configuration do
54
+ ActiveRecord::Base.establish_connection @config
55
+ ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
56
+ end
57
+
58
+ desc 'Create the database from config/disco.yml for the current DATABASE_ENV'
59
+ task :create => :configure_connection do
60
+ create_database @config
61
+ end
62
+
63
+ desc 'Drops the database for the current DATABASE_ENV'
64
+ task :drop => :configure_connection do
65
+ ActiveRecord::Base.connection.drop_database @config['database']
66
+ end
67
+
68
+ desc 'Migrate the database (options: VERSION=x, VERBOSE=false).'
69
+ task :migrate => :configure_connection do
70
+ ActiveRecord::Migration.verbose = true
71
+ ActiveRecord::Migrator.migrate @migration_dir, ENV['VERSION'] ? ENV['VERSION'].to_i : nil
72
+ end
73
+
74
+ desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
75
+ task :rollback => :configure_connection do
76
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
77
+ ActiveRecord::Migrator.rollback @migration_dir, step
78
+ end
79
+
80
+ desc 'Retrieves the current schema version number'
81
+ task :version => :configure_connection do
82
+ puts "Current version: #{ActiveRecord::Migrator.current_version}"
83
+ end
84
+
85
+ desc 'Migrates the domain database (for use in multi project setup)'
86
+ task :setup_domain do
87
+ Rake::Task[:'db:configuration'].invoke('domain')
88
+ cp_r Gem::Specification.find_by_name('active_event').gem_dir + '/db/migrate/.', 'db/migrate_domain'
89
+ Rake::Task[:'db:migrate'].invoke
90
+ end
91
+
92
+ desc 'Migrates the projection database (for use in multi project setup)'
93
+ task :setup_projection do
94
+ Rake::Task[:'db:configuration'].invoke('projection')
95
+ cp_r Gem::Specification.find_by_name('active_projection').gem_dir + '/db/migrate/.', 'db/migrate'
96
+ Rake::Task[:'db:migrate'].invoke
97
+ end
98
+
99
+ desc 'Migrates the domain and the projection database (for use in single project setup)'
100
+ task :setup do
101
+ Rake::Task[:'db:setup_domain'].invoke
102
+ Rake::Task[:'db:environment'].reenable
103
+ Rake::Task[:'db:configure_connection'].reenable
104
+ Rake::Task[:'db:configuration'].reenable
105
+ Rake::Task[:'db:migrate'].reenable
106
+ Rake::Task[:'db:setup_projection'].invoke
107
+ end
108
+ end
@@ -0,0 +1,94 @@
1
+ def do_in_parent_dir
2
+ cd '../'
3
+ yield
4
+ cd PROJECT_NAME
5
+ end
6
+
7
+ task :environment do
8
+ PROJECT_NAME = File.basename(pwd)
9
+ end
10
+
11
+ task :create_domain => :create_frontend do
12
+ dir = "#{PROJECT_NAME}_domain"
13
+ app_dir = "#{dir}/app"
14
+ config_dir = "#{dir}/config"
15
+ bin_dir = "#{dir}/bin"
16
+ db_dir = "#{dir}/db"
17
+ do_in_parent_dir do
18
+ mkdir dir unless Dir.exists?(dir)
19
+ mkdir app_dir unless Dir.exists?(app_dir)
20
+ move "#{FRONTEND_DIR}/domain/command_processors", app_dir
21
+ move "#{FRONTEND_DIR}/domain/models", app_dir
22
+ move "#{FRONTEND_DIR}/domain/projections", app_dir
23
+ move "#{FRONTEND_DIR}/domain/validations", app_dir
24
+ remove_dir "#{FRONTEND_DIR}/domain"
25
+ mkdir config_dir unless Dir.exists?(config_dir)
26
+ move "#{FRONTEND_DIR}/config/disco.yml", config_dir
27
+ mkdir bin_dir unless Dir.exists?(bin_dir)
28
+ move "#{FRONTEND_DIR}/bin/domain_server.rb", bin_dir
29
+ mkdir db_dir unless Dir.exists?(db_dir)
30
+ move "#{FRONTEND_DIR}/db/migrate_domain/.", "#{db_dir}/migrate"
31
+ remove_dir "#{FRONTEND_DIR}/db" if Dir["#{FRONTEND_DIR}/db/*"].empty?
32
+ #todo: gemfile and gemspec
33
+ end
34
+ end
35
+
36
+ task :create_projection => :create_frontend do
37
+ dir = "#{PROJECT_NAME}_projection"
38
+ app_dir = "#{dir}/app"
39
+ config_dir = "#{dir}/config"
40
+ bin_dir = "#{dir}/bin"
41
+ db_dir = "#{dir}/db"
42
+ do_in_parent_dir do
43
+ mkdir dir unless Dir.exists?(dir)
44
+ mkdir app_dir unless Dir.exists?(app_dir)
45
+ move "#{FRONTEND_DIR}/app/models", app_dir
46
+ move "#{FRONTEND_DIR}/app/projections", app_dir
47
+ mkdir config_dir unless Dir.exists?(config_dir)
48
+ move "#{FRONTEND_DIR}/config/disco.yml", config_dir
49
+ mkdir bin_dir unless Dir.exists?(bin_dir)
50
+ move "#{FRONTEND_DIR}/bin/projection_servers.rb", bin_dir
51
+ mkdir db_dir unless Dir.exists?(db_dir)
52
+ move "#{FRONTEND_DIR}/db/migrate/.", "#{db_dir}/migrate"
53
+ remove_dir "#{FRONTEND_DIR}/db" if Dir["#{FRONTEND_DIR}/db/*"].empty?
54
+ #todo: gemfile and gemspec
55
+ end
56
+ end
57
+
58
+ task :create_interface => :create_frontend do
59
+ dir = "#{PROJECT_NAME}_interface"
60
+ app_dir = "#{dir}/app"
61
+ lib_dir = "#{dir}/lib"
62
+ do_in_parent_dir do
63
+ mkdir dir unless Dir.exists?(dir)
64
+ mkdir app_dir unless Dir.exists?(app_dir)
65
+ move "#{FRONTEND_DIR}/app/commands", app_dir
66
+ move "#{FRONTEND_DIR}/app/events", app_dir
67
+ move "#{FRONTEND_DIR}/app/validations", app_dir
68
+ mkdir lib_dir unless Dir.exists?(lib_dir)
69
+ temp_dir = "#{lib_dir}/#{PROJECT_NAME}"
70
+ mkdir temp_dir unless Dir.exists?(temp_dir)
71
+ move "#{FRONTEND_DIR}/lib/#{PROJECT_NAME}/domain.rb", temp_dir
72
+ move "#{FRONTEND_DIR}/lib/#{PROJECT_NAME}.rb", "#{lib_dir}/#{dir}.rb"
73
+ #todo: delete first line in this file
74
+ #todo: gemfile and gemspec
75
+ end
76
+ end
77
+
78
+ task :create_frontend => :environment do
79
+ FRONTEND_DIR = "#{PROJECT_NAME}_frontend"
80
+ app_dir = "#{FRONTEND_DIR}/app"
81
+ lib_dir = "#{FRONTEND_DIR}/lib"
82
+ do_in_parent_dir do
83
+ mkdir FRONTEND_DIR unless Dir.exists?(FRONTEND_DIR)
84
+ cp_r "#{PROJECT_NAME}/.", FRONTEND_DIR
85
+ cp_r "#{FRONTEND_DIR}/lib/#{PROJECT_NAME}.rb", "#{lib_dir}/#{FRONTEND_DIR}.rb"
86
+ #todo: delete line 2, 5 and 6 in this file
87
+ end
88
+ end
89
+
90
+
91
+ desc 'split this'
92
+ task :split => [:create_projection, :create_domain, :create_interface] do
93
+ puts 'done'
94
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drailties
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Kranz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_event
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_domain
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: active_projection
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ description: 'Internals: rake tasks, generators, commandline interface'
70
+ email: robert.kranz@hicknhack-software.com
71
+ executables:
72
+ - drails
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/drails/application.rb
77
+ - lib/drails/domainserver.rb
78
+ - lib/drails/generate.rb
79
+ - lib/drails/projectionserver.rb
80
+ - lib/drails/rake.rb
81
+ - lib/drails/server/domain_server.rb
82
+ - lib/drails/server/projection_servers.rb
83
+ - lib/drails/server.rb
84
+ - lib/drailties.rb
85
+ - lib/generators/drails/app/app_generator.rb
86
+ - lib/generators/drails/app/templates/build_validations_registry.rb
87
+ - lib/generators/drails/app/templates/create_domain.rb
88
+ - lib/generators/drails/app/templates/disco.yml
89
+ - lib/generators/drails/app/USAGE
90
+ - lib/generators/drails/command/command_generator.rb
91
+ - lib/generators/drails/command/templates/command.rb
92
+ - lib/generators/drails/command/templates/event.rb
93
+ - lib/generators/drails/command/USAGE
94
+ - lib/generators/drails/command_processor/command_processor_generator.rb
95
+ - lib/generators/drails/command_processor/templates/command_processor.rb
96
+ - lib/generators/drails/command_processor/USAGE
97
+ - lib/generators/drails/controller/controller_generator.rb
98
+ - lib/generators/drails/controller/templates/controller.rb
99
+ - lib/generators/drails/controller/USAGE
100
+ - lib/generators/drails/migration/migration_generator.rb
101
+ - lib/generators/drails/migration/templates/create_table_migration.rb
102
+ - lib/generators/drails/migration/USAGE
103
+ - lib/generators/drails/projection/projection_generator.rb
104
+ - lib/generators/drails/projection/templates/domain_model.rb
105
+ - lib/generators/drails/projection/templates/domain_projection.rb
106
+ - lib/generators/drails/projection/templates/model.rb
107
+ - lib/generators/drails/projection/templates/projection.rb
108
+ - lib/generators/drails/projection/USAGE
109
+ - lib/generators/drails/scaffold/scaffold_generator.rb
110
+ - lib/generators/drails/scaffold/USAGE
111
+ - lib/rails-disco/version.rb
112
+ - lib/tasks/db.rake
113
+ - lib/tasks/split.rake
114
+ - Rakefile
115
+ - MIT-LICENSE
116
+ - bin/drails
117
+ homepage: https://github.com/hicknhack-software/rails-disco
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.0.5
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Tools for working with Rails Disco
141
+ test_files: []