railsmdb 1.0.0.alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'railsmdb/generators/setup/concerns/setuppable'
4
+
5
+ module Rails
6
+ module Generators
7
+ # Monkeypatch the Rails AppGenerator class to make it emit a Mongoid-friendly
8
+ # application.
9
+ #
10
+ # @api private
11
+ class AppGenerator
12
+ include Railsmdb::Generators::Setup::Concerns::Setuppable
13
+
14
+ # change the --skip-active-record option to default to true. Users
15
+ # may pass --no-skip-active-record to enable it again, if they want
16
+ # to use both Mongoid and ActiveRecord.
17
+ class_option :skip_active_record, type: :boolean,
18
+ aliases: '-O',
19
+ default: true,
20
+ desc: 'Skip Active Record files'
21
+
22
+ prioritize :save_initial_path, :before, :create_root
23
+ prioritize :confirm_legal_shenanigans, :before, :create_root
24
+ prioritize :fetch_crypt_shared, :after, :create_root
25
+ prioritize :add_mongoid_gem_entries, :before, :run_bundle
26
+
27
+ public_task :save_initial_path
28
+ public_task :confirm_legal_shenanigans
29
+ public_task :fetch_crypt_shared
30
+ public_task :add_mongoid_gem_entries
31
+ public_task :mongoid_yml
32
+ public_task :add_mongodb_local_master_key_to_credentials
33
+ public_task :add_encryption_options_to_mongoid_yml
34
+ public_task :mongoid_initializer
35
+ public_task :railsmdb
36
+
37
+ # OVERRIDES
38
+ # The following methods override the existing methods on AppGenerator,
39
+ # to replace the default behavior with Mongoid-specific behavior.
40
+
41
+ # Overridden to ignore the "skip_active_record" guard. We always want the
42
+ # db folder, even when active record is being skipped.
43
+ def create_db_files
44
+ build(:db)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'railsmdb/ext/rails/command/behavior'
5
+
6
+ module Rails
7
+ module Generators # :nodoc:
8
+ class << self
9
+ private
10
+
11
+ # Prepends the railsmdb generators lookup path to the existing rails
12
+ # lookup paths.
13
+ def railsmdb_lookup_paths
14
+ @railsmdb_lookup_paths ||= [ 'railsmdb/generators', *rails_lookup_paths ]
15
+ end
16
+
17
+ alias rails_lookup_paths lookup_paths
18
+ alias lookup_paths railsmdb_lookup_paths
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitar'
4
+ require 'zlib'
5
+ require 'zip'
6
+
7
+ module Railsmdb
8
+ # A utility class for extracting a specific file from a given archive.
9
+ # Currently only supports .tgz, .tar.gz, and .zip archives.
10
+ class Extractor
11
+ # Returns the appropriate extractor class for the given archive path.
12
+ #
13
+ # @param [ String ] archive_path the path to the archive file
14
+ #
15
+ # @return [ ZipExtractor | TarGzipExtractor ] the extractor instance
16
+ # corresponding to the given archive.
17
+ def self.for(archive_path)
18
+ case archive_path
19
+ when /\.(tgz|tar\.gz)$/ then TarGzipExtractor.new(archive_path)
20
+ when /\.zip$/ then ZipExtractor.new(archive_path)
21
+ else raise ArgumentError, "don't know how to extract #{archive_path}"
22
+ end
23
+ end
24
+
25
+ # The path to the archive.
26
+ attr_reader :archive_path
27
+
28
+ # Instantiates a new extractor with the given archive path.
29
+ #
30
+ # @param [ String ] archive_path the path to the archive.
31
+ def initialize(archive_path)
32
+ @archive_path = archive_path
33
+ end
34
+
35
+ # Extract the first file that matches the given pattern from the
36
+ # archive.
37
+ #
38
+ # @param [ Regexp ] pattern the pattern to use for finding the file
39
+ #
40
+ # @yield [ String, String ] report the name and contents of the first
41
+ # matching file. It will never yield more than once.
42
+ #
43
+ # @return [ String | nil ] returns the name of the matching file, or
44
+ # nil if no file matched.
45
+ #
46
+ # @raise [ NotImplementedError ] subclasses must override this method.
47
+ def extract(pattern)
48
+ raise NotImplementedError
49
+ end
50
+ end
51
+
52
+ # An extractor subclass for dealing with .zip files.
53
+ #
54
+ # @api private
55
+ class ZipExtractor < Extractor
56
+ # See Extractor#extract for documentation.
57
+ def extract(pattern)
58
+ Zip::File.open(archive_path).each do |entry|
59
+ if entry.name.match?(pattern)
60
+ yield entry.name, entry.get_input_stream.read.force_encoding('BINARY')
61
+ return entry.name
62
+ end
63
+ end
64
+
65
+ nil
66
+ end
67
+ end
68
+
69
+ # An extractor subclass for dealing with .tgz/.tar.gz files.
70
+ #
71
+ # @api private
72
+ class TarGzipExtractor < Extractor
73
+ # See Extractor#extract for documentation.
74
+ def extract(pattern)
75
+ reader.each_entry do |entry|
76
+ if entry.name.match?(pattern)
77
+ yield entry.name, entry.read.force_encoding('BINARY')
78
+ return entry.name
79
+ end
80
+ end
81
+
82
+ nil
83
+ end
84
+
85
+ # Returns a reader able to iterate over all the entries in the
86
+ # archive.
87
+ def reader
88
+ @reader ||= begin
89
+ gzip = Zlib::GzipReader.new(File.open(archive_path, 'rb'))
90
+ Minitar::Reader.new(gzip)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/named_base'
4
+
5
+ module Mongoid
6
+ module Generators
7
+ # Generator implementation for creating a new model.
8
+ class ModelGenerator < Rails::Generators::NamedBase
9
+ desc 'Creates a Mongoid model'
10
+ argument :attributes, type: :array, default: [], banner: 'field:type field:type'
11
+
12
+ check_class_collision
13
+
14
+ def self.base_root
15
+ File.expand_path('../..', __dir__)
16
+ end
17
+
18
+ class_option :timestamps, type: :boolean, default: true
19
+ class_option :parent, type: :string, desc: 'The parent class for the generated model'
20
+ class_option :collection, type: :string, desc: 'The collection for storing model\'s documents'
21
+
22
+ # Task for creating a new model file
23
+ def create_model_file
24
+ template 'model.rb.tt', File.join('app/models', class_path, "#{file_name}.rb")
25
+ end
26
+
27
+ hook_for :test_framework
28
+
29
+ private
30
+
31
+ def type_class_for(attribute)
32
+ case attribute.type
33
+ when :datetime then 'Time'
34
+ when :text then 'String'
35
+ when :boolean then 'Mongoid::Boolean'
36
+ else attribute.type.to_s.classify
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ <% module_namespacing do -%>
2
+ class <%= class_name %><%= " < #{options[:parent].classify}" if options[:parent] %>
3
+ <% unless options[:parent] -%>
4
+ include Mongoid::Document
5
+ <% end -%>
6
+ <% if options[:timestamps] -%>
7
+ include Mongoid::Timestamps
8
+ <% end -%>
9
+ <% if options[:collection] -%>
10
+ store_in collection: '<%= options[:collection] %>'
11
+ <% end -%>
12
+ <% attributes.reject(&:reference?).each do |attribute| -%>
13
+ field :<%= attribute.name %>, type: <%= type_class_for(attribute) %>
14
+ <% end -%>
15
+ <% attributes.select(&:reference?).each do |attribute| -%>
16
+ belongs_to :<%= attribute.name %>
17
+ <% end -%>
18
+ end
19
+ <% end -%>