ishin 0.2.0 → 0.2.1
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/.gitignore +1 -0
- data/README.md +9 -0
- data/lib/ishin.rb +23 -23
- data/lib/ishin/version.rb +1 -1
- 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: 2d63099f6c8f19f6a16138fd55d0d5bb556c229a
|
4
|
+
data.tar.gz: 3681c9613c481488fbf5697521865990a0116481
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f4692e1e48867a3b07e973861f7708b93f4053978a12f656b0a257442d1af6bb1cab4b47c398b360caec5c7aefb243b3b7c08d5d26dd23ca60eda65e4663382
|
7
|
+
data.tar.gz: 4258b470c8ae5efcd9b04636ef8ef0c62687a4aa39394b3b5157f750ef4204c729b71126821acc8374acd36cbca172accc2e787d6456b4a4f351a586e3404c95
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -147,3 +147,12 @@ Your object now exposes a method named `to_hash` taking the same options at the
|
|
147
147
|
Once `bundle` is executed, simply run:
|
148
148
|
|
149
149
|
rake spec
|
150
|
+
|
151
|
+
## Changelog
|
152
|
+
|
153
|
+
* 0.1.0
|
154
|
+
* Initial release.
|
155
|
+
* 0.2.0
|
156
|
+
* Added `Ishin::Mixin`.
|
157
|
+
* 0.2.1
|
158
|
+
* Now using `Struct.to_h` when converting a `Struct` instance.
|
data/lib/ishin.rb
CHANGED
@@ -3,7 +3,7 @@ require 'ishin/version'
|
|
3
3
|
module Ishin
|
4
4
|
|
5
5
|
module Mixin
|
6
|
-
def to_hash
|
6
|
+
def to_hash(options = {})
|
7
7
|
Ishin::to_hash(self, options)
|
8
8
|
end
|
9
9
|
end
|
@@ -13,12 +13,9 @@ module Ishin
|
|
13
13
|
result = {}
|
14
14
|
|
15
15
|
case object
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
hash_to_hash(result, object, options)
|
20
|
-
else
|
21
|
-
object_to_hash(result, object, options)
|
16
|
+
when Struct then struct_to_hash(result, object, options)
|
17
|
+
when Hash then hash_to_hash(result, object, options)
|
18
|
+
else object_to_hash(result, object, options)
|
22
19
|
end
|
23
20
|
|
24
21
|
result
|
@@ -31,28 +28,26 @@ module Ishin
|
|
31
28
|
options
|
32
29
|
end
|
33
30
|
|
34
|
-
def self.assign_value(result, key, value, options, new_options)
|
35
|
-
result[key] = should_recurse?(options, value) ? to_hash(value, new_options) : value
|
36
|
-
end
|
37
|
-
|
38
31
|
def self.hash_to_hash(result, object, options)
|
39
|
-
return result.replace(object)
|
32
|
+
return result.replace(object) if !should_recurse?(options) && options[:symbolize]
|
40
33
|
|
41
34
|
new_options = decrement_recursion_depth(options.clone)
|
42
35
|
|
43
36
|
object.each do |key, value|
|
44
37
|
key = key.to_sym if options[:symbolize] && key.is_a?(String)
|
45
|
-
|
38
|
+
|
39
|
+
result[key] = is_native_type?(value) ? value : to_hash(value, new_options)
|
46
40
|
end
|
47
41
|
end
|
48
42
|
|
49
|
-
def self.struct_to_hash
|
50
|
-
|
43
|
+
def self.struct_to_hash(result, object, options)
|
44
|
+
result.replace(object.to_h)
|
45
|
+
return unless should_recurse?(options)
|
51
46
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
47
|
+
new_options = decrement_recursion_depth(options.clone)
|
48
|
+
|
49
|
+
result.each do |key, value|
|
50
|
+
result[key] = to_hash(value, new_options) unless is_native_type?(value)
|
56
51
|
end
|
57
52
|
end
|
58
53
|
|
@@ -63,15 +58,20 @@ module Ishin
|
|
63
58
|
value = object.instance_variable_get(var)
|
64
59
|
key = var.to_s.delete('@')
|
65
60
|
key = key.to_sym if options[:symbolize]
|
66
|
-
|
61
|
+
|
62
|
+
if should_recurse?(options) && !is_native_type?(value)
|
63
|
+
result[key] = to_hash(value, new_options)
|
64
|
+
else
|
65
|
+
result[key] = value
|
66
|
+
end
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
-
def self.should_recurse?
|
71
|
-
options[:recursive] && options[:recursion_depth] > 0
|
70
|
+
def self.should_recurse?(options)
|
71
|
+
options[:recursive] && options[:recursion_depth] > 0
|
72
72
|
end
|
73
73
|
|
74
|
-
def self.is_native_type?
|
74
|
+
def self.is_native_type?(value)
|
75
75
|
[ String, Numeric, TrueClass, FalseClass ].any? { |i| value.is_a?(i) }
|
76
76
|
end
|
77
77
|
|
data/lib/ishin/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ishin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eddy Luten
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|