mongorm 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 Derek Hammer
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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.
@@ -0,0 +1,70 @@
1
+ # Mongorm
2
+
3
+ Mongorm is a micro ODM for ruby. The difference between mongorm and
4
+ every other Mongo ODM is that mongorm strongly believes in plain Ruby
5
+ objects (PROs) and separation of domain logic (Models) from persistance
6
+ logic (Repositories). With Mongorm, you will never include anything
7
+ into your models.
8
+
9
+ ## Getting Started
10
+
11
+ If you want to start using Mongorm, do the following steps:
12
+
13
+ 1. Include the following two gems (will be one once origin releases 1.0)
14
+ `gem 'origin', :git => 'http://github.com/mongoid/origin.git'`
15
+ `gem 'mongorm'`
16
+ 2. In your application, set the Mongorm environment via
17
+ `Mongorm.environment = Rails.env` or `Mongorm.environment =
18
+ ENV["RACK_ENV"]`
19
+ 3. Create a repository for the model you want to persist. For example,
20
+ for User you'll need a UserRepository.
21
+ 4. In your repository, `include Mongorm::Repository`
22
+ 5. Done. Start using it and persisting to mongo.
23
+
24
+ [Example app](http://github.com/hammerdr/mongorm-rails-example)
25
+
26
+ ## Practices
27
+
28
+ Views -> Controllers -> **Repositories** -> Models
29
+
30
+ Controllers should talk to repositories
31
+ `UserRepository.new.find(params[:id])`
32
+ Models should be PROs
33
+ Repositories should be data access only.
34
+ Use services for manipulation of multiple repositories and models, if
35
+ needed.
36
+ Simple over Easy.
37
+ Do not load Rails unless you need it (Hint: Models do not need it!)
38
+
39
+ ## Background
40
+
41
+ This is a repository that is currently showing my attempt to create a
42
+ purely DataMapper-backed ORM that is as easy to use as Mongoid. It will
43
+ start with me persisting and pulling of a User object out of a mongo db.
44
+
45
+ Where I'm at now:
46
+
47
+ * This will autowire things, based on these conventions:
48
+
49
+ * DomainModal has a DomainModalRepository
50
+ * DomainModelRepository includes a Mongorm::Repository
51
+ * DomainModel has a hash-based constructor (do not like this but not
52
+ sure of a way around it)
53
+ * Needs a config/mongo.yml
54
+ * All 'fields' need to be `attr_accessible` in DomainModel
55
+ * DomainModel needs `id`
56
+
57
+ ## API
58
+
59
+ `#find(id)` where id is a string. It will return the found DomainModel
60
+ object. If not found, it will throw an error.
61
+
62
+ `#delete(object)`
63
+
64
+ `#delete_all` will delete all documents in the collection
65
+
66
+ `#save(object)` will create the object and set the id
67
+
68
+ `#where(origin_hash)` will search using origin query generation
69
+
70
+ `#collection` will give direct access to 10gen's Mongo Ruby Driver
@@ -0,0 +1,24 @@
1
+ require 'mongo'
2
+ require 'origin'
3
+ require_relative 'mongorm/repository'
4
+
5
+ module Mongorm
6
+ class << self
7
+ attr_accessor :environment
8
+ def register(repository)
9
+ klass = Kernel.const_get(repository.name.sub('Repository', ''))
10
+ repository.map_to(klass)
11
+ end
12
+
13
+ def connection
14
+ @connection ||= ::Mongo::Connection.new(config['host'], config['port']).
15
+ db(config['database'])
16
+ end
17
+
18
+ def config
19
+ config_file = File.join('config', 'mongo.yml')
20
+ raise Errors::ConfigurationError unless File.exists?(config_file)
21
+ YAML.load_file(config_file)[environment || "development"]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ require 'origin'
2
+
3
+ module Mongorm
4
+ class Criteria
5
+ include Origin::Queryable
6
+
7
+ def arguments
8
+ [selector, options]
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ module Mongorm::Errors
2
+ class ConfigurationError < StandardError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Mongorm::Errors
2
+ class DocumentNotFoundError < StandardError; end
3
+ end
@@ -0,0 +1,62 @@
1
+ require 'active_support'
2
+ require_relative 'criteria'
3
+ require_relative 'repository/class_methods'
4
+
5
+ module Mongorm
6
+ module Repository
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ Mongorm.register(self)
11
+ end
12
+
13
+ def collection_name
14
+ mapping.to_s.downcase
15
+ end
16
+
17
+ def fields
18
+ self.class.fields
19
+ end
20
+
21
+ def mapping
22
+ self.class.mapping
23
+ end
24
+
25
+ def connection
26
+ self.class.connection
27
+ end
28
+
29
+ def collection
30
+ connection[collection_name]
31
+ end
32
+
33
+ def save object
34
+ value = fields.inject({}) do |sum, field|
35
+ sum.merge(Hash[field, object.send(field)])
36
+ end
37
+ object.id = collection.insert(value)
38
+ end
39
+
40
+ def find id
41
+ hash = collection.find_one(:_id => id)
42
+ raise Mongorm::Errors::DocumentNotFoundError unless hash
43
+ hash = hash.keys.inject({}) { |sum, k| sum.merge(k.to_sym => hash[k]) }
44
+ object = mapping.new(hash)
45
+ object.id = hash[:_id]
46
+ object
47
+ end
48
+
49
+ def delete object
50
+ collection.remove(:_id => object.id)
51
+ end
52
+
53
+ def delete_all
54
+ collection.remove
55
+ end
56
+
57
+ def where(args={})
58
+ criteria = self.class.criteria.where(args)
59
+ collection.find(*criteria.arguments)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,30 @@
1
+ module Mongorm
2
+ module Repository
3
+ module ClassMethods
4
+ attr_reader :mapping
5
+ def map_to(class_name)
6
+ @mapping = class_name
7
+ end
8
+
9
+ def fields
10
+ return @fields if @fields
11
+ methods = mapping.instance_methods - Object.instance_methods
12
+ @fields = []
13
+ methods.each do |method|
14
+ if method =~ /[^=]$/
15
+ @fields << method if methods.include? "#{method}=".to_sym
16
+ end
17
+ end
18
+ @fields
19
+ end
20
+
21
+ def connection
22
+ Mongorm.connection
23
+ end
24
+
25
+ def criteria
26
+ Criteria.new({}, {}, :mongo)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module Mongorm
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongorm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Derek Hammer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-20 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70120788923840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70120788923840
25
+ - !ruby/object:Gem::Dependency
26
+ name: mongo
27
+ requirement: &70120788923360 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70120788923360
36
+ - !ruby/object:Gem::Dependency
37
+ name: bson_ext
38
+ requirement: &70120788922880 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.3'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70120788922880
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70120788922400 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.6'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70120788922400
58
+ description: The micro ODM for Mongo. It is a full featured, repository-based ODM
59
+ that doesnt get in the way.
60
+ email:
61
+ - derek.r.hammer@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - lib/mongorm/criteria.rb
67
+ - lib/mongorm/errors/configuration_error.rb
68
+ - lib/mongorm/errors/document_not_found_error.rb
69
+ - lib/mongorm/repository/class_methods.rb
70
+ - lib/mongorm/repository.rb
71
+ - lib/mongorm/version.rb
72
+ - lib/mongorm.rb
73
+ - LICENSE
74
+ - README.md
75
+ homepage: http://github.com/hammerdr/mongorm
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: 1.3.6
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.10
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Simple Mongo ODM following Data Mapper Pattern
99
+ test_files: []
100
+ has_rdoc: