zenaton 0.1.1 → 0.2.0

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
  SHA256:
3
- metadata.gz: f1385767c881e6ef34069a875ca988133b9904b28f1446ab14cf1104d928122a
4
- data.tar.gz: 01f116af0dbd6ccfead4ccd2a5a02d8a059f2ae59702e370d88a2ca9ac635141
3
+ metadata.gz: db17bf2caf28d2e2c9f8c610e0c882f53543af959af846e63f25dc4b9cf71d88
4
+ data.tar.gz: 28ae921a4fcfd86ab11c726238db69350341190fae32e897dc1c518562baec64
5
5
  SHA512:
6
- metadata.gz: b8510ccddad7b89fc23ffd13de253f1b40a1ba07b435adba23e3805f90518b42787bb73e832c34d449d43b81b92448c2c92ba48aaae766dec3f0997dffd60a9b
7
- data.tar.gz: 759d4c154c3362e981f9665aa65da1d350bcea2d82742e0624d5180abd5d66ef876ec134c78fac9cca2b3c3ea6af34ac2714e39c31822b0f4e22f94fe203b39a
6
+ metadata.gz: 5974cd83124ff35a036dce13e4800095f2a3c37923a6c568b5315140f44fb62390858545403ed52873a29ff432b7e785ed20c80c29e5fe467b75274ae40745ac
7
+ data.tar.gz: 548fc102b9aa3861efa4cb538845589f02ab32baefabd7821201cdb2992b01cbb07b90a75294a04032c036a70db2d06477880bb7a6f579ec228574607d893abe
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zenaton (0.1.1)
4
+ zenaton (0.2.0)
5
5
  activesupport
6
6
  httparty
7
7
  tzinfo-data
@@ -28,14 +28,10 @@ module Zenaton
28
28
  @decoded = []
29
29
  value = {}
30
30
  raise ArgumentError, 'Procs cannot be serialized' if data.is_a?(Proc)
31
- if data.is_a?(Array)
32
- value[KEY_ARRAY] = encode_array(data)
33
- elsif data.is_a?(Hash)
34
- value[KEY_ARRAY] = encode_hash(data)
35
- elsif basic_type?(data)
31
+ if basic_type?(data)
36
32
  value[KEY_DATA] = data
37
33
  else
38
- value[KEY_OBJECT] = encode_object(data)
34
+ value[KEY_OBJECT] = encode_to_store(data)
39
35
  end
40
36
  value[KEY_STORE] = @encoded
41
37
  value.to_json
@@ -53,17 +49,13 @@ module Zenaton
53
49
  return decode_enumerable(parsed_json[KEY_ARRAY])
54
50
  when KEY_OBJECT
55
51
  id = parsed_json[KEY_OBJECT][ID_PREFIX.length..-1].to_i
56
- return decode_object(id, @encoded[id])
52
+ return decode_from_store(id, @encoded[id])
57
53
  end
58
54
  end
59
55
  # rubocop:enable Metrics/MethodLength
60
56
 
61
57
  private
62
58
 
63
- def array_type?(data)
64
- data.is_a?(Array) || data.is_a?(Hash)
65
- end
66
-
67
59
  def basic_type?(data)
68
60
  data.is_a?(String) \
69
61
  || data.is_a?(Integer) \
@@ -73,82 +65,144 @@ module Zenaton
73
65
  || data.nil?
74
66
  end
75
67
 
76
- def encode_array(array)
77
- array.map { |elem| encode_value(elem) }
78
- end
79
-
80
- def encode_hash(hash)
81
- transform_values(hash) { |value| encode_value(value) }
82
- end
83
-
84
68
  def encode_value(value)
85
69
  raise ArgumentError, 'Procs cannot be serialized' if value.is_a?(Proc)
86
- if value.is_a?(Array)
87
- encode_array(value)
88
- elsif value.is_a?(Hash)
89
- encode_hash(value)
90
- elsif basic_type?(value)
70
+ if basic_type?(value)
91
71
  value
92
72
  else
93
- encode_object(value)
73
+ encode_to_store(value)
94
74
  end
95
75
  end
96
76
 
97
- def encode_object(object)
77
+ def encode_to_store(object)
98
78
  id = @decoded.index(object)
99
- unless id
100
- id = @decoded.length
101
- @decoded[id] = object
102
- @encoded[id] = {
103
- KEY_OBJECT_NAME => object.class.name,
104
- KEY_OBJECT_PROPERTIES => encode_hash(@properties.from(object))
105
- }
106
- end
79
+ return store_id(id) if id
80
+ store_and_encode(object)
81
+ end
82
+
83
+ def store_and_encode(object)
84
+ id = @decoded.length
85
+ @decoded[id] = object
86
+ @encoded[id] = encoded_object_by_type(object)
87
+ store_id(id)
88
+ end
89
+
90
+ def store_id(id)
107
91
  "#{ID_PREFIX}#{id}"
108
92
  end
109
93
 
