mongodb_model 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/mongo/model/conversion.rb +5 -9
- data/lib/mongo/model/support.rb +1 -2
- data/spec/conversion_spec.rb +16 -16
- metadata +8 -8
@@ -20,10 +20,6 @@ module Mongo::Model::Conversion
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def to_hash options = {}
|
23
|
-
# return super if args.empty?
|
24
|
-
#
|
25
|
-
# options = args.first
|
26
|
-
|
27
23
|
if profile = options[:profile]
|
28
24
|
raise "no other optins are allowed when using :profile option!" if options.size > 1
|
29
25
|
profile_options = self.class.profiles[profile] || raise("profile :#{profile} not defined for #{self.class}!")
|
@@ -44,21 +40,21 @@ module Mongo::Model::Conversion
|
|
44
40
|
instance_variables.each do |iv_name|
|
45
41
|
value = instance_variable_get iv_name
|
46
42
|
value = convert_model value, :to_hash, child_options
|
47
|
-
result[iv_name[1.. -1]] = value
|
43
|
+
result[iv_name[1.. -1].to_sym] = value
|
48
44
|
end
|
49
45
|
|
50
46
|
methods = options[:methods] ? Array(options[:methods]) : []
|
51
47
|
methods.each do |method|
|
52
48
|
value = send method
|
53
49
|
value = convert_model value, :to_hash, child_options
|
54
|
-
result[method.
|
50
|
+
result[method.to_sym] = value
|
55
51
|
end
|
56
52
|
|
57
53
|
with_errors = options.include?(:errors) ? options[:errors] : true
|
58
54
|
if with_errors and !(errors = self.errors).empty?
|
59
|
-
|
60
|
-
errors.each{|k, v|
|
61
|
-
result[
|
55
|
+
errors = {}
|
56
|
+
self.errors.each{|k, v| errors[k.to_sym] = v}
|
57
|
+
result[:errors] = errors
|
62
58
|
end
|
63
59
|
|
64
60
|
result
|
data/lib/mongo/model/support.rb
CHANGED
data/spec/conversion_spec.rb
CHANGED
@@ -41,24 +41,24 @@ describe 'Conversion' do
|
|
41
41
|
it "should work without arguments" do
|
42
42
|
post = build_post_with_comment
|
43
43
|
post.to_hash.should == {
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
{
|
44
|
+
text: 'StarCraft releasing soon!',
|
45
|
+
token: 'secret',
|
46
|
+
comments: [
|
47
|
+
{text: 'Cool!'}
|
48
48
|
]
|
49
49
|
}
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should accept :only, :except and :methods options" do
|
53
53
|
post = build_post_with_comment
|
54
|
-
post.to_hash(only: :text).should == {
|
54
|
+
post.to_hash(only: :text).should == {text: 'StarCraft releasing soon!'}
|
55
55
|
post.to_hash(except: :token).should == {
|
56
|
-
|
57
|
-
|
58
|
-
{
|
56
|
+
text: 'StarCraft releasing soon!',
|
57
|
+
comments: [
|
58
|
+
{text: 'Cool!'}
|
59
59
|
]
|
60
60
|
}
|
61
|
-
post.to_hash(only: [], methods: :teaser).should == {
|
61
|
+
post.to_hash(only: [], methods: :teaser).should == {teaser: 'StarCraft r'}
|
62
62
|
end
|
63
63
|
|
64
64
|
it "should use conversion profiles" do
|
@@ -75,10 +75,10 @@ describe 'Conversion' do
|
|
75
75
|
end
|
76
76
|
|
77
77
|
post.to_hash(profile: :public).should == {
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
{
|
78
|
+
text: 'StarCraft releasing soon!',
|
79
|
+
teaser: 'StarCraft r',
|
80
|
+
comments: [
|
81
|
+
{text: 'Cool!'}
|
82
82
|
]
|
83
83
|
}
|
84
84
|
end
|
@@ -92,12 +92,12 @@ describe 'Conversion' do
|
|
92
92
|
post.valid?.should be_false
|
93
93
|
|
94
94
|
post.to_hash.should == {
|
95
|
-
|
96
|
-
|
95
|
+
text: 'StarCraft releasing soon!',
|
96
|
+
errors: {token: ["can't be empty"]}
|
97
97
|
}
|
98
98
|
|
99
99
|
post.to_hash(errors: false).should == {
|
100
|
-
|
100
|
+
text: 'StarCraft releasing soon!'
|
101
101
|
}
|
102
102
|
end
|
103
103
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongodb_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-19 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mongodb
|
16
|
-
requirement: &
|
16
|
+
requirement: &2848930 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2848930
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: validatable2
|
27
|
-
requirement: &
|
27
|
+
requirement: &2848690 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2848690
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: ruby_ext
|
38
|
-
requirement: &
|
38
|
+
requirement: &2848450 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2848450
|
47
47
|
description:
|
48
48
|
email:
|
49
49
|
executables: []
|