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.
- checksums.yaml +4 -4
- data/lib/datastruct.rb +24 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8586b78a00a992565165d8302c2a05eab5e0efce
|
4
|
+
data.tar.gz: ba8064a0cc5803ca41de9ad332437e1dd4fad641
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
183
|
-
|
187
|
+
elsif obj.is_a? Hash
|
188
|
+
out = {}
|
184
189
|
|
185
|
-
|
186
|
-
|
187
|
-
|
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
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2016-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|