110
- def object_id?(string)
111
- string.is_a?(String) \
112
- && string.start_with?(ID_PREFIX) \
113
- && string[ID_PREFIX.length..-1].to_i <= @encoded.length
94
+ def encoded_object_by_type(object)
95
+ case object
96
+ when Array
97
+ encode_array(object)
98
+ when Hash
99
+ encode_hash(object)
100
+ else
101
+ encode_object(object)
102
+ end
114
103
  end
115
104
 
116
- def decode_enumerable(enumerable)
117
- return decode_array(enumerable) if enumerable.is_a?(Array)
118
- return decode_hash(enumerable) if enumerable.is_a?(Hash)
119
- raise ArgumentError, 'Unknown type'
105
+ def encode_object(object)
106
+ {
107
+ KEY_OBJECT_NAME => object.class.name,
108
+ KEY_OBJECT_PROPERTIES => encode_legacy_hash(@properties.from(object))
109
+ }
110
+ end
111
+
112
+ def encode_array(array)
113
+ {
114
+ KEY_ARRAY => array.map(&method(:encode_value))
115
+ }
116
+ end
117
+
118
+ def encode_hash(hash)
119
+ {
120
+ KEY_ARRAY => transform_values(hash, &method(:encode_value))
121
+ }
120
122
  end
121
123
 
122
- def decode_array(array)
123
- array.map { |elem| decode_element(elem) }
124
+ def encode_legacy_hash(hash)
125
+ transform_values(hash, &method(:encode_value))
124
126
  end
125
127
 
126
- def decode_hash(hash)
127
- transform_values(hash) { |value| decode_element(value) }
128
+ def store_id?(string)
129
+ string.is_a?(String) \
130
+ && string.start_with?(ID_PREFIX) \
131
+ && string[ID_PREFIX.length..-1].to_i <= @encoded.length
128
132
  end
129
133
 
130
134
  # rubocop:disable Metrics/MethodLength
131
135
  def decode_element(value)
132
- if object_id?(value)
136
+ if store_id?(value)
133
137
  id = value[ID_PREFIX.length..-1].to_i
134
138
  encoded = @encoded[id]
135
- decode_object(id, encoded) if encoded.is_a?(Hash)
139
+ decode_from_store(id, encoded)
136
140
  elsif value.is_a?(Array)
137
- decode_array(value)
141
+ decode_legacy_array(value)
138
142
  elsif value.is_a?(Hash)
139
- decode_hash(value)
143
+ decode_legacy_hash(value)
140
144
  else
141
145
  value
142
146
  end
143
147
  end
144
148
  # rubocop:enable Metrics/MethodLength
145
149
 
146
- def decode_object(id, encoded_object)
150
+ def decode_enumerable(enumerable)
151
+ return decode_legacy_array(enumerable) if enumerable.is_a?(Array)
152
+ return decode_legacy_hash(enumerable) if enumerable.is_a?(Hash)
153
+ raise ArgumentError, 'Unknown type'
154
+ end
155
+
156
+ def decode_legacy_array(array)
157
+ array.map(&method(:decode_element))
158
+ end
159
+
160
+ def decode_legacy_hash(hash)
161
+ transform_values(hash, &method(:decode_element))
162
+ end
163
+
164
+ def decode_array(id, array)
165
+ @decoded[id] = object = []
166
+ object.concat(array.map(&method(:decode_element)))
167
+ end
168
+
169
+ def decode_hash(id, hash)
170
+ @decoded[id] = {}
171
+ hash.each do |key, value|
172
+ @decoded[id][key] = decode_element(value)
173
+ end
174
+ @decoded[id]
175
+ end
176
+
177
+ def decode_from_store(id, encoded)
147
178
  decoded = @decoded[id]
148
179
  return decoded if decoded
180
+ case encoded[KEY_ARRAY]
181
+ when Array
182
+ decode_array(id, encoded[KEY_ARRAY])
183
+ when Hash
184
+ decode_hash(id, encoded[KEY_ARRAY])
185
+ else
186
+ decode_object(id, encoded)
187
+ end
188
+ end
189
+
190
+ def decoded_object_by_type(id, encoded)
191
+ enumerable = encoded[KEY_ARRAY]
192
+ case enumerable
193
+ when Array
194
+ decode_array(id, enumerable)
195
+ when Hash
196
+ decode_hash(id, enumerable)
197
+ else
198
+ decode_object(id, encoded)
199
+ end
200
+ end
201
+
202
+ def decode_object(id, encoded_object)
149
203
  object = @properties.blank_instance(encoded_object[KEY_OBJECT_NAME])
150
204
  @decoded[id] = object
151
- properties = decode_hash(encoded_object[KEY_OBJECT_PROPERTIES])
205
+ properties = decode_legacy_hash(encoded_object[KEY_OBJECT_PROPERTIES])
152
206
  @properties.set(object, properties)
153
207
  end
154
208
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Zenaton
4
4
  # This gem's current version
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zenaton
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zenaton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-03 00:00:00.000000000 Z
11
+ date: 2018-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport