mongify-mongoid 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+
20
+ vendor/ruby
data/.yardopts ADDED
@@ -0,0 +1,2 @@
1
+ -
2
+ ./CHANGELOG.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 1.0.0 / 28 MAY 2013
2
+
3
+ * Inital Release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongify-mongoid.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,19 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml|\.slim)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
13
+
14
+
15
+ # Turnip features and steps
16
+ watch(%r{^spec/acceptance/(.+)\.feature$})
17
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
18
+ end
19
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrew Kalek
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # Mongify::Mongoid
2
+
3
+ Mongoify-Mongoid generates valid Mongoid Models from the Mongify translation file.
4
+
5
+ Learn more about [Mongify](http://mongify.com/)
6
+
7
+ ## Installation
8
+
9
+ gem install mongify-mongoid
10
+
11
+ ## Usage
12
+
13
+ mongify_mongoid translation_file.rb [--output ~/output_dir]
14
+
15
+ *default output dir is the current_directory/models*
16
+
17
+ ## Contributing
18
+
19
+ 1. Fork it
20
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
21
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
22
+ 4. Push to the branch (`git push origin my-new-feature`)
23
+ 5. Create new Pull Request
24
+
25
+ ## About
26
+
27
+ This gem was made by:
28
+
29
+ Andrew Kalek from [Anlek Consulting](http://anlek.com)
30
+
31
+ > - Twitter: [@anlek](http://www.twitter.com/anlek)
32
+ > - Email: andrew.kalek@anlek[dot]com
33
+
34
+ Afolabi Badmos
35
+
36
+ ## License
37
+
38
+ Copyright (c) 2013 Andrew Kalek, Anlek Consulting
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining
41
+ a copy of this software and associated documentation files (the
42
+ "Software"), to deal in the Software without restriction, including
43
+ without limitation the rights to use, copy, modify, merge, publish,
44
+ distribute, sublicense, and/or sell copies of the Software, and to
45
+ permit persons to whom the Software is furnished to do so, subject to
46
+ the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
54
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
55
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
56
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
57
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc "Run rspec test"
7
+ task :test do
8
+ Rake::Task["test:rspec"].invoke
9
+ end
10
+
11
+ namespace :test do
12
+ RSpec::Core::RakeTask.new(:rspec) do |t|
13
+ t.rspec_opts = "--format d -c"
14
+ end
15
+ end
16
+
17
+ task :default => ['test']
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # mongify_mongoid helps output Mongoid
4
+ # Visit http://github.com/anlek/mongify_mongoid for more information.
5
+ #
6
+ # Author: Andrew Kalek
7
+ #
8
+
9
+ $:.unshift File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'lib')
10
+
11
+ require 'mongify/mongoid/cli'
12
+
13
+ begin
14
+ exit Mongify::Mongoid::CLI::Application.new(ARGV).execute!
15
+ rescue Mongify::Mongoid::Error => error
16
+ $stderr.puts "Error: #{error}"
17
+ exit MongifyMongoid::CLI::Application.execution_error_status
18
+ end
@@ -0,0 +1,16 @@
1
+ require "mongify"
2
+ require "mongify/mongoid/exceptions"
3
+ require "mongify/mongoid/model"
4
+ require "mongify/mongoid/generator"
5
+ require "mongify/mongoid/printer"
6
+ require "mongify/mongoid/version"
7
+ require 'mongify/mongoid/ui'
8
+
9
+
10
+ # Mongify
11
+ module Mongify
12
+ # Mongoid
13
+ module Mongoid
14
+
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ require 'mongify/mongoid'
2
+
3
+ require 'mongify/mongoid/cli/command/version'
4
+ require 'mongify/mongoid/cli/command/help'
5
+ require 'mongify/mongoid/cli/command/worker'
6
+
7
+
8
+ require 'mongify/mongoid/cli/options'
9
+ require 'mongify/mongoid/cli/application'
@@ -0,0 +1,60 @@
1
+ module Mongify
2
+ module Mongoid
3
+ # The Command Line Interface module
4
+ module CLI
5
+ #
6
+ # Represents an instance of a Mongify Mongoid application.
7
+ # This is the entry point for all invocations of Mongify from the
8
+ # command line.
9
+ #
10
+ class Application
11
+
12
+ # Successful execution exit code
13
+ STATUS_SUCCESS = 0
14
+ # Failed execution exit code
15
+ STATUS_ERROR = 1
16
+
17
+ def initialize(arguments=[], stdin=$stdin, stdout=$stdout)
18
+ arguments = ['-h'] if arguments.empty?
19
+ @options = Options.new(arguments)
20
+ @status = STATUS_SUCCESS
21
+ end
22
+
23
+ # Runs the application
24
+ def execute!
25
+ begin
26
+ cmd = @options.parse
27
+ return cmd.execute(self)
28
+ rescue Error => error
29
+ $stderr.puts "ERROR: \n#{error}"
30
+ $stderr.puts "\nIf this is an issue that you can not figure out, feel free to submit an issue report at:"
31
+ $stderr.puts "https://github.com/anlek/mongify-mongoid/issues"
32
+ report_error
33
+ rescue Exception => error
34
+ $stderr.puts "UNKNOWN ERROR: \n#{error}"
35
+ $stderr.puts "\nWe are not sure what happen but feel free to submit this issue report at:"
36
+ $stderr.puts "https://github.com/anlek/mongify-mongoid/issues"
37
+ $stderr.puts ""
38
+ report_error
39
+ raise error
40
+ end
41
+ end
42
+
43
+ # Sends output to the UI
44
+ def output(message)
45
+ UI.puts(message)
46
+ end
47
+
48
+ # Sets status code as successful
49
+ def report_success
50
+ @status = STATUS_SUCCESS
51
+ end
52
+
53
+ # Sets status code as failure (or error)
54
+ def report_error
55
+ @status = STATUS_ERROR
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,22 @@
1
+ module Mongify
2
+ module Mongoid
3
+ module CLI
4
+ # Command
5
+ module Command
6
+ #
7
+ # A command to display usage information for this application.
8
+ #
9
+ class Help
10
+ def initialize(parser)
11
+ @parser = parser
12
+ end
13
+ #Executes the help command
14
+ def execute(view)
15
+ view.output(@parser.to_s)
16
+ view.report_success
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module Mongify
2
+ module Mongoid
3
+ module CLI
4
+ module Command
5
+ #
6
+ # A command to report the application's current version number.
7
+ #
8
+ class Version
9
+ def initialize(progname)
10
+ @progname = progname
11
+ end
12
+ #Executes version command
13
+ def execute(view)
14
+ view.output("#{@progname} #{Mongify::Mongoid::VERSION}\n")
15
+ view.report_success
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,51 @@
1
+ module Mongify
2
+ module Mongoid
3
+ module CLI
4
+ module Command
5
+ #
6
+ # A command to run the different commands in the application (related to Mongifying).
7
+ #
8
+ class Worker
9
+ attr_accessor :view
10
+
11
+
12
+ def initialize(translation_file=nil, output_dir=nil, options={})
13
+ @translation_file = translation_file
14
+ @output_dir = output_dir
15
+ @options = options
16
+ end
17
+
18
+ #Executes the worked based on a given command
19
+ def execute(view)
20
+ self.view = view
21
+
22
+ raise TranslationFileNotFound, "Translation file is required" unless @translation_file
23
+ raise TranslationFileNotFound, "Unable to find Translation File #{@translation_file}" unless File.exists?(@translation_file)
24
+
25
+ raise OverwritingFolder, "Output folder (#{output_folder}) already exists, for your safety we can't continue, pass -f force an overwrite" if File.exists?(output_folder) && !@options[:overwrite]
26
+
27
+ unless File.directory?(output_folder)
28
+ FileUtils.mkdir_p(output_folder)
29
+ end
30
+
31
+ generator = Mongify::Mongoid::Generator.new(@translation_file, output_folder)
32
+ generator.process
33
+ view.output("\nSuccessfully processed #{generator.models.count} models")
34
+ view.output("You can find your files in #{output_folder}")
35
+ view.output("\nThank you for using Mongify and Mongify-Mongoid!")
36
+ view.output("If you have any issues, please feel free to report them at:\nhttps://github.com/anlek/mongify-mongoid/issues")
37
+ view.output("")
38
+ view.report_success
39
+ end
40
+
41
+ #Folder location of the output
42
+ def output_folder
43
+ @output_dir = File.join(Dir.pwd, "models") if @output_dir.nil?
44
+ @output_dir
45
+ end
46
+
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,87 @@
1
+ require 'optparse'
2
+ module Mongify
3
+ module Mongoid
4
+ module CLI
5
+ #
6
+ # Used to parse the options for an application
7
+ #
8
+ class Options
9
+ def initialize(argv)
10
+ @parsed = false
11
+ @argv = argv
12
+ @parser = OptionParser.new
13
+ @options = {}
14
+ set_options
15
+ parse_options
16
+ end
17
+
18
+ # Banner for help output
19
+ def banner
20
+ progname = @parser.program_name
21
+ return <<EOB
22
+ Usage: #{progname} translation_file.rb [--output ~/output_dir]
23
+
24
+ Examples:
25
+ #{progname} database_translation.rb
26
+ #{progname} database_translation.rb -O ~/output_dir
27
+
28
+ See http://github.com/anlek/mongify for more details
29
+
30
+ EOB
31
+ end
32
+
33
+ # Sets the options for CLI
34
+ # Also used for help output
35
+ def set_options
36
+ @parser.banner = banner
37
+ @parser.separator "Common options:"
38
+ @parser.on("-h", "--help", "Show this message") do
39
+ @command_class = Command::Help
40
+ end
41
+ @parser.on("-v", "--version", "Show version") do
42
+ @command_class = Command::Version
43
+ end
44
+ @parser.on("-O", "--output DIR", "Output Directory") do |dir|
45
+ @output_dir = dir
46
+ end
47
+ @parser.on("-F", "--force", "Force overwrite of Output Directory") do
48
+ @options[:overwrite] = true
49
+ end
50
+ end
51
+
52
+ # Parses CLI passed attributes and figures out what command user is trying to run
53
+ def parse
54
+ case
55
+ when @command_class == Command::Help
56
+ Command::Help.new(@parser)
57
+ when @command_class == Command::Version
58
+ Command::Version.new(@parser.program_name)
59
+ else
60
+ Command::Worker.new(translation_file, output_dir, @options)
61
+ end
62
+ end
63
+
64
+ #######
65
+ private
66
+ #######
67
+
68
+ # Returns the translation_file or nil
69
+ def translation_file(argv=@argv)
70
+ argv[0] if argv.length >= 1
71
+ end
72
+
73
+ # Returns the config file
74
+ def output_dir(argv=@argv)
75
+ @output_dir if @output_dir && File.exist?(@output_dir) && File.directory?(@output_dir)
76
+ end
77
+
78
+ # option parser, ensuring parse_options is only called once
79
+ def parse_options
80
+ @parser.parse!(@argv)
81
+ rescue OptionParser::InvalidOption => er
82
+ raise Mongify::InvalidOption, er.message, er.backtrace
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end