mongo_db_gen 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -15,7 +15,22 @@ Setup use of Mongoid
15
15
 
16
16
  Create new model Person with name attribute
17
17
 
18
- rails g mongoid:model person name:string
18
+ rails g mongoid:model person name:string
19
+
20
+ Create document Address with attributes defaulting to string
21
+
22
+ rails g mongoid:model address name street
23
+
24
+ TODO:
25
+ It should support inheritance
26
+
27
+ class Browser < Canvas
28
+ field :version, :type => Integer
29
+ end
30
+
31
+ Fx like this:
32
+
33
+ rails g mongoid:model browser version:integer --inherit canvas
19
34
 
20
35
  Mongo Mapper
21
36
  ------------
@@ -33,6 +48,9 @@ Create new Mongo Mapper model Person with name attribute
33
48
 
34
49
  rails g mongoid:model person name:string
35
50
 
51
+ Create Embedded document Address with attributes defaulting to string
52
+ rails g mongoid:model address name street --embedded
53
+
36
54
  Notice
37
55
  ======
38
56
  This is currently a work in progress. Please feel free to join in the effort and fork this project and fix whatever bugs etc.
data/README.rdoc CHANGED
@@ -1,8 +1,18 @@
1
1
  = Mongo DB generators
2
2
 
3
- The Mongo DB Rails 3 generators includes the following generators:
3
+ The Mongo DB Rails 3 generators includes generators for both Mongo Mapper and Mongoid.
4
+
5
+ Add the following to your Gemfile in your Rails 3 project:
6
+
7
+ gem 'mongo_ext'
8
+ gem 'mongoid'
9
+ gem "mongo_mapper", :git => 'http://github.com/merbjedi/mongomapper.git', :branch => 'rails3'
10
+
11
+ $ bundle install
4
12
 
5
13
  == Mongoid
14
+
15
+ Includes the following generators
6
16
 
7
17
  * mongoid:setup - setup Rails app to use Mongoid
8
18
  * mongoid:model - generates a Mongoid model
@@ -19,6 +29,8 @@ Create new model Person with name attribute
19
29
 
20
30
  == Mongo Mapper
21
31
 
32
+ Includes the following generators
33
+
22
34
  * mongo_mapper:setup - setup Rails app to use Mongo Mapper
23
35
  * mongo_mapper:model - generates a Mongo Mapper model
24
36
 
@@ -1,9 +1,75 @@
1
+ require 'rails/generators/base'
2
+
1
3
  module MongoMapper
2
4
  module Generators
3
- class ModelGenerator < Rails::Generators::NamedBase
4
-
5
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
5
+ class ModelGenerator < Rails::Generators::Base
6
+
7
+ argument :model_name, :type => :string, :required => true, :banner => 'ModelName'
8
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
9
+
10
+ class_option :embedded, :type => :boolean, :aliases => "-E", :default => false,
11
+ :desc => "Use EmbeddedDocument for model"
12
+
13
+ attr_accessor :model_attributes
14
+
15
+ def initialize(*args, &block)
16
+ super
6
17
 
18
+ @model_attributes = []
19
+
20
+ attributes.each do |arg|
21
+ if arg.include?(':')
22
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
23
+ else
24
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(arg, "string")
25
+ end
26
+ end
27
+
28
+ @model_attributes.uniq!
29
+
30
+ if @model_attributes.empty?
31
+ if model_exists?
32
+ model_columns_for_attributes.each do |column|
33
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
34
+ end
35
+ else
36
+ @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
37
+ end
38
+ end
39
+ end
40
+
41
+ def create_model_file
42
+ template 'model.rb', "app/models/#{singular_name}.rb"
43
+ end
44
+
45
+ no_tasks do
46
+ def model_exists?
47
+ File.exist? destination_path("app/models/#{singular_name}.rb")
48
+ end
49
+
50
+ def singular_name
51
+ model_name.underscore
52
+ end
53
+
54
+ def plural_name
55
+ model_name.underscore.pluralize
56
+ end
57
+
58
+ def class_name
59
+ model_name.camelize
60
+ end
61
+
62
+ def plural_class_name
63
+ plural_name.camelize
64
+ end
65
+
66
+ def model_columns_for_attributes
67
+ class_name.constantize.columns.reject do |column|
68
+ column.name.to_s =~ /^(id|created_at|updated_at)$/
69
+ end
70
+ end
71
+ end
72
+
7
73
  def self.source_root
