easy_serializer 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8c4bcd2510b26fd9bc5bce354fa24b8063ab3cd7
4
- data.tar.gz: 6ad7cac70735ddc9ba2b5cec6285a04a8ef62f70
3
+ metadata.gz: 358888cc7f61e9b3510147bbe505d1418225b41e
4
+ data.tar.gz: 2de0f0cda813e67bd51a10166da9b05a5d854f8b
5
5
  SHA512:
6
- metadata.gz: 8d7bedfffb05a5a67d7f6719cc6fdf2caea443d71506b227cdf10e840602516b1d8f92dcf86bcfcef5207953b42b60edc250e25b4cfd3f5cf7b5fc233139942e
7
- data.tar.gz: 39425cec8ea178f8e7123483dceeac92b5790e277b7c7befa09feb4c11d950946459edcab9dc92ac1797444a7e372fa12b4299cc1e40ef1f973da20b0177a21a
6
+ metadata.gz: 09d793a662fb451a75f0a503361e62f1d38677f46e3297b432d31348f11d810c1c87dd88efbc00ff35177cde624189303273f3a730ec86c3c2d840f3f519b66c
7
+ data.tar.gz: 5855120cdba4d4eb42e77c7da6f0e25ab80225f62c9ada0cb19298cd09078c16c3126b66c7cf2766cce7bcd54cc905f2d4d05e6b1e6007d978fc9d83beaad568
@@ -14,4 +14,5 @@ end
14
14
 
15
15
  require 'easy_serializer/helpers'
16
16
  require 'easy_serializer/cacher'
17
+ require 'easy_serializer/attribute'
17
18
  require 'easy_serializer/base'
@@ -0,0 +1,60 @@
1
+ module EasySerializer
2
+ Attribute = Struct.new(:serializer, :setup) do
3
+ include Helpers
4
+ def self.call(serializer, setup)
5
+ new(serializer, setup).value
6
+ end
7
+
8
+ def object
9
+ serializer.object
10
+ end
11
+ def value
12
+ value_or_default
13
+ end
14
+
15
+ def value_or_default
16
+ value = attr_serializer
17
+ if value.nil? && setup[:default]
18
+ return option_to_value(setup[:default], object, serializer)
19
+ end
20
+ value
21
+ end
22
+
23
+ def attr_serializer
24
+ value = cache_or_attribute
25
+ return value.output if value.is_a?(CacheOutput)
26
+ return value unless serializer_class = setup[:serializer]
27
+ if setup[:collection]
28
+ Array.wrap(value).map { |o| serialize!(serializer_class, o) }
29
+ else
30
+ serialize!(serializer_class, value)
31
+ end
32
+ end
33
+ def send_name(obj)
34
+ obj.send(setup[:name])
35
+ end
36
+
37
+ def cache_or_attribute
38
+ execute = setup[:block] || method(:send_name)
39
+ if EasySerializer.perform_caching && setup[:cache]
40
+ Cacher.call(serializer, setup, nil, &execute)
41
+ else
42
+ serializer.instance_exec object, &execute
43
+ end
44
+ end
45
+
46
+ def send_to_serializer(serializer_class, value)
47
+ return unless value
48
+ option_to_value(serializer_class, value, serializer).call(value)
49
+ end
50
+
51
+ def serialize!(serializer_class, value)
52
+ return unless value
53
+ if EasySerializer.perform_caching && setup[:cache]
54
+ Cacher.call(serializer, setup, value)
55
+ else
56
+ send_to_serializer(serializer_class, value)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -47,10 +47,15 @@ module EasySerializer
47
47
  alias_method :to_hash, :serialize
48
48
  alias_method :to_s, :to_json
49
49
 
50
+ def send_to_serializer(serializer, value)
51
+ return unless value
52
+ option_to_value(serializer, value).call(value)
53
+ end
54
+
50
55
  private
51
56
 
52
57
  def _serialize
53
- __serializable_attributes.each_with_object(HashWithIndifferentAccess.new) do |setup, hash|
58
+ __serializable_attributes.each_with_object({}) do |setup, hash|
54
59
  if setup[:key] === false
55
60
  hash.merge!(value_or_default(setup))
56
61
  else
@@ -64,7 +69,7 @@ module EasySerializer
64
69
  return false unless EasySerializer.perform_caching
65
70
  cache = __cache
66
71
  return false unless cache
67
- Cacher.root_call(self, cache, object) { _serialize }
72
+ Cacher.root_call(self, cache, object) { _serialize }.output
68
73
  end
69
74
 
70
75
  def __cache
@@ -76,45 +81,50 @@ module EasySerializer
76
81
  end
77
82
 
78
83
  def value_or_default(setup)
79
- value = attr_serializer(setup)
80
- if value.nil? && setup[:default]
81
- return option_to_value(setup[:default], object)
82
- end
83
- value
84
+ Attribute.call(self, setup)
84
85
  end
85
86
 
