Permalink
Browse files

initial commit (some of its working, just xfered from local branch of…

… mongomatic)
  • Loading branch information...
0 parents commit 8466bf7c32d0b08796e60c4037c8acdb976c67d0 @jrwest committed Aug 18, 2010
No changes.
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Ben Myles
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No changes.
@@ -0,0 +1,20 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "mongomatic-rails3"
+ gem.summary = %Q{Mongomatic support for rails 3}
+ gem.description = %Q{Mongomatic is a simple Ruby object mapper for Mongo}
+ gem.email = "jordanrw@gmail.com"
+ gem.homepage = "http://mongomatic.com/"
+ gem.authors = ["Jordan West"]
+ gem.files = ["lib/**/*.rb"]
+ gem.add_dependency "mongomatic", ">= 0.3.1"
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
@@ -0,0 +1 @@
+0.0.0
@@ -0,0 +1,22 @@
+require 'rails/generators/mongomatic_generator'
+module Mongomatic
+ module Generators
+ class ConfigGenerator < ::Rails::Generators::Base
+ desc "Create mongomatic configuration file (config/mongomatic.yml)"
+
+ argument :db, :type => :string, :optional => true
+
+ def self.source_root
+ @_source_root ||= File.expand_path("../templates", __FILE__)
+ end
+
+ def default
+ 'test'
+ end
+
+ def create_config_file
+ template 'mongomatic.yml', Rails.root.join('config', 'mongomatic.yml')
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+development: Mongo::Connection.new.db("<%= db || default %>")
+test: Mongo::Connection.new.db("<%= db || default %>")
+production: Mongo::Connection.new.db("<%= db || default %>")
@@ -0,0 +1,37 @@
+require 'rails/generators/mongomatic_generator'
+
+module Mongomatic
+ module Generators
+ class ModelGenerator < Base
+ desc "Creates a mongomatic model"
+ class_option :connection,
+ :type => :string,
+ :default => "",
+ :desc => "Connection String",
+ :banner => "Mongo::Connection.new.db('test')",
+ :aliases => ['-c']
+
+ class_option :expectations,
+ :desc => "Include Mongomatic::Expectations",
+ :default => false,
+ :aliases => ['-e'],
+ :type => :boolean
+
+ check_class_collision
+
+ def connection
+ options[:connection]
+ end
+
+ def expectations?
+ options[:expectations] == true
+ end
+
+ def create_model_file
+ template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
+ end
+
+ hook_for :test_framework
+ end
+ end
+end
@@ -0,0 +1,8 @@
+class <%= class_name %> < Mongomatic::Base
+ <%- if expectations? -%>
+ include Mongomatic::Expectations::Helper
+ <%- end -%>
+end
+<%- unless connection.blank? -%>
+<%= class_name %>.db = <%= connection %>
+<%- end -%>
@@ -0,0 +1,42 @@
+require "rails/generators/named_base"
+require "rails/generators/active_model"
+
+module Mongomatic
+ module Generators
+ class Base < ::Rails::Generators::NamedBase
+ def self.source_root
+ @_source_root ||= File.expand_path("../#{base_name}/#{generator_name}/templates", __FILE__)
+ end
+ end
+
+ class ActiveModel < ::Rails::Generators::ActiveModel
+ def self.all(klass)
+ "#{klass}.find"
+ end
+
+ def self.find(klass, params=nil)
+ (params.blank?) ? "#{klass}.find" : "#{klass}.find_one(BSON::ObjectID(#{params}))"
+ end
+
+ def self.build(klass, params=nil)
+ "#{klass}.new(#{params})"
+ end
+
+ def save
+ "#{name}.upsert"
+ end
+
+ def update_attributes(params=nil)
+ "#{name}.merge!(#{params})"
+ end
+
+ def errors
+ "#{name}.errors"
+ end
+
+ def destroy
+ "#{name}.remove"
+ end
+ end
+ end
+end
@@ -0,0 +1,9 @@
+require 'mongomatic'
+require 'rails/mongomatic/railtie'
+require 'rails/mongomatic/support'
+
+module Mongomatic
+ class Base
+ include Rails::Mongomatic::Support
+ end
+end
@@ -0,0 +1,18 @@
+require 'rails'
+require 'rails/mongomatic'
+
+module Rails
+ module Monogmatic
+ class Railtie < Rails::Railtie
+ config.generators.orm :mongomatic, :migrations => false
+
+ initializer "setup" do
+ config_file = Rails.root.join("config", "mongomatic.yml")
+ if config_file.file?
+ connection_str = YAML.load(ERB.new(config_file.read).result)[Rails.env]
+ ::Mongomatic.db = eval(connection_str)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,37 @@
+module Rails
+ module Mongomatic
+ module Support
+ def self.included(base)
+ base.extend ClassMethods
+ end
+
+ def new_record?
+ new?
+ end
+
+ def persisted?
+ !(new? || removed?)
+ end
+
+ def to_key
+ (new_record?) ? nil : [to_param]
+ end
+
+ def to_param
+ self['_id'].to_s
+ end
+
+ def merge!(hash)
+ return true unless hash
+ merge(hash)
+ update
+ end
+
+ module ClassMethods
+ def model_name
+ @_model_name ||= ActiveModel::Name.new(self)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,46 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{mongomatic-rails3}
+ s.version = "0.0.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jordan West"]
+ s.date = %q{2010-08-17}
+ s.description = %q{Mongomatic is a simple Ruby object mapper for Mongo}
+ s.email = %q{jordanrw@gmail.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ "lib/rails/generators/mongomatic/config/config_generator.rb",
+ "lib/rails/generators/mongomatic/model/model_generator.rb",
+ "lib/rails/generators/mongomatic/model/templates/model.rb",
+ "lib/rails/generators/mongomatic_generator.rb",
+ "lib/rails/mongomatic.rb",
+ "lib/rails/mongomatic/support.rb"
+ ]
+ s.homepage = %q{http://mongomatic.com/}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{Mongomatic support for rails 3}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<mongomatic>, [">= 0.3.1"])
+ else
+ s.add_dependency(%q<mongomatic>, [">= 0.3.1"])
+ end
+ else
+ s.add_dependency(%q<mongomatic>, [">= 0.3.1"])
+ end
+end
+
Binary file not shown.

0 comments on commit 8466bf7

Please sign in to comment.