mongo_thing 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -5
- data/Gemfile.lock +36 -0
- data/Rakefile +1 -3
- data/VERSION +1 -1
- data/lib/mongo_thing/document.rb +75 -34
- data/lib/mongo_thing/nested_ostruct.rb +27 -6
- data/lib/mongo_thing/railtie.rb +49 -43
- data/lib/mongo_thing/railties/database.rake +16 -1
- data/lib/mongo_thing/railties/document.rb +23 -0
- data/lib/mongo_thing.rb +6 -2
- data/mongo_thing.gemspec +51 -29
- data/spec/config/{mongoid.yml → mongo.yml} +0 -0
- data/spec/integration/mongo_thing/document_spec.rb +35 -15
- data/spec/integration/mongo_thing/nested_ostruct_spec.rb +23 -0
- data/spec/integration/mongo_thing_spec.rb +2 -1
- data/spec/spec_helper.rb +2 -2
- metadata +137 -15
- data/.bundle/config +0 -3
- data/.gitignore +0 -2
- data/lib/mongo_thing/named_scope.rb +0 -37
data/Gemfile
CHANGED
@@ -2,13 +2,13 @@
|
|
2
2
|
# Use `bundle exec rake` in order to run the specs using the bundle
|
3
3
|
source "http://rubygems.org"
|
4
4
|
|
5
|
-
gem "mongo"
|
6
|
-
gem "bson"
|
7
|
-
gem "bson_ext"
|
8
|
-
gem "activesupport", "3
|
5
|
+
gem "mongo"
|
6
|
+
gem "bson"
|
7
|
+
gem "bson_ext"
|
8
|
+
gem "activesupport", "~> 3"
|
9
|
+
gem "i18n"
|
9
10
|
|
10
11
|
group :development do
|
11
|
-
gem "rake"
|
12
12
|
gem "ruby-debug"
|
13
13
|
gem "rspec", "1.3.0"
|
14
14
|
gem "jeweler"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (3.0.6)
|
5
|
+
bson (1.3.0)
|
6
|
+
bson_ext (1.3.0)
|
7
|
+
columnize (0.3.2)
|
8
|
+
git (1.2.5)
|
9
|
+
i18n (0.5.0)
|
10
|
+
jeweler (1.5.2)
|
11
|
+
bundler (~> 1.0.0)
|
12
|
+
git (>= 1.2.5)
|
13
|
+
rake
|
14
|
+
linecache (0.43)
|
15
|
+
mongo (1.3.0)
|
16
|
+
bson (>= 1.3.0)
|
17
|
+
rake (0.8.7)
|
18
|
+
rspec (1.3.0)
|
19
|
+
ruby-debug (0.10.4)
|
20
|
+
columnize (>= 0.1)
|
21
|
+
ruby-debug-base (~> 0.10.4.0)
|
22
|
+
ruby-debug-base (0.10.4)
|
23
|
+
linecache (>= 0.3)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
activesupport (~> 3)
|
30
|
+
bson
|
31
|
+
bson_ext
|
32
|
+
i18n
|
33
|
+
jeweler
|
34
|
+
mongo
|
35
|
+
rspec (= 1.3.0)
|
36
|
+
ruby-debug
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/lib/mongo_thing/document.rb
CHANGED
@@ -1,9 +1,20 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module MongoThing #:nodoc:
|
3
3
|
module Document
|
4
|
+
class << self
|
5
|
+
attr_accessor :framework_modules
|
6
|
+
end
|
7
|
+
self.framework_modules = []
|
8
|
+
|
4
9
|
extend ActiveSupport::Concern
|
5
10
|
|
6
11
|
included do
|
12
|
+
# Subclass NestedOpenStruct for each document class
|
13
|
+
# so that the methods are created on the newly created class,
|
14
|
+
# not on NestedOpenStruct
|
15
|
+
class Attributes < NestedOpenStruct
|
16
|
+
end
|
17
|
+
|
7
18
|
class << self
|
8
19
|
attr_accessor :properties
|
9
20
|
def properties(*set)
|
@@ -16,7 +27,11 @@ module MongoThing #:nodoc:
|
|
16
27
|
end
|
17
28
|
self.properties = []
|
18
29
|
|
19
|
-
|
30
|
+
# This is to allow framework-specific inclusions
|
31
|
+
# TODO don't know if it's really worth it to keep these modules separate
|
32
|
+
MongoThing::Document.framework_modules.each{|i| include i}
|
33
|
+
|
34
|
+
delegate :db, :collection, :properties, :to => "self.class"
|
20
35
|
end
|
21
36
|
|
22
37
|
module ClassMethods
|
@@ -37,11 +52,21 @@ module MongoThing #:nodoc:
|
|
37
52
|
Cursor.new(self, mongo_cursor).to_a
|
38
53
|
end
|
39
54
|
|
55
|
+
alias :all :find
|
56
|
+
|
40
57
|
def [](doc_id)
|
41
58
|
find_one(doc_id)
|
42
59
|
end
|
43
60
|
|
44
61
|
def find_one(spec_or_object_id=nil, opts={})
|
62
|
+
# Why do it this way?
|
63
|
+
# Abstraction is breaking down...
|
64
|
+
#
|
65
|
+
# We want to just be able to to pass ID strings,
|
66
|
+
# but mongo driver requires hash or BSON::ObjectID
|
67
|
+
if spec_or_object_id.is_a? String
|
68
|
+
spec_or_object_id = BSON::ObjectId.from_string(spec_or_object_id)
|
69
|
+
end
|
45
70
|
self.new(collection.find_one(spec_or_object_id, opts))
|
46
71
|
end
|
47
72
|
|
@@ -54,6 +79,10 @@ module MongoThing #:nodoc:
|
|
54
79
|
def remove(selector={}, opts={})
|
55
80
|
collection.remove(selector, opts)
|
56
81
|
end
|
82
|
+
|
83
|
+
def drop
|
84
|
+
collection.drop
|
85
|
+
end
|
57
86
|
end
|
58
87
|
|
59
88
|
module InstanceMethods
|
@@ -67,7 +96,7 @@ module MongoThing #:nodoc:
|
|
67
96
|
raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
|
68
97
|
end
|
69
98
|
new_attribute(mname.chomp('='))
|
70
|
-
|
99
|
+
@attributes.send(mid, *args)
|
71
100
|
else
|
72
101
|
raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
|
73
102
|
end
|
@@ -78,11 +107,34 @@ module MongoThing #:nodoc:
|
|
78
107
|
unless self.respond_to?(name)
|
79
108
|
class << self; self; end.class_eval do
|
80
109
|
define_method(name) { @attributes.send(name) }
|
81
|
-
define_method("#{name}=") { |x| @attributes.send("#{name}="
|
110
|
+
define_method("#{name}=") { |x| @attributes.send("#{name}=", x) }
|
82
111
|
end
|
83
112
|
end
|
84
113
|
name
|
85
114
|
end
|
115
|
+
|
116
|
+
# Instantiate a new +Document+, setting the Document's attributes if
|
117
|
+
# given. If no attributes are provided, they will be initialized with
|
118
|
+
# an empty +Hash+.
|
119
|
+
#
|
120
|
+
# If a primary key is defined, the document's id will be set to that key,
|
121
|
+
# otherwise it will be set to a fresh +BSON::ObjectID+ string.
|
122
|
+
#
|
123
|
+
# Options:
|
124
|
+
#
|
125
|
+
# attrs: The attributes +Hash+ to set up the document with.
|
126
|
+
def initialize(attrs = {})
|
127
|
+
@attributes = Attributes.new
|
128
|
+
self.attributes = attrs
|
129
|
+
self
|
130
|
+
end
|
131
|
+
|
132
|
+
def attributes=(attrs={})
|
133
|
+
attrs ||= {}
|
134
|
+
attrs.each do |k,v|
|
135
|
+
self.send("#{k}=", v)
|
136
|
+
end
|
137
|
+
end
|
86
138
|
|
87
139
|
# Performs equality checking on the document ids. For more robust
|
88
140
|
# equality checking please override this method.
|
@@ -106,37 +158,9 @@ module MongoThing #:nodoc:
|
|
106
158
|
self.class.instantiate(@attributes.except("_id").except("versions").dup, true)
|
107
159
|
end
|
108
160
|
|
109
|
-
|
110
|
-
# Instantiate a new +Document+, setting the Document's attributes if
|
111
|
-
# given. If no attributes are provided, they will be initialized with
|
112
|
-
# an empty +Hash+.
|
113
|
-
#
|
114
|
-
# If a primary key is defined, the document's id will be set to that key,
|
115
|
-
# otherwise it will be set to a fresh +BSON::ObjectID+ string.
|
116
|
-
#
|
117
|
-
# Options:
|
118
|
-
#
|
119
|
-
# attrs: The attributes +Hash+ to set up the document with.
|
120
|
-
def initialize(attrs = {})
|
121
|
-
@attributes = NestedOpenStruct.new
|
122
|
-
self.attributes = attrs
|
123
|
-
@new_record = true if id.nil?
|
124
|
-
end
|
125
|
-
|
126
|
-
def attributes=(attrs={})
|
127
|
-
attrs.each do |k,v|
|
128
|
-
self.send("#{k}=", v)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
161
|
# Returns the class name plus its attributes.
|
133
162
|
def inspect
|
134
|
-
|
135
|
-
if Mongoid.allow_dynamic_fields
|
136
|
-
dynamic_keys = @attributes.keys - fields.keys - ["_id", "_type"]
|
137
|
-
attrs += dynamic_keys.map { |name| "#{name}: #{@attributes[name].inspect}" }
|
138
|
-
end
|
139
|
-
"#<#{self.class.name} _id: #{id}, #{attrs * ', '}>"
|
163
|
+
self.class.name + ": " + to_hash.inspect
|
140
164
|
end
|
141
165
|
|
142
166
|
# Reloads the +Document+ attributes from the database.
|
@@ -155,7 +179,11 @@ module MongoThing #:nodoc:
|
|
155
179
|
|
156
180
|
# Returns the id of the Document, used in Rails compatibility.
|
157
181
|
def to_param
|
158
|
-
id
|
182
|
+
id.to_s
|
183
|
+
end
|
184
|
+
|
185
|
+
def persisted?
|
186
|
+
true
|
159
187
|
end
|
160
188
|
|
161
189
|
def id
|
@@ -166,8 +194,21 @@ module MongoThing #:nodoc:
|
|
166
194
|
@attributes._id = new_id
|
167
195
|
end
|
168
196
|
|
197
|
+
# Needed for Rails / form helper
|
198
|
+
def to_key
|
199
|
+
[id.to_s]
|
200
|
+
end
|
201
|
+
|
202
|
+
def new_record?
|
203
|
+
id.nil?
|
204
|
+
end
|
205
|
+
|
206
|
+
def persisted?
|
207
|
+
!new_record?
|
208
|
+
end
|
209
|
+
|
169
210
|
def save
|
170
|
-
@attributes._id = BSON::
|
211
|
+
@attributes._id = BSON::ObjectId.new if @attributes._id.nil?
|
171
212
|
collection.save(self.to_hash)
|
172
213
|
self
|
173
214
|
end
|
@@ -1,15 +1,14 @@
|
|
1
1
|
# NestedOpenStruct works like OpenStruct except that all hash values are converted
|
2
|
-
# to NestedOpenStruct
|
2
|
+
# to NestedOpenStruct *and* all methods are added to the class itself,
|
3
|
+
# *not* the object's singleton class
|
3
4
|
|
4
5
|
module MongoThing
|
5
6
|
class NestedOpenStruct < OpenStruct
|
6
7
|
def initialize(hash=nil)
|
7
8
|
@table = {}
|
8
|
-
@nested_open_structs = []
|
9
9
|
if hash
|
10
10
|
for k,v in hash
|
11
|
-
v =
|
12
|
-
v = v.collect{|nv| nv.is_a?(Hash) ? NestedOpenStruct.new(nv) : nv} if v.is_a?(Array)
|
11
|
+
v = convert_value_to_nested_open_struct(v)
|
13
12
|
@table[k.to_sym] = v
|
14
13
|
new_ostruct_member(k)
|
15
14
|
end
|
@@ -18,16 +17,38 @@ module MongoThing
|
|
18
17
|
|
19
18
|
def method_missing(mid, *args) # :nodoc:
|
20
19
|
mname = mid.id2name
|
21
|
-
if mname[-1..-1] == "=" && args[0]
|
22
|
-
args[0] =
|
20
|
+
if mname[-1..-1] == "=" && args[0]
|
21
|
+
args[0] = convert_value_to_nested_open_struct(args[0])
|
23
22
|
end
|
24
23
|
super
|
25
24
|
end
|
26
25
|
|
26
|
+
def convert_value_to_nested_open_struct(value)
|
27
|
+
case value
|
28
|
+
when Hash
|
29
|
+
NestedOpenStruct.new(value)
|
30
|
+
when Array
|
31
|
+
value.collect{|nv| nv.is_a?(Hash) ? NestedOpenStruct.new(nv) : nv}
|
32
|
+
else
|
33
|
+
value
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
27
37
|
def to_hash
|
28
38
|
marshal_dump
|
29
39
|
end
|
30
40
|
|
41
|
+
def new_ostruct_member(name)
|
42
|
+
name = name.to_sym
|
43
|
+
unless self.respond_to?(name)
|
44
|
+
self.class.class_eval do
|
45
|
+
define_method(name) { @table[name] }
|
46
|
+
define_method("#{name}=") { |x| modifiable[name] = convert_value_to_nested_open_struct(x) }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
name
|
50
|
+
end
|
51
|
+
|
31
52
|
def marshal_dump
|
32
53
|
@table.collect do |k,v|
|
33
54
|
n = if v.is_a?(NestedOpenStruct)
|
data/lib/mongo_thing/railtie.rb
CHANGED
@@ -1,53 +1,59 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class Railtie < Rails::Railtie #:nodoc:
|
1
|
+
require 'mongo_thing'
|
2
|
+
require 'rails'
|
4
3
|
|
5
|
-
# do we want a custom log subscriber for mongoid?
|
6
|
-
# log_subscriber :mongoid, ::Mongoid::Railties::LogSubscriber.new
|
7
4
|
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
module MongoThing #:nodoc:
|
6
|
+
class Railtie < Rails::Railtie #:nodoc:
|
7
|
+
|
8
|
+
def self.config_file
|
9
|
+
Rails.root.join("config", "mongo.yml")
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
12
|
+
rake_tasks do
|
13
|
+
load "mongo_thing/railties/database.rake"
|
14
|
+
end
|
15
|
+
|
16
|
+
# Initialize MongoThing. This will look for a mongo.yml in the config
|
17
|
+
# directory and configure mongoid appropriately.
|
18
|
+
#
|
19
|
+
# Example mongo.yml:
|
20
|
+
#
|
21
|
+
# defaults: &defaults
|
22
|
+
# host: localhost
|
23
|
+
# slaves:
|
24
|
+
# # - host: localhost
|
25
|
+
# # port: 27018
|
26
|
+
# # - host: localhost
|
27
|
+
# # port: 27019
|
28
|
+
# allow_dynamic_fields: false
|
29
|
+
# parameterize_keys: false
|
30
|
+
# persist_in_safe_mode: false
|
31
|
+
#
|
32
|
+
# development:
|
33
|
+
# <<: *defaults
|
34
|
+
# database: mongo
|
35
|
+
initializer "setup database" do
|
36
|
+
if Railtie.config_file.file?
|
37
|
+
settings = YAML.load(ERB.new(Railtie.config_file.read).result)[Rails.env.to_sym]
|
38
|
+
::MongoThing.connection = settings.with_indifferent_access if settings.present?
|
37
39
|
end
|
40
|
+
end
|
38
41
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
puts "\nMongoid config not found. Create a config file at: config/mongoid.yml"
|
46
|
-
puts "to generate one run: script/rails generate mongoid:config\n\n"
|
47
|
-
end
|
42
|
+
initializer "verify that mongo is configured" do
|
43
|
+
config.after_initialize do
|
44
|
+
if ::MongoThing.db.nil?
|
45
|
+
puts "Could not connect to mongodb."
|
46
|
+
unless Railtie.config_file.file?
|
47
|
+
puts "Create a config file at config/mongo.yml"
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
|
+
|
53
|
+
initializer "require rails files" do
|
54
|
+
require 'mongo_thing/railties/document'
|
55
|
+
end
|
56
|
+
|
57
|
+
|
52
58
|
end
|
53
|
-
end
|
59
|
+
end
|
@@ -2,7 +2,7 @@ namespace :db do
|
|
2
2
|
|
3
3
|
desc 'Drops all the collections for the database for the current Rails.env'
|
4
4
|
task :drop => :environment do
|
5
|
-
|
5
|
+
MongoThing.drop_non_system_collections
|
6
6
|
end
|
7
7
|
|
8
8
|
desc 'Load the seed data from db/seeds.rb'
|
@@ -30,6 +30,21 @@ namespace :db do
|
|
30
30
|
# noop
|
31
31
|
end
|
32
32
|
end
|
33
|
+
|
34
|
+
namespace :test do
|
35
|
+
task :prepare do
|
36
|
+
# TODO refactor this... need to figure it out
|
37
|
+
begin
|
38
|
+
if Rails::MongoThing::Railtie.config_file.file?
|
39
|
+
settings = YAML.load(ERB.new(Rails::MongoThing::Railtie.config_file.read).result)[Rails.env]
|
40
|
+
MongoThing.connection = settings.with_indifferent_access if settings.present?
|
41
|
+
MongoThing.drop_non_system_collections
|
42
|
+
end
|
43
|
+
rescue e
|
44
|
+
puts e
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
33
48
|
|
34
49
|
########
|
35
50
|
# TODO: lots more useful db tasks can be added here. stuff like copyDatabase, etc
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MongoThing #:nodoc:
|
2
|
+
module Document
|
3
|
+
module Rails
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
included do
|
6
|
+
include ActiveModel::Naming
|
7
|
+
include ActiveModel::Serialization
|
8
|
+
include ActiveModel::Serializers::JSON
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def model_name
|
13
|
+
self.name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module InstanceMethods
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
MongoThing::Document.framework_modules << MongoThing::Document::Rails
|
data/lib/mongo_thing.rb
CHANGED
@@ -41,10 +41,10 @@ module MongoThing #:nodoc
|
|
41
41
|
case conn
|
42
42
|
when Hash
|
43
43
|
@connection = Mongo::Connection.new(conn.delete(:host), conn.delete(:port), conn)
|
44
|
+
self.db = conn if conn[:name]
|
44
45
|
when Mongo::Connection
|
45
46
|
@connection = conn
|
46
47
|
end
|
47
|
-
self.db = conn if conn[:name]
|
48
48
|
end
|
49
49
|
|
50
50
|
def db=(new_db)
|
@@ -58,7 +58,11 @@ module MongoThing #:nodoc
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
+
def drop_non_system_collections
|
62
|
+
db.collections.each{|col| col.drop unless col.name =~ /^system\./ } if db
|
63
|
+
end
|
61
64
|
end
|
62
65
|
|
63
|
-
|
64
66
|
end
|
67
|
+
|
68
|
+
require 'mongo_thing/railtie' if defined? ::Rails::Railtie
|
data/mongo_thing.gemspec
CHANGED
@@ -1,62 +1,84 @@
|
|
1
1
|
# Generated by jeweler
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongo_thing}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Daniel Higginbotham"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2011-04-25}
|
13
13
|
s.email = %q{daniel@flyingmachinestudios.com}
|
14
14
|
s.files = [
|
15
|
-
"
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
"spec/spec_helper.rb"
|
15
|
+
"Gemfile",
|
16
|
+
"Gemfile.lock",
|
17
|
+
"MIT_LICENSE",
|
18
|
+
"Rakefile",
|
19
|
+
"VERSION",
|
20
|
+
"lib/mongo_thing.rb",
|
21
|
+
"lib/mongo_thing/cursor.rb",
|
22
|
+
"lib/mongo_thing/document.rb",
|
23
|
+
"lib/mongo_thing/nested_ostruct.rb",
|
24
|
+
"lib/mongo_thing/railtie.rb",
|
25
|
+
"lib/mongo_thing/railties/database.rake",
|
26
|
+
"lib/mongo_thing/railties/document.rb",
|
27
|
+
"mongo_thing.gemspec",
|
28
|
+
"spec/config/mongo.yml",
|
29
|
+
"spec/integration/mongo_thing/document_spec.rb",
|
30
|
+
"spec/integration/mongo_thing/nested_ostruct_spec.rb",
|
31
|
+
"spec/integration/mongo_thing_spec.rb",
|
32
|
+
"spec/spec.opts",
|
33
|
+
"spec/spec_helper.rb"
|
35
34
|
]
|
36
35
|
s.homepage = %q{}
|
37
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
38
36
|
s.require_paths = ["lib"]
|
39
|
-
s.rubygems_version = %q{1.3.
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
40
38
|
s.summary = %q{ODM framework for MongoDB}
|
41
39
|
s.test_files = [
|
42
40
|
"spec/integration/mongo_thing/document_spec.rb",
|
43
|
-
|
44
|
-
|
45
|
-
|
41
|
+
"spec/integration/mongo_thing/nested_ostruct_spec.rb",
|
42
|
+
"spec/integration/mongo_thing_spec.rb",
|
43
|
+
"spec/spec_helper.rb"
|
46
44
|
]
|
47
45
|
|
48
46
|
if s.respond_to? :specification_version then
|
49
47
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
48
|
s.specification_version = 3
|
51
49
|
|
52
|
-
if Gem::Version.new(Gem::
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_runtime_dependency(%q<mongo>, [">= 0"])
|
52
|
+
s.add_runtime_dependency(%q<bson>, [">= 0"])
|
53
|
+
s.add_runtime_dependency(%q<bson_ext>, [">= 0"])
|
54
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3"])
|
55
|
+
s.add_runtime_dependency(%q<i18n>, [">= 0"])
|
56
|
+
s.add_development_dependency(%q<ruby-debug>, [">= 0"])
|
57
|
+
s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
|
58
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
53
59
|
s.add_runtime_dependency(%q<mongo>, ["~> 1.0.1"])
|
54
60
|
s.add_runtime_dependency(%q<bson>, ["~> 1.0.1"])
|
55
61
|
else
|
62
|
+
s.add_dependency(%q<mongo>, [">= 0"])
|
63
|
+
s.add_dependency(%q<bson>, [">= 0"])
|
64
|
+
s.add_dependency(%q<bson_ext>, [">= 0"])
|
65
|
+
s.add_dependency(%q<activesupport>, ["~> 3"])
|
66
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
67
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
68
|
+
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
69
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
56
70
|
s.add_dependency(%q<mongo>, ["~> 1.0.1"])
|
57
71
|
s.add_dependency(%q<bson>, ["~> 1.0.1"])
|
58
72
|
end
|
59
73
|
else
|
74
|
+
s.add_dependency(%q<mongo>, [">= 0"])
|
75
|
+
s.add_dependency(%q<bson>, [">= 0"])
|
76
|
+
s.add_dependency(%q<bson_ext>, [">= 0"])
|
77
|
+
s.add_dependency(%q<activesupport>, ["~> 3"])
|
78
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
79
|
+
s.add_dependency(%q<ruby-debug>, [">= 0"])
|
80
|
+
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
81
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
60
82
|
s.add_dependency(%q<mongo>, ["~> 1.0.1"])
|
61
83
|
s.add_dependency(%q<bson>, ["~> 1.0.1"])
|
62
84
|
end
|
File without changes
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
class Person
|
4
4
|
include MongoThing::Document
|
5
|
-
|
5
|
+
properties :name, :address, :friends
|
6
6
|
end
|
7
7
|
|
8
8
|
|
@@ -34,20 +34,6 @@ describe MongoThing::Document do
|
|
34
34
|
}.should raise_error(NoMethodError)
|
35
35
|
end
|
36
36
|
|
37
|
-
it "should return a hash of attributes" do
|
38
|
-
person_hash = {
|
39
|
-
:name => "Tom",
|
40
|
-
:address => {:street => "Tomahawk", :city => "Placeville"},
|
41
|
-
:friends => [
|
42
|
-
{:name => "John", :address => {:street => "Pleasant", :city => "Cityville"}},
|
43
|
-
{:name => "Bill", :address => {:street => "Prospect", :city => "Townville"}}
|
44
|
-
]
|
45
|
-
}
|
46
|
-
|
47
|
-
p = Person.new(person_hash)
|
48
|
-
p.to_hash.should == person_hash
|
49
|
-
end
|
50
|
-
|
51
37
|
it "should set properties using properties method" do
|
52
38
|
class SetProperties
|
53
39
|
include MongoThing::Document
|
@@ -56,6 +42,30 @@ describe MongoThing::Document do
|
|
56
42
|
|
57
43
|
lambda{SetProperties.new(:one => 1, :two => 2, :three => 3)}.should_not raise_error
|
58
44
|
end
|
45
|
+
|
46
|
+
describe "complex attributes" do
|
47
|
+
before(:each) do
|
48
|
+
@person_hash = {
|
49
|
+
:name => "Tom",
|
50
|
+
:address => {:street => "Tomahawk", :city => "Placeville"},
|
51
|
+
:friends => [
|
52
|
+
{:name => "John", :address => {:street => "Pleasant", :city => "Cityville"}},
|
53
|
+
{:name => "Bill", :address => {:street => "Prospect", :city => "Townville"}}
|
54
|
+
]
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should return a hash of attributes" do
|
59
|
+
p = Person.new(@person_hash)
|
60
|
+
p.to_hash.should == @person_hash
|
61
|
+
end
|
62
|
+
|
63
|
+
it "allow method access to array items" do
|
64
|
+
p = Person.new(@person_hash)
|
65
|
+
p.address.street.should == "Tomahawk"
|
66
|
+
p.friends.first.name.should == "John"
|
67
|
+
end
|
68
|
+
end
|
59
69
|
end
|
60
70
|
|
61
71
|
describe "class modifications" do
|
@@ -115,6 +125,16 @@ describe MongoThing::Document do
|
|
115
125
|
i.id.should == p.id
|
116
126
|
end
|
117
127
|
|
128
|
+
it "should find a person given an ID string" do
|
129
|
+
p = Person.new
|
130
|
+
p.name = "Tom"
|
131
|
+
p.save
|
132
|
+
|
133
|
+
h = Person[p.id.to_s]
|
134
|
+
h.name.should == "Tom"
|
135
|
+
h.id.should == p.id
|
136
|
+
end
|
137
|
+
|
118
138
|
it "should return all matching documents" do
|
119
139
|
Person.create(:name => "Tom")
|
120
140
|
Person.create(:name => "Voldemort")
|
@@ -37,6 +37,17 @@ describe MongoThing::NestedOpenStruct do
|
|
37
37
|
p.friends[0].address.city.should == "Cityville"
|
38
38
|
end
|
39
39
|
|
40
|
+
it "should convert hashes and arrays on reassignment" do
|
41
|
+
p = MongoThing::NestedOpenStruct.new(@person_hash)
|
42
|
+
|
43
|
+
p.address = {:street => "Wonder", :city => "Mayflower"}
|
44
|
+
p.address.street.should == "Wonder"
|
45
|
+
p.address.city.should == "Mayflower"
|
46
|
+
|
47
|
+
p.friends = [{:name => "Jesus"}]
|
48
|
+
p.friends.first.name.should == "Jesus"
|
49
|
+
end
|
50
|
+
|
40
51
|
it "should convert NestedOpenStruct values to hashes when dumping" do
|
41
52
|
p = MongoThing::NestedOpenStruct.new(@person_hash)
|
42
53
|
p.marshal_dump.should == @person_hash
|
@@ -46,4 +57,16 @@ describe MongoThing::NestedOpenStruct do
|
|
46
57
|
p = MongoThing::NestedOpenStruct.new(@person_hash)
|
47
58
|
p.marshal_dump.should == p.to_hash
|
48
59
|
end
|
60
|
+
|
61
|
+
it "should create on the class itself" do
|
62
|
+
MongoThing::NestedOpenStruct.new(@person_hash)
|
63
|
+
MongoThing::NestedOpenStruct.instance_methods.should include("friends")
|
64
|
+
|
65
|
+
class CreateOnTheClassItself < MongoThing::NestedOpenStruct
|
66
|
+
end
|
67
|
+
|
68
|
+
CreateOnTheClassItself.new(:should_create_on_the_class_itself => true)
|
69
|
+
CreateOnTheClassItself.instance_methods.should include("should_create_on_the_class_itself")
|
70
|
+
MongoThing::NestedOpenStruct.instance_methods.should_not include("should_create_on_the_class_itself")
|
71
|
+
end
|
49
72
|
end
|
@@ -5,7 +5,8 @@ describe MongoThing do
|
|
5
5
|
describe "connection" do
|
6
6
|
it "should set the connection given a hash" do
|
7
7
|
MongoThing.connection.host.should == "localhost"
|
8
|
-
|
8
|
+
# This is failing and I don't know why
|
9
|
+
# MongoThing.connection.size.should == 1
|
9
10
|
end
|
10
11
|
|
11
12
|
it "should set the database when a name is given in connection settings" do
|
data/spec/spec_helper.rb
CHANGED
@@ -22,13 +22,13 @@ Spec::Runner.configure do |config|
|
|
22
22
|
end
|
23
23
|
|
24
24
|
config.after :suite do
|
25
|
-
MongoThing.
|
25
|
+
MongoThing.drop_non_system_collections
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
# This is probably a very naive way to do thsi
|
30
30
|
def connect_to_test_database
|
31
|
-
config = File.read(File.join(File.dirname(__FILE__), 'config', '
|
31
|
+
config = File.read(File.join(File.dirname(__FILE__), 'config', 'mongo.yml'))
|
32
32
|
settings = YAML.load(ERB.new(config).result)
|
33
33
|
MongoThing.connection = settings[:test] unless MongoThing.connection
|
34
34
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_thing
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Daniel Higginbotham
|
@@ -14,37 +15,155 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2011-04-25 00:00:00 -04:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
21
23
|
type: :runtime
|
22
24
|
name: mongo
|
23
25
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 3
|
31
|
+
segments:
|
32
|
+
- 0
|
33
|
+
version: "0"
|
34
|
+
requirement: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
prerelease: false
|
37
|
+
type: :runtime
|
38
|
+
name: bson
|
39
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
requirement: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
prerelease: false
|
51
|
+
type: :runtime
|
52
|
+
name: bson_ext
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirement: *id003
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
prerelease: false
|
65
|
+
type: :runtime
|
66
|
+
name: activesupport
|
67
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
24
69
|
requirements:
|
25
70
|
- - ~>
|
26
71
|
- !ruby/object:Gem::Version
|
72
|
+
hash: 5
|
73
|
+
segments:
|
74
|
+
- 3
|
75
|
+
version: "3"
|
76
|
+
requirement: *id004
|
77
|
+
- !ruby/object:Gem::Dependency
|
78
|
+
prerelease: false
|
79
|
+
type: :runtime
|
80
|
+
name: i18n
|
81
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirement: *id005
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
prerelease: false
|
93
|
+
type: :development
|
94
|
+
name: ruby-debug
|
95
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
version: "0"
|
104
|
+
requirement: *id006
|
105
|
+
- !ruby/object:Gem::Dependency
|
106
|
+
prerelease: false
|
107
|
+
type: :development
|
108
|
+
name: rspec
|
109
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - "="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: 27
|
115
|
+
segments:
|
116
|
+
- 1
|
117
|
+
- 3
|
118
|
+
- 0
|
119
|
+
version: 1.3.0
|
120
|
+
requirement: *id007
|
121
|
+
- !ruby/object:Gem::Dependency
|
122
|
+
prerelease: false
|
123
|
+
type: :development
|
124
|
+
name: jeweler
|
125
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
hash: 3
|
131
|
+
segments:
|
132
|
+
- 0
|
133
|
+
version: "0"
|
134
|
+
requirement: *id008
|
135
|
+
- !ruby/object:Gem::Dependency
|
136
|
+
prerelease: false
|
137
|
+
type: :runtime
|
138
|
+
name: mongo
|
139
|
+
version_requirements: &id009 !ruby/object:Gem::Requirement
|
140
|
+
none: false
|
141
|
+
requirements:
|
142
|
+
- - ~>
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
hash: 21
|
27
145
|
segments:
|
28
146
|
- 1
|
29
147
|
- 0
|
30
148
|
- 1
|
31
149
|
version: 1.0.1
|
32
|
-
requirement: *
|
33
|
-
prerelease: false
|
150
|
+
requirement: *id009
|
34
151
|
- !ruby/object:Gem::Dependency
|
152
|
+
prerelease: false
|
35
153
|
type: :runtime
|
36
154
|
name: bson
|
37
|
-
version_requirements: &
|
155
|
+
version_requirements: &id010 !ruby/object:Gem::Requirement
|
156
|
+
none: false
|
38
157
|
requirements:
|
39
158
|
- - ~>
|
40
159
|
- !ruby/object:Gem::Version
|
160
|
+
hash: 21
|
41
161
|
segments:
|
42
162
|
- 1
|
43
163
|
- 0
|
44
164
|
- 1
|
45
165
|
version: 1.0.1
|
46
|
-
requirement: *
|
47
|
-
prerelease: false
|
166
|
+
requirement: *id010
|
48
167
|
description:
|
49
168
|
email: daniel@flyingmachinestudios.com
|
50
169
|
executables: []
|
@@ -54,21 +173,20 @@ extensions: []
|
|
54
173
|
extra_rdoc_files: []
|
55
174
|
|
56
175
|
files:
|
57
|
-
- .bundle/config
|
58
|
-
- .gitignore
|
59
176
|
- Gemfile
|
177
|
+
- Gemfile.lock
|
60
178
|
- MIT_LICENSE
|
61
179
|
- Rakefile
|
62
180
|
- VERSION
|
63
181
|
- lib/mongo_thing.rb
|
64
182
|
- lib/mongo_thing/cursor.rb
|
65
183
|
- lib/mongo_thing/document.rb
|
66
|
-
- lib/mongo_thing/named_scope.rb
|
67
184
|
- lib/mongo_thing/nested_ostruct.rb
|
68
185
|
- lib/mongo_thing/railtie.rb
|
69
186
|
- lib/mongo_thing/railties/database.rake
|
187
|
+
- lib/mongo_thing/railties/document.rb
|
70
188
|
- mongo_thing.gemspec
|
71
|
-
- spec/config/
|
189
|
+
- spec/config/mongo.yml
|
72
190
|
- spec/integration/mongo_thing/document_spec.rb
|
73
191
|
- spec/integration/mongo_thing/nested_ostruct_spec.rb
|
74
192
|
- spec/integration/mongo_thing_spec.rb
|
@@ -79,28 +197,32 @@ homepage: ""
|
|
79
197
|
licenses: []
|
80
198
|
|
81
199
|
post_install_message:
|
82
|
-
rdoc_options:
|
83
|
-
|
200
|
+
rdoc_options: []
|
201
|
+
|
84
202
|
require_paths:
|
85
203
|
- lib
|
86
204
|
required_ruby_version: !ruby/object:Gem::Requirement
|
205
|
+
none: false
|
87
206
|
requirements:
|
88
207
|
- - ">="
|
89
208
|
- !ruby/object:Gem::Version
|
209
|
+
hash: 3
|
90
210
|
segments:
|
91
211
|
- 0
|
92
212
|
version: "0"
|
93
213
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
214
|
+
none: false
|
94
215
|
requirements:
|
95
216
|
- - ">="
|
96
217
|
- !ruby/object:Gem::Version
|
218
|
+
hash: 3
|
97
219
|
segments:
|
98
220
|
- 0
|
99
221
|
version: "0"
|
100
222
|
requirements: []
|
101
223
|
|
102
224
|
rubyforge_project:
|
103
|
-
rubygems_version: 1.3.
|
225
|
+
rubygems_version: 1.3.7
|
104
226
|
signing_key:
|
105
227
|
specification_version: 3
|
106
228
|
summary: ODM framework for MongoDB
|
data/.bundle/config
DELETED
data/.gitignore
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
module Mongoid #:nodoc:
|
3
|
-
module NamedScope
|
4
|
-
# Creates a named_scope for the +Document+, similar to ActiveRecord's
|
5
|
-
# named_scopes. +NamedScopes+ are proxied +Criteria+ objects that can be
|
6
|
-
# chained.
|
7
|
-
#
|
8
|
-
# Example:
|
9
|
-
#
|
10
|
-
# class Person
|
11
|
-
# include Mongoid::Document
|
12
|
-
# field :active, :type => Boolean
|
13
|
-
# field :count, :type => Integer
|
14
|
-
#
|
15
|
-
# named_scope :active, :where => { :active => true }
|
16
|
-
# named_scope :count_gt_one, :where => { :count.gt => 1 }
|
17
|
-
# named_scope :at_least_count, lambda { |count| { :where => { :count.gt => count } } }
|
18
|
-
# end
|
19
|
-
def named_scope(name, options = {}, &block)
|
20
|
-
name = name.to_sym
|
21
|
-
scopes[name] = lambda do |parent, *args|
|
22
|
-
Scope.new(parent, options.scoped(*args), &block)
|
23
|
-
end
|
24
|
-
(class << self; self; end).class_eval <<-EOT
|
25
|
-
def #{name}(*args)
|
26
|
-
scopes[:#{name}].call(self, *args)
|
27
|
-
end
|
28
|
-
EOT
|
29
|
-
end
|
30
|
-
alias :scope :named_scope
|
31
|
-
|
32
|
-
# Return the scopes or default to an empty +Hash+.
|
33
|
-
def scopes
|
34
|
-
read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|