8
74
  @source_root ||= File.expand_path('../templates', __FILE__)
9
75
  end
@@ -11,12 +77,7 @@ module MongoMapper
11
77
  def self.banner
12
78
  "#{$0} mongo_mapper:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
13
79
  end
14
-
15
- def create_model_file
16
- template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
17
- end
18
-
19
- # hook_for :test
80
+
20
81
  end
21
82
  end
22
83
  end
@@ -0,0 +1,9 @@
1
+ class <%= class_name %>
2
+ <% if options[:embedded] %>
3
+ include MongoMapper::EmbeddedDocument
4
+ <% else %>
5
+ include MongoMapper::Document
6
+ <% end %>
7
+
8
+ <%= model_attributes.map { |a| "key :#{a.name}, #{a.type.capitalize}" }.join("\n") %>
9
+ end
@@ -1,12 +1,23 @@
1
- require 'generators/mongo_db_base'
1
+ # require 'thor-ext'
2
+ require 'rails/generators/base'
2
3
 
3
4
  module MongoMapper
4
5
  module Generators
5
6
  class SetupGenerator < Rails::Generators::Base
6
- include MongoDatabase::Generators::Base
7
+ # include ThorExtensions
7
8
 
8
9
  argument :database, :type => :string, :default => 'mongo_db_default'
9
10
 
11
+ # def configure_gems
12
+ # cleanup_gemfile
13
+ # add_gems %w{mongo_ext mongo mongo_mapper}
14
+ # end
15
+
16
+ def create_files
17
+ template "mongo_mapper_db_config.rb" , "config/initializers/mongo_mapper_db_config.rb"
18
+ puts "Please ensure Gemfile contains: gem 'mongo_mapper'"
19
+ end
20
+
10
21
  def self.source_root
11
22
  @source_root ||= File.expand_path('../templates', __FILE__)
12
23
  end
@@ -15,13 +26,6 @@ module MongoMapper
15
26
  "#{$0} mongo_mapper:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
16
27
  end
17
28
 
18
- def configure_gems
19
- add_gems %w{mongo_ext mongo mongo_mapper}
20
- end
21
-
22
- def create_files
23
- template "mongo_mapper_db_config.rb" , "config/initializers/mongo_mapper_db_config.rb"
24
- end
25
29
 
26
30
  end
27
31
  end
@@ -1,8 +1,68 @@
1
+ require 'rails/generators/base'
2
+
1
3
  module Mongoid
2
4
  module Generators
3
- class ModelGenerator < Rails::Generators::NamedBase
5
+ class ModelGenerator < Rails::Generators::Base
6
+ argument :model_name, :type => :string, :required => true, :banner => 'ModelName'
4
7
  argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
5
-
8
+
9
+ class_option :inherit, :type => :string, :aliases => "-I",
10
+ :desc => "Embed document by Inheriting"
11
+
12
+
13
+ attr_accessor :model_attributes
14
+
15
+ def initialize(*args, &block)
16
+ super
17
+
18
+ @model_attributes = []
19
+
20
+ attributes.each do |arg|
21
+ if arg.include?(':')
22
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
23
+ else
24
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(arg, "string")
25
+ end
26
+ end
27
+
28
+ @model_attributes.uniq!
29
+
30
+ if @model_attributes.empty?
31
+ if model_exists?
32
+ model_columns_for_attributes.each do |column|
33
+ @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
34
+ end
35
+ else
36
+ @model_attributes << Rails::Generators::GeneratedAttribute.new('name', 'string')
37
+ end
38
+ end
39
+ end
40
+
41
+ def create_model_file
42
+ template 'model.rb', "app/models/#{singular_name}.rb"
43
+ end
44
+
45
+ no_tasks do
46
+ def model_exists?
47
+ File.exist? destination_path("app/models/#{singular_name}.rb")
48
+ end
49
+ def singular_name
50
+ model_name.underscore
51
+ end
52
+
53
+ def plural_name
54
+ model_name.underscore.pluralize
55
+ end
56
+
57
+ def class_name
58
+ model_name.camelize
59
+ end
60
+
61
+ def plural_class_name
62
+ plural_name.camelize
63
+ end
64
+ end
65
+
6
66
  def self.source_root
