serialization_scopes 0.3.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/serialization_scopes.rb +33 -18
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.0
|
data/lib/serialization_scopes.rb
CHANGED
@@ -2,41 +2,56 @@ module SerializationScopes
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
|
6
|
-
|
5
|
+
class_attribute :serialization_scopes, :instance_writer => false
|
6
|
+
alias_method_chain :to_xml, :scopes
|
7
|
+
alias_method_chain :as_json, :scopes
|
7
8
|
end
|
8
9
|
|
9
10
|
module ClassMethods
|
10
11
|
|
11
12
|
def serialization_scope(name, options = {})
|
13
|
+
self.serialization_scopes ||= {}
|
12
14
|
serialization_scopes[name.to_sym] = options
|
13
15
|
end
|
14
16
|
|
15
17
|
def scoped_serialization_options(options = {})
|
16
|
-
options
|
17
|
-
name =
|
18
|
-
scopes = name.present? && serialization_scopes
|
19
|
-
|
20
|
-
|
21
|
-
options[key] =
|
22
|
-
custom_options ? (Array.wrap(custom_options) + Array.wrap(scope_options)).uniq : Array.wrap(scope_options)
|
23
|
-
elsif [:only, :methods, :include].include?(key)
|
24
|
-
custom_options ? Array.wrap(custom_options) & Array.wrap(scope_options) : Array.wrap(scope_options)
|
25
|
-
else
|
26
|
-
custom_options ? custom_options : scope_options
|
27
|
-
end
|
18
|
+
options = options.try(:clone) || {}
|
19
|
+
name = options[:scope].try(:to_sym)
|
20
|
+
scopes = name.present? && serialization_scopes.key?(name) ? serialization_scopes[name] : serialization_scopes[:default]
|
21
|
+
|
22
|
+
scopes.each do |key, defaults|
|
23
|
+
options[key] = options[key] ? Resolver.scope(key, defaults, options[key]) : defaults
|
28
24
|
end if scopes
|
25
|
+
|
29
26
|
options
|
30
27
|
end
|
31
28
|
|
32
29
|
end
|
33
30
|
|
34
|
-
|
35
|
-
|
31
|
+
module Resolver
|
32
|
+
|
33
|
+
def self.scope(key, defaults, settings)
|
34
|
+
defaults = Array.wrap(defaults)
|
35
|
+
settings = Array.wrap(settings)
|
36
|
+
|
37
|
+
case key
|
38
|
+
when :except
|
39
|
+
(settings + defaults).uniq
|
40
|
+
when :only, :methods, :include
|
41
|
+
settings & defaults
|
42
|
+
else
|
43
|
+
settings
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_xml_with_scopes(options = {})
|
50
|
+
to_xml_without_scopes self.class.scoped_serialization_options(options)
|
36
51
|
end
|
37
52
|
|
38
|
-
def
|
39
|
-
|
53
|
+
def as_json_with_scopes(options = {})
|
54
|
+
as_json_without_scopes self.class.scoped_serialization_options(options)
|
40
55
|
end
|
41
56
|
|
42
57
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serialization_scopes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 63
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 8
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.8.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dimitrij Denissenko
|