mongomatic-rails3 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jordan West
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/README.rdoc ADDED
@@ -0,0 +1,56 @@
1
+ = Rails::Mongomatic
2
+
3
+ Rails 3 support for Mongomatic (http://www.mongomatic.com). Generators, url_for support, and more.
4
+
5
+ == Background
6
+
7
+ Mongomatic is a minimalist framework. It strives to do just enough atop an already great Mongo driver. Not developed in an ActiveRecord style, the Mongomatic core can be used in Rails 3 but certain helpers are not supported. This library adds support certain Rails 3 features. Although the original developers continue to use the core of Mongomatic only in their work, we have provided this library for people extremely accustomed to parts of the Rails framework who still want to use Mongomatic, for convenience. There is a chance certain parts of this library will be merged into Mongomatic core but there is an equally likely chance they will not.
8
+
9
+ == Installation
10
+
11
+ Add mongomatic-rails3 to your gemfile like this,
12
+
13
+ gem 'mongomatic-rails3', :require => 'rails/mongomatic'
14
+
15
+ See Config File below.
16
+
17
+ == Config. File
18
+
19
+ Mongomatic loads the database connection configuration file config/mongomatic.yml. In the spirit of Mongomatic the configuration file is, in our opinion, simpler than other OMs. This may change in the future depending on feedback. Simply define your configuration using Mongo::Connection. There cannot be any syntax errors and each connection must return an instance of Mongo::DB.
20
+
21
+ An example configuration file:
22
+ development: Mongo::Connection.new.db("test")
23
+ test: Mongo::Connection.new.db("test")
24
+ production: Mongo::Connection.new.db("test")
25
+
26
+ You can use ERB syntax inside of your file if pieces of connection options are shared between environments. So you can generate your config file instead of starting to type it by hand, mongomatic-rails3 provides a config generator. You will, however, need to make some changes to the connection options.
27
+
28
+ You can generate the config file by running,
29
+ rails g mongomatic:config [DB]
30
+
31
+ Run,
32
+ rails g mongomatic:config --help
33
+ for more information.
34
+
35
+ == Generating Models
36
+
37
+ Mongomatic fully implements the ActiveModel generator. This means that with mongmatic-rails3 installed running,
38
+ rails g model [NAME]
39
+
40
+ will generate a class inheriting from Mongomatic::Base. Mongomatic supports per-class connections. You can specify one when generating your model. See below for more information. You can also run
41
+
42
+ rails g model --help
43
+
44
+ Usage:
45
+ rails generate model NAME [field:type field:type] [options]
46
+
47
+ Mongomatic options:
48
+ -c, [--connection=Mongo::Connection.new.db('test')] # Connection String
49
+ -e, [--expectations] # Include Mongomatic::Expectations
50
+
51
+
52
+ == Other Notes
53
+
54
+ ==== Scaffolds
55
+
56
+ mongomatic-rails3 adds support for Scaffold generation, however, scaffold support is currently limited. This library does not *yet* support form_for, due to its hash-only data access syntax (you should instead use form_tag), which scaffolds generate in their views (*yet* will most likely mean a special form helper for Mongomatic). You can, however, still use scaffolds as a starting point.
@@ -0,0 +1,22 @@
1
+ require 'rails/generators/mongomatic_generator'
2
+ module Mongomatic
3
+ module Generators
4
+ class ConfigGenerator < ::Rails::Generators::Base
5
+ desc "Create mongomatic configuration file (config/mongomatic.yml)"
6
+
7
+ argument :db, :type => :string, :optional => true
8
+
9
+ def self.source_root
10
+ @_source_root ||= File.expand_path("../templates", __FILE__)
11
+ end
12
+
13
+ def default
14
+ 'test'
15
+ end
16
+
17
+ def create_config_file
18
+ template 'mongomatic.yml', Rails.root.join('config', 'mongomatic.yml')
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,37 @@
1
+ require 'rails/generators/mongomatic_generator'
2
+
3
+ module Mongomatic
4
+ module Generators
5
+ class ModelGenerator < Base
6
+ desc "Creates a mongomatic model"
7
+ class_option :connection,
8
+ :type => :string,
9
+ :default => "",
10
+ :desc => "Connection String",
11
+ :banner => "Mongo::Connection.new.db('test')",
12
+ :aliases => ['-c']
13
+
14
+ class_option :expectations,
15
+ :desc => "Include Mongomatic::Expectations",
16
+ :default => false,
17
+ :aliases => ['-e'],
18
+ :type => :boolean
19
+
20
+ check_class_collision
21
+
22
+ def connection
23
+ options[:connection]
24
+ end
25
+
26
+ def expectations?
27
+ options[:expectations] == true
28
+ end
29
+
30
+ def create_model_file
31
+ template "model.rb", File.join("app/models", class_path, "#{file_name}.rb")
32
+ end
33
+
34
+ hook_for :test_framework
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,8 @@
1
+ class <%= class_name %> < Mongomatic::Base
2
+ <%- if expectations? -%>
3
+ include Mongomatic::Expectations::Helper
4
+ <%- end -%>
5
+ end
6
+ <%- unless connection.blank? -%>
7
+ <%= class_name %>.db = <%= connection %>
8
+ <%- end -%>
@@ -0,0 +1,42 @@
1
+ require "rails/generators/named_base"
2
+ require "rails/generators/active_model"
3
+
4
+ module Mongomatic
5
+ module Generators
6
+ class Base < ::Rails::Generators::NamedBase
7
+ def self.source_root
8
+ @_source_root ||= File.expand_path("../#{base_name}/#{generator_name}/templates", __FILE__)
9
+ end
10
+ end
11
+
12
+ class ActiveModel < ::Rails::Generators::ActiveModel
13
+ def self.all(klass)
14
+ "#{klass}.find"
15
+ end
16
+
17
+ def self.find(klass, params=nil)
18
+ (params.blank?) ? "#{klass}.find" : "#{klass}.find_one(BSON::ObjectID(#{params}))"
19
+ end
20
+
21
+ def self.build(klass, params=nil)
22
+ "#{klass}.new(#{params})"
23
+ end
24
+
25
+ def save
26
+ "#{name}.upsert"
27
+ end
28
+
29
+ def update_attributes(params=nil)
30
+ "#{name}.merge!(#{params})"
31
+ end
32
+
33
+ def errors
34
+ "#{name}.errors"
35
+ end
36
+
37
+ def destroy
38
+ "#{name}.remove"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ require 'mongomatic'
2
+ require 'rails/mongomatic/railtie'
3
+ require 'rails/mongomatic/support'
4
+
5
+ module Mongomatic
6
+ class Base
7
+ include Rails::Mongomatic::Support
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails'
2
+ require 'rails/mongomatic'
3
+
4
+ module Rails
5
+ module Monogmatic
6
+ class Railtie < Rails::Railtie
7
+ config.generators.orm :mongomatic, :migrations => false
8
+
9
+ initializer "setup" do
10
+ config_file = Rails.root.join("config", "mongomatic.yml")
11
+ if config_file.file?
12
+ connection_str = YAML.load(ERB.new(config_file.read).result)[Rails.env]
13
+ ::Mongomatic.db = eval(connection_str)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,37 @@
1
+ module Rails
2
+ module Mongomatic
3
+ module Support
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ def new_record?
9
+ new?
10
+ end
11
+
12
+ def persisted?
13
+ !(new? || removed?)
14
+ end
15
+
16
+ def to_key
17
+ (new_record?) ? nil : [to_param]
18
+ end
19
+
20
+ def to_param
21
+ self['_id'].to_s
22
+ end
23
+
24
+ def merge!(hash)
25
+ return true unless hash
26
+ merge(hash)
27
+ update
28
+ end
29
+
30
+ module ClassMethods
31
+ def model_name
32
+ @_model_name ||= ActiveModel::Name.new(self)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongomatic-rails3
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Jordan West
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-17 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: mongomatic
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ - 3
31
+ - 1
32
+ version: 0.3.1
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Mongomatic is a simple Ruby object mapper for Mongo
36
+ email: jordanrw@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - lib/rails/generators/mongomatic/config/config_generator.rb
46
+ - lib/rails/generators/mongomatic/model/model_generator.rb
47
+ - lib/rails/generators/mongomatic/model/templates/model.rb
48
+ - lib/rails/generators/mongomatic_generator.rb
49
+ - lib/rails/mongomatic.rb
50
+ - lib/rails/mongomatic/railtie.rb
51
+ - lib/rails/mongomatic/support.rb
52
+ - LICENSE
53
+ - README.rdoc
54
+ has_rdoc: true
55
+ homepage: http://mongomatic.com/
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Mongomatic support for rails 3
86
+ test_files: []
87
+