serialization_scopes 0.3.0 → 0.8.0

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.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/serialization_scopes.rb +33 -18
  3. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.8.0
@@ -2,41 +2,56 @@ module SerializationScopes
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- class_inheritable_reader :serialization_scopes
6
- write_inheritable_attribute :serialization_scopes, {}
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 = (options || {})[:scope]
18
- scopes = name.present? && serialization_scopes[name.to_sym] ? serialization_scopes[name.to_sym] : serialization_scopes[:default]
19
- scopes.each do |key, scope_options|
20
- custom_options = options[key]
21
- options[key] = if key == :except
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
- def to_xml(options = {})
35
- super self.class.scoped_serialization_options(options)
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 as_json(options = {})
39
- super self.class.scoped_serialization_options(options)
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: 19
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
8
+ - 8
9
9
  - 0
10
- version: 0.3.0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dimitrij Denissenko