7
67
  @source_root ||= File.expand_path('../templates', __FILE__)
8
68
  end
@@ -10,12 +70,7 @@ module Mongoid
10
70
  def self.banner
11
71
  "#{$0} mongoid:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
12
72
  end
13
-
14
- def create_model_file
15
- template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
16
- end
17
-
18
- # hook_for :test
73
+
19
74
  end
20
75
  end
21
76
  end
@@ -0,0 +1,5 @@
1
+ class <%= class_name.camelize %> <%= "< #{options[:inherit]}" if options[:inherit] %>
2
+ include Mongoid::Document
3
+
4
+ <%= model_attributes.map { |a| "field :#{a.name}, :type => #{a.type.capitalize}" }.join("\n") %>
5
+ end
@@ -1,12 +1,25 @@
1
- require 'generators/mongo_db_base'
1
+ # require 'thor-ext'
2
+ require 'rails/generators/base'
2
3
 
3
4
  module Mongoid
4
5
  module Generators
5
6
  class SetupGenerator < Rails::Generators::Base
6
- include MongoDatabase::Generators::Base
7
+ # include ThorExtensions
7
8
 
8
9
  argument :database, :type => :string, :default => 'mongo_db_default'
9
10
 
11
+ # def configure_gems
12
+ # cleanup_gemfile
13
+ # add_gems %w{mongo_ext mongo mongoid}
14
+ # end
15
+
16
+ def create_files
17
+ template "mongoid.rb" , "config/initializers/mongoid.rb"
18
+ template 'database.mongo.yml', "config/database.mongo.yml"
19
+
20
+ puts "Please ensure Gemfile contains: gem 'mongoid'"
21
+ end
22
+
10
23
  def self.source_root
11
24
  @source_root ||= File.expand_path('../templates', __FILE__)
12
25
  end
@@ -15,15 +28,6 @@ module Mongoid
15
28
  "#{$0} mongoid:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
16
29
  end
17
30
 
18
- def configure_gems
19
- add_gems %w{mongo_ext mongo mongoid}
20
- end
21
-
22
- def create_files
23
- template "mongoid.rb" , "config/initializers/mongoid.rb"
24
- template 'database.mongo.yml', "config/database.mongo.yml"
25
- end
26
-
27
31
  end
28
32
  end
29
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_db_gen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Mandrup
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-07 00:00:00 +01:00
12
+ date: 2010-02-21 00:00:00 -05:00
13
13
  default_executable: mongomap_model
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -50,11 +50,12 @@ files:
50
50
  - bin/mongoid_setup
51
51
  - bin/mongomapper_model
52
52
  - bin/mongomapper_setup
53
- - lib/generators/mongo_db_base.rb
54
53
  - lib/generators/mongo_mapper/model/model_generator.rb
54
+ - lib/generators/mongo_mapper/model/templates/model.rb
55
55
  - lib/generators/mongo_mapper/setup/setup_generator.rb
56
56
  - lib/generators/mongo_mapper/setup/templates/mongo_mapper_db_config.rb
57
57
  - lib/generators/mongoid/model/model_generator.rb
58
+ - lib/generators/mongoid/model/templates/model.rb
58
59
  - lib/generators/mongoid/setup/setup_generator.rb
59
60
  - lib/generators/mongoid/setup/templates/database.mongo.yml
60
61
  - lib/generators/mongoid/setup/templates/mongoid.rb
@@ -1,15 +0,0 @@
1
- require 'thor-ext'
2
-
3
- module MongoDatabase
4
- module Generators
5
- module Base
6
- include ThorExtensions
7
-
8
- def configure_gems
9
- cleanup_gemfile
10
- add_gems(%w{mongo mongo-ext mongoid})
11
- end
12
- end
13
- end
14
- end
15
-