86
- def attr_serializer(setup)
87
- value = cache_or_attribute(setup)
88
- return value unless serializer = setup[:serializer]
89
- if setup[:collection]
90
- Array.wrap(value).map { |o| cache_or_serialize(serializer, o, setup) }
91
- else
92
- cache_or_serialize(serializer, value, setup)
93
- end
94
- end
87
+ # def value_or_default(setup)
88
+ # value = attr_serializer(setup)
89
+ # if value.nil? && setup[:default]
90
+ # return option_to_value(setup[:default], object)
91
+ # end
92
+ # value
93
+ # end
94
+ #
95
+ # def attr_serializer(setup)
96
+ # value = cache_or_attribute(setup)
97
+ # return value if value.respond_to?(:fetch) || value.respond_to?(:each)
98
+ # return value unless serializer = setup[:serializer]
99
+ # if setup[:collection]
100
+ # Array.wrap(value).map { |o| cache_or_serialize(serializer, o, setup) }
101
+ # else
102
+ # cache_or_serialize(serializer, value, setup)
103
+ # end
104
+ # end
105
+ #
106
+ # def cache_or_attribute(setup)
107
+ # execute = setup[:block] || proc { |o| o.send(setup[:name]) }
108
+ # if EasySerializer.perform_caching && setup[:cache]
109
+ # Cacher.call(self, setup, nil, &execute)
110
+ # else
111
+ # if object.respond_to?(:each)
112
+ # object.map { |o| instance_exec o, &execute }
113
+ # else
114
+ # instance_exec object, &execute
115
+ # end
116
+ # end
117
+ # end
118
+ #
119
+ # def cache_or_serialize(serializer, value, opts)
120
+ # return unless value
121
+ # if EasySerializer.perform_caching && opts[:cache]
122
+ # Cacher.call(self, opts, value)
123
+ # else
124
+ # send_to_serializer(serializer, value)
125
+ # end
126
+ # end
95
127
 
96
- def cache_or_attribute(setup)
97
- execute = setup[:block] || proc { |o| o.send(setup[:name]) }
98
- if EasySerializer.perform_caching && setup[:cache]
99
- Cacher.call(self, setup, nil, &execute)
100
- else
101
- instance_exec object, &execute
102
- end
103
- end
104
128
 
105
- def cache_or_serialize(serializer, value, opts)
106
- return unless value
107
- if EasySerializer.perform_caching && opts[:cache]
108
- Cacher.call(self, opts, value)
109
- else
110
- send_to_serializer(serializer, value)
111
- end
112
- end
113
-
114
-
115
- def send_to_serializer(serializer, value)
116
- return unless value
117
- option_to_value(serializer, value).call(value)
118
- end
119
129
  end
120
130
  end
@@ -1,4 +1,5 @@
1
1
  module EasySerializer
2
+ CacheOutput = Struct.new(:output)
2
3
  Cacher = Struct.new(:serializer) do
3
4
  include Helpers
4
5
 
@@ -29,14 +30,14 @@ module EasySerializer
29
30
  @value ||= serializer.instance_exec object, &block
30
31
  end
31
32
 
32
-
33
- def key
33
+ def key(_value = nil)
34
+ _value ||= value
34
35
  @key ||= if options[:cache_key]
35
- option_to_value(options[:cache_key], value, serializer)
36
+ option_to_value(options[:cache_key], _value, serializer)
36
37
  elsif options[:serializer] || options[:root_call]
37
- [value, 'EasySerialized'].flatten
38
+ [_value, 'EasySerialized'].flatten
38
39
  else
39
- [value, options[:name], 'EasySerialized'].flatten
40
+ [_value, options[:name], 'EasySerialized'].flatten
40
41
  end
41
42
  end
42
43
 
@@ -48,13 +49,32 @@ module EasySerializer
48
49
  end || {}
49
50
  end
50
51
 
51
- def execute
52
- to_execute = if options[:serializer]
53
- proc { serializer.send_to_serializer(options[:serializer], value) }
54
- elsif !options[:serializer]
52
+ def _block_to_execute(_value = nil)
53
+ _value ||= value
54
+ if options[:serializer]
55
+ proc { serializer.send_to_serializer(options[:serializer], _value) }
56
+ else
55
57
  proc { serializer.instance_exec object, &block }
56
58
  end
57
- EasySerializer.cache.fetch(key, options_for_cache, &to_execute)
59
+ end
60
+
61
+ def fetch(_key = nil, _value = nil)
62
+ _key ||= key
63
+ _value ||= value
64
+ EasySerializer.cache.fetch(_key, options_for_cache, &_block_to_execute(_value))
65
+ end
66
+
67
+ def wrap(elem)
68
+ CacheOutput.new(elem)
69
+ end
70
+
71
+ def execute
72
+ elem = if options[:collection]
73
+ Array.wrap(value).map{ |o| fetch(key(o), o) }
74
+ else
75
+ fetch
76
+ end
77
+ wrap(elem)
58
78
  end
59
79
  end
60
80
  end
@@ -1,3 +1,3 @@
1
1
  module EasySerializer
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Pañach
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-31 00:00:00.000000000 Z
11
+ date: 2016-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ files:
88
88
  - bin/setup
89
89
  - easy_serializer.gemspec
90
90
  - lib/easy_serializer.rb
91
+ - lib/easy_serializer/attribute.rb
91
92
  - lib/easy_serializer/base.rb
92
93
  - lib/easy_serializer/cacher.rb
93
94
  - lib/easy_serializer/helpers.rb