datastruct 0.0.6 → 0.0.7

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. checksums.yaml +4 -4
  2. data/lib/datastruct.rb +24 -8
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d87f5724b107fd84295b0128e69c50902894204c
4
- data.tar.gz: bec01b7ffc717fde3401eefe4890f7278e484d78
3
+ metadata.gz: 8586b78a00a992565165d8302c2a05eab5e0efce
4
+ data.tar.gz: ba8064a0cc5803ca41de9ad332437e1dd4fad641
5
5
  SHA512:
6
- metadata.gz: 4e449b0ff8418176f11d5699e13de2a9c6b34facf2abb012b4ec44b697657d763939703588e91af9431a4c44d2759c17d56e650943543cdf6257e162b3575403
7
- data.tar.gz: f68a909dba3b9f79a05e70016c1793534f7798a612b38ce6b25c69f24b23d8c41fe823645bf5849cc36a5ea48b216a8c613ffa53c980870dabef7dff82ee2b29
6
+ metadata.gz: 3e6fff90d7f9a74ab5155367cb78ecb72ff5c4789e014cb9b4d558b846edc360fa269d071f998b0ebab09ef5222ac7e097f3e786f1149c6f8761719d2fa1261e
7
+ data.tar.gz: 69af720cb5c40f90e03441bfc7a1226cc46f079d4ee0783d0ab575e1575d1918ae4e881d95be079c7482ed2e1994cbad2ff0bf0bee90164fe2f89237fc8b6de2
data/lib/datastruct.rb CHANGED
@@ -172,24 +172,40 @@ class DataStruct
172
172
  ##
173
173
  # Returns the properties of the object as a hash
174
174
  #
175
+ # @param [Boolean] string_keys Return keys as strings instead of symbols
176
+ # @param [Boolean] deep Go deep and serialize all DataStruct instances found
177
+ #
175
178
  # @return [Hash]
176
179
  #
177
180
  def to_hash(string_keys: false, deep: false)
178
181
  return @data.dup if not string_keys and not deep
179
182
 
180
- out = {}
183
+ hashify = lambda { |obj|
184
+ if obj.is_a? DataStruct and deep
185
+ return obj.to_hash(string_keys: string_keys, deep: deep)
181
186
 
182
- @data.each_pair { |key, value|
183
- key = key.to_s if string_keys
187
+ elsif obj.is_a? Hash
188
+ out = {}
184
189
 
185
- if deep and value.is_a? DataStruct
186
- value = value.to_hash(string_keys: string_keys, deep: true)
187
- end
190
+ obj.each_pair { |key, value|
191
+ # Only stringify the keys of the *top level* of the structure. We have
192
+ # no business messing with the keys of nested hashes.
193
+ key = key.to_s if string_keys and obj.equal? @data
194
+
195
+ out[key] = hashify[value]
196
+ }
197
+
198
+ return out
188
199
 
189
- out[key] = value
200
+ elsif obj.is_a? Array
201
+ return obj.map { |x| hashify[x] }
202
+
203
+ else
204
+ return obj
205
+ end
190
206
  }
191
207
 
192
- return out
208
+ return hashify[@data]
193
209
  end
194
210
 
195
211
  alias_method :to_h, :to_hash
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datastruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Sandven
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-16 00:00:00.000000000 Z
11
+ date: 2016-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake