mongo_mapper_ign 0.7.7 → 0.7.8
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -20
- data/README.rdoc +34 -34
- data/bin/mmconsole +60 -60
- data/lib/mongo_mapper.rb +123 -123
- data/lib/mongo_mapper/document.rb +292 -292
- data/lib/mongo_mapper/embedded_document.rb +71 -71
- data/lib/mongo_mapper/plugins.rb +36 -36
- data/lib/mongo_mapper/plugins/associations.rb +115 -115
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -124
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +31 -31
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +26 -26
- data/lib/mongo_mapper/plugins/associations/collection.rb +21 -21
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +39 -39
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +160 -144
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +29 -29
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +130 -130
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -32
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -24
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -14
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +42 -42
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +70 -70
- data/lib/mongo_mapper/plugins/associations/proxy.rb +125 -125
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -241
- data/lib/mongo_mapper/plugins/clone.rb +13 -13
- data/lib/mongo_mapper/plugins/descendants.rb +16 -16
- data/lib/mongo_mapper/plugins/dirty.rb +119 -119
- data/lib/mongo_mapper/plugins/equality.rb +23 -23
- data/lib/mongo_mapper/plugins/identity_map.rb +123 -123
- data/lib/mongo_mapper/plugins/inspect.rb +14 -14
- data/lib/mongo_mapper/plugins/keys.rb +322 -322
- data/lib/mongo_mapper/plugins/keys/key.rb +53 -53
- data/lib/mongo_mapper/plugins/logger.rb +17 -17
- data/lib/mongo_mapper/plugins/modifiers.rb +111 -111
- data/lib/mongo_mapper/plugins/pagination.rb +24 -24
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +72 -72
- data/lib/mongo_mapper/plugins/persistence.rb +96 -96
- data/lib/mongo_mapper/plugins/protected.rb +46 -46
- data/lib/mongo_mapper/plugins/rails.rb +57 -57
- data/lib/mongo_mapper/plugins/serialization.rb +92 -92
- data/lib/mongo_mapper/plugins/serialization/array.rb +56 -56
- data/lib/mongo_mapper/plugins/serialization/xml_serializer.rb +239 -239
- data/lib/mongo_mapper/plugins/timestamps.rb +21 -21
- data/lib/mongo_mapper/plugins/userstamps.rb +14 -14
- data/lib/mongo_mapper/plugins/validations.rb +46 -46
- data/lib/mongo_mapper/query.rb +28 -28
- data/lib/mongo_mapper/support.rb +197 -197
- data/lib/mongo_mapper/support/descendant_appends.rb +46 -46
- data/lib/mongo_mapper/support/find.rb +77 -77
- data/lib/mongo_mapper/version.rb +3 -3
- metadata +4 -25
@@ -1,97 +1,97 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Persistence
|
5
|
-
module ClassMethods
|
6
|
-
class Unsupported < MongoMapperError; end
|
7
|
-
|
8
|
-
def connection(mongo_connection=nil)
|
9
|
-
not_supported_by_embedded
|
10
|
-
if mongo_connection.nil?
|
11
|
-
@connection ||= MongoMapper.connection
|
12
|
-
else
|
13
|
-
@connection = mongo_connection
|
14
|
-
end
|
15
|
-
@connection
|
16
|
-
end
|
17
|
-
|
18
|
-
def slave_connection
|
19
|
-
not_supported_by_embedded
|
20
|
-
raise "Slave connections have not been configured" if MongoMapper.slave_connections.empty?
|
21
|
-
@slave_idx ||= 0
|
22
|
-
conn = MongoMapper.slave_connections[@slave_idx]
|
23
|
-
increment_slave_idx
|
24
|
-
conn
|
25
|
-
end
|
26
|
-
|
27
|
-
def set_database_name(name)
|
28
|
-
not_supported_by_embedded
|
29
|
-
@database_name = name
|
30
|
-
end
|
31
|
-
|
32
|
-
def database_name
|
33
|
-
not_supported_by_embedded
|
34
|
-
@database_name
|
35
|
-
end
|
36
|
-
|
37
|
-
def database
|
38
|
-
not_supported_by_embedded
|
39
|
-
if database_name.nil?
|
40
|
-
MongoMapper.database
|
41
|
-
else
|
42
|
-
connection.db(database_name)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def slave_database
|
47
|
-
not_supported_by_embedded
|
48
|
-
conn = slave_connection
|
49
|
-
if database_name.nil?
|
50
|
-
conn.db(MongoMapper.database.name)
|
51
|
-
else
|
52
|
-
conn.db(database_name)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def set_collection_name(name)
|
57
|
-
not_supported_by_embedded
|
58
|
-
@collection_name = name
|
59
|
-
end
|
60
|
-
|
61
|
-
def collection_name
|
62
|
-
not_supported_by_embedded
|
63
|
-
@collection_name ||= self.to_s.tableize.gsub(/\//, '.')
|
64
|
-
end
|
65
|
-
|
66
|
-
def collection
|
67
|
-
not_supported_by_embedded
|
68
|
-
database.collection(collection_name)
|
69
|
-
end
|
70
|
-
|
71
|
-
def slave_collection
|
72
|
-
not_supported_by_embedded
|
73
|
-
slave_database.collection(collection_name)
|
74
|
-
end
|
75
|
-
|
76
|
-
private
|
77
|
-
def not_supported_by_embedded
|
78
|
-
raise Unsupported.new('This is not supported for embeddable documents at this time.') if embeddable?
|
79
|
-
end
|
80
|
-
|
81
|
-
def increment_slave_idx
|
82
|
-
@slave_idx = @slave_idx + 1 <= MongoMapper.slave_connections.length-1 ? @slave_idx + 1 : 0
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
module InstanceMethods
|
87
|
-
def collection
|
88
|
-
_root_document.class.collection
|
89
|
-
end
|
90
|
-
|
91
|
-
def database
|
92
|
-
_root_document.class.database
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Persistence
|
5
|
+
module ClassMethods
|
6
|
+
class Unsupported < MongoMapperError; end
|
7
|
+
|
8
|
+
def connection(mongo_connection=nil)
|
9
|
+
not_supported_by_embedded
|
10
|
+
if mongo_connection.nil?
|
11
|
+
@connection ||= MongoMapper.connection
|
12
|
+
else
|
13
|
+
@connection = mongo_connection
|
14
|
+
end
|
15
|
+
@connection
|
16
|
+
end
|
17
|
+
|
18
|
+
def slave_connection
|
19
|
+
not_supported_by_embedded
|
20
|
+
raise "Slave connections have not been configured" if MongoMapper.slave_connections.empty?
|
21
|
+
@slave_idx ||= 0
|
22
|
+
conn = MongoMapper.slave_connections[@slave_idx]
|
23
|
+
increment_slave_idx
|
24
|
+
conn
|
25
|
+
end
|
26
|
+
|
27
|
+
def set_database_name(name)
|
28
|
+
not_supported_by_embedded
|
29
|
+
@database_name = name
|
30
|
+
end
|
31
|
+
|
32
|
+
def database_name
|
33
|
+
not_supported_by_embedded
|
34
|
+
@database_name
|
35
|
+
end
|
36
|
+
|
37
|
+
def database
|
38
|
+
not_supported_by_embedded
|
39
|
+
if database_name.nil?
|
40
|
+
MongoMapper.database
|
41
|
+
else
|
42
|
+
connection.db(database_name)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def slave_database
|
47
|
+
not_supported_by_embedded
|
48
|
+
conn = slave_connection
|
49
|
+
if database_name.nil?
|
50
|
+
conn.db(MongoMapper.database.name)
|
51
|
+
else
|
52
|
+
conn.db(database_name)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_collection_name(name)
|
57
|
+
not_supported_by_embedded
|
58
|
+
@collection_name = name
|
59
|
+
end
|
60
|
+
|
61
|
+
def collection_name
|
62
|
+
not_supported_by_embedded
|
63
|
+
@collection_name ||= self.to_s.tableize.gsub(/\//, '.')
|
64
|
+
end
|
65
|
+
|
66
|
+
def collection
|
67
|
+
not_supported_by_embedded
|
68
|
+
database.collection(collection_name)
|
69
|
+
end
|
70
|
+
|
71
|
+
def slave_collection
|
72
|
+
not_supported_by_embedded
|
73
|
+
slave_database.collection(collection_name)
|
74
|
+
end
|
75
|
+
|
76
|
+
private
|
77
|
+
def not_supported_by_embedded
|
78
|
+
raise Unsupported.new('This is not supported for embeddable documents at this time.') if embeddable?
|
79
|
+
end
|
80
|
+
|
81
|
+
def increment_slave_idx
|
82
|
+
@slave_idx = @slave_idx + 1 <= MongoMapper.slave_connections.length-1 ? @slave_idx + 1 : 0
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
module InstanceMethods
|
87
|
+
def collection
|
88
|
+
_root_document.class.collection
|
89
|
+
end
|
90
|
+
|
91
|
+
def database
|
92
|
+
_root_document.class.database
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
97
|
end
|
@@ -1,46 +1,46 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Protected
|
5
|
-
module ClassMethods
|
6
|
-
def attr_protected(*attrs)
|
7
|
-
self.write_inheritable_attribute(:attr_protected, Set.new(attrs) + (protected_attributes || []))
|
8
|
-
end
|
9
|
-
|
10
|
-
def protected_attributes
|
11
|
-
self.read_inheritable_attribute(:attr_protected)
|
12
|
-
end
|
13
|
-
|
14
|
-
def key(*args)
|
15
|
-
key = super
|
16
|
-
attr_protected key.name.to_sym if key.options[:protected]
|
17
|
-
key
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
module InstanceMethods
|
22
|
-
def assign(attrs={})
|
23
|
-
super(filter_protected_attrs(attrs))
|
24
|
-
end
|
25
|
-
|
26
|
-
def update_attributes(attrs={})
|
27
|
-
super(filter_protected_attrs(attrs))
|
28
|
-
end
|
29
|
-
|
30
|
-
def update_attributes!(attrs={})
|
31
|
-
super(filter_protected_attrs(attrs))
|
32
|
-
end
|
33
|
-
|
34
|
-
def protected_attributes
|
35
|
-
self.class.protected_attributes
|
36
|
-
end
|
37
|
-
|
38
|
-
protected
|
39
|
-
def filter_protected_attrs(attrs)
|
40
|
-
return attrs if protected_attributes.blank? || attrs.blank?
|
41
|
-
attrs.dup.delete_if { |key, val| protected_attributes.include?(key.to_sym) }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Protected
|
5
|
+
module ClassMethods
|
6
|
+
def attr_protected(*attrs)
|
7
|
+
self.write_inheritable_attribute(:attr_protected, Set.new(attrs) + (protected_attributes || []))
|
8
|
+
end
|
9
|
+
|
10
|
+
def protected_attributes
|
11
|
+
self.read_inheritable_attribute(:attr_protected)
|
12
|
+
end
|
13
|
+
|
14
|
+
def key(*args)
|
15
|
+
key = super
|
16
|
+
attr_protected key.name.to_sym if key.options[:protected]
|
17
|
+
key
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module InstanceMethods
|
22
|
+
def assign(attrs={})
|
23
|
+
super(filter_protected_attrs(attrs))
|
24
|
+
end
|
25
|
+
|
26
|
+
def update_attributes(attrs={})
|
27
|
+
super(filter_protected_attrs(attrs))
|
28
|
+
end
|
29
|
+
|
30
|
+
def update_attributes!(attrs={})
|
31
|
+
super(filter_protected_attrs(attrs))
|
32
|
+
end
|
33
|
+
|
34
|
+
def protected_attributes
|
35
|
+
self.class.protected_attributes
|
36
|
+
end
|
37
|
+
|
38
|
+
protected
|
39
|
+
def filter_protected_attrs(attrs)
|
40
|
+
return attrs if protected_attributes.blank? || attrs.blank?
|
41
|
+
attrs.dup.delete_if { |key, val| protected_attributes.include?(key.to_sym) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -1,58 +1,58 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
module MongoMapper
|
3
|
-
module Plugins
|
4
|
-
module Rails
|
5
|
-
def self.configure(model)
|
6
|
-
model.extend ActiveModel::Naming if defined?(ActiveModel)
|
7
|
-
end
|
8
|
-
|
9
|
-
module InstanceMethods
|
10
|
-
def to_param
|
11
|
-
id.to_s if persisted?
|
12
|
-
end
|
13
|
-
|
14
|
-
def to_model
|
15
|
-
self
|
16
|
-
end
|
17
|
-
|
18
|
-
def to_key
|
19
|
-
[id] if persisted?
|
20
|
-
end
|
21
|
-
|
22
|
-
def new_record?
|
23
|
-
new?
|
24
|
-
end
|
25
|
-
|
26
|
-
def read_attribute(name)
|
27
|
-
self[name]
|
28
|
-
end
|
29
|
-
|
30
|
-
def read_attribute_before_typecast(name)
|
31
|
-
read_key_before_typecast(name)
|
32
|
-
end
|
33
|
-
|
34
|
-
def write_attribute(name, value)
|
35
|
-
self[name] = value
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
module ClassMethods
|
40
|
-
def has_one(*args)
|
41
|
-
one(*args)
|
42
|
-
end
|
43
|
-
|
44
|
-
def has_many(*args)
|
45
|
-
many(*args)
|
46
|
-
end
|
47
|
-
|
48
|
-
def column_names
|
49
|
-
keys.keys
|
50
|
-
end
|
51
|
-
|
52
|
-
def human_name
|
53
|
-
self.name.demodulize.titleize
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Rails
|
5
|
+
def self.configure(model)
|
6
|
+
model.extend ActiveModel::Naming if defined?(ActiveModel)
|
7
|
+
end
|
8
|
+
|
9
|
+
module InstanceMethods
|
10
|
+
def to_param
|
11
|
+
id.to_s if persisted?
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_model
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_key
|
19
|
+
[id] if persisted?
|
20
|
+
end
|
21
|
+
|
22
|
+
def new_record?
|
23
|
+
new?
|
24
|
+
end
|
25
|
+
|
26
|
+
def read_attribute(name)
|
27
|
+
self[name]
|
28
|
+
end
|
29
|
+
|
30
|
+
def read_attribute_before_typecast(name)
|
31
|
+
read_key_before_typecast(name)
|
32
|
+
end
|
33
|
+
|
34
|
+
def write_attribute(name, value)
|
35
|
+
self[name] = value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module ClassMethods
|
40
|
+
def has_one(*args)
|
41
|
+
one(*args)
|
42
|
+
end
|
43
|
+
|
44
|
+
def has_many(*args)
|
45
|
+
many(*args)
|
46
|
+
end
|
47
|
+
|
48
|
+
def column_names
|
49
|
+
keys.keys
|
50
|
+
end
|
51
|
+
|
52
|
+
def human_name
|
53
|
+
self.name.demodulize.titleize
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
58
|
end
|
@@ -1,92 +1,92 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
require 'active_support/json'
|
3
|
-
|
4
|
-
module MongoMapper
|
5
|
-
module Plugins
|
6
|
-
module Serialization
|
7
|
-
def self.configure(model)
|
8
|
-
model.class_eval { cattr_accessor :include_root_in_json, :instance_writer => true }
|
9
|
-
end
|
10
|
-
|
11
|
-
module InstanceMethods
|
12
|
-
def to_xml(options = {}, &block)
|
13
|
-
options[:dasherize] = false unless options.has_key?(:dasherize)
|
14
|
-
serializer = Serialization::XmlSerializer.new(self, options)
|
15
|
-
block_given? ? serializer.to_s(&block) : serializer.to_s
|
16
|
-
end
|
17
|
-
|
18
|
-
def as_json options={}
|
19
|
-
options ||= {}
|
20
|
-
unless options[:only]
|
21
|
-
methods = [options.delete(:methods)].flatten.compact
|
22
|
-
methods << :id
|
23
|
-
options[:methods] = methods.uniq
|
24
|
-
end
|
25
|
-
|
26
|
-
except = [options.delete(:except)].flatten.compact
|
27
|
-
except << :_id
|
28
|
-
options[:except] = except
|
29
|
-
|
30
|
-
# Direct rip from Rails 3 ActiveModel Serialization (#serializable_hash)
|
31
|
-
hash = begin
|
32
|
-
options[:only] = Array.wrap(options[:only]).map { |n| n.to_s }
|
33
|
-
options[:except] = Array.wrap(options[:except]).map { |n| n.to_s }
|
34
|
-
|
35
|
-
attribute_names = attributes.keys.sort
|
36
|
-
if options[:only].any?
|
37
|
-
attribute_names &= options[:only]
|
38
|
-
elsif options[:except].any?
|
39
|
-
attribute_names -= options[:except]
|
40
|
-
end
|
41
|
-
|
42
|
-
method_names = Array.wrap(options[:methods]).inject([]) do |methods, name|
|
43
|
-
methods << name if respond_to?(name.to_s)
|
44
|
-
methods
|
45
|
-
end
|
46
|
-
|
47
|
-
(attribute_names + method_names).inject({}) { |hash, name|
|
48
|
-
hash[name] = send(name)
|
49
|
-
hash
|
50
|
-
}
|
51
|
-
end
|
52
|
-
# End rip
|
53
|
-
|
54
|
-
options.delete(:only) if options[:only].nil? or options[:only].empty?
|
55
|
-
|
56
|
-
hash.each do |key, value|
|
57
|
-
if value.is_a?(Array)
|
58
|
-
hash[key] = value.map do |item|
|
59
|
-
item.respond_to?(:as_json) ? item.as_json(options) : item
|
60
|
-
end
|
61
|
-
elsif value.is_a? BSON::ObjectID
|
62
|
-
hash[key] = value.to_s
|
63
|
-
elsif value.respond_to?(:as_json)
|
64
|
-
hash[key] = value.as_json(options)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# Replicate Rails 3 naming - and also bin anytihng after : for use in our dynamic classes from unit tests
|
69
|
-
hash = { ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).gsub(/:.*/,'') => hash } if include_root_in_json
|
70
|
-
hash
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
module ClassMethods
|
75
|
-
def from_json(json)
|
76
|
-
self.new(ActiveSupport::JSON.decode(json))
|
77
|
-
end
|
78
|
-
|
79
|
-
def from_xml(xml)
|
80
|
-
# instance = self.new
|
81
|
-
# instance.attributes = self.new
|
82
|
-
# instance
|
83
|
-
instance.attributes = self.new(Hash.from_xml(xml).values.first)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
require 'mongo_mapper/plugins/serialization/xml_serializer'
|
92
|
-
require 'mongo_mapper/plugins/serialization/array'
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'active_support/json'
|
3
|
+
|
4
|
+
module MongoMapper
|
5
|
+
module Plugins
|
6
|
+
module Serialization
|
7
|
+
def self.configure(model)
|
8
|
+
model.class_eval { cattr_accessor :include_root_in_json, :instance_writer => true }
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
def to_xml(options = {}, &block)
|
13
|
+
options[:dasherize] = false unless options.has_key?(:dasherize)
|
14
|
+
serializer = Serialization::XmlSerializer.new(self, options)
|
15
|
+
block_given? ? serializer.to_s(&block) : serializer.to_s
|
16
|
+
end
|
17
|
+
|
18
|
+
def as_json options={}
|
19
|
+
options ||= {}
|
20
|
+
unless options[:only]
|
21
|
+
methods = [options.delete(:methods)].flatten.compact
|
22
|
+
methods << :id
|
23
|
+
options[:methods] = methods.uniq
|
24
|
+
end
|
25
|
+
|
26
|
+
except = [options.delete(:except)].flatten.compact
|
27
|
+
except << :_id
|
28
|
+
options[:except] = except
|
29
|
+
|
30
|
+
# Direct rip from Rails 3 ActiveModel Serialization (#serializable_hash)
|
31
|
+
hash = begin
|
32
|
+
options[:only] = Array.wrap(options[:only]).map { |n| n.to_s }
|
33
|
+
options[:except] = Array.wrap(options[:except]).map { |n| n.to_s }
|
34
|
+
|
35
|
+
attribute_names = attributes.keys.sort
|
36
|
+
if options[:only].any?
|
37
|
+
attribute_names &= options[:only]
|
38
|
+
elsif options[:except].any?
|
39
|
+
attribute_names -= options[:except]
|
40
|
+
end
|
41
|
+
|
42
|
+
method_names = Array.wrap(options[:methods]).inject([]) do |methods, name|
|
43
|
+
methods << name if respond_to?(name.to_s)
|
44
|
+
methods
|
45
|
+
end
|
46
|
+
|
47
|
+
(attribute_names + method_names).inject({}) { |hash, name|
|
48
|
+
hash[name] = send(name)
|
49
|
+
hash
|
50
|
+
}
|
51
|
+
end
|
52
|
+
# End rip
|
53
|
+
|
54
|
+
options.delete(:only) if options[:only].nil? or options[:only].empty?
|
55
|
+
|
56
|
+
hash.each do |key, value|
|
57
|
+
if value.is_a?(Array)
|
58
|
+
hash[key] = value.map do |item|
|
59
|
+
item.respond_to?(:as_json) ? item.as_json(options) : item
|
60
|
+
end
|
61
|
+
elsif value.is_a? BSON::ObjectID
|
62
|
+
hash[key] = value.to_s
|
63
|
+
elsif value.respond_to?(:as_json)
|
64
|
+
hash[key] = value.as_json(options)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Replicate Rails 3 naming - and also bin anytihng after : for use in our dynamic classes from unit tests
|
69
|
+
hash = { ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).gsub(/:.*/,'') => hash } if include_root_in_json
|
70
|
+
hash
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
module ClassMethods
|
75
|
+
def from_json(json)
|
76
|
+
self.new(ActiveSupport::JSON.decode(json))
|
77
|
+
end
|
78
|
+
|
79
|
+
def from_xml(xml)
|
80
|
+
# instance = self.new
|
81
|
+
# instance.attributes = self.new
|
82
|
+
# instance
|
83
|
+
instance.attributes = self.new(Hash.from_xml(xml).values.first)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
require 'mongo_mapper/plugins/serialization/xml_serializer'
|
92
|
+
require 'mongo_mapper/plugins/serialization/array'
|