ishin 0.2.0 → 0.2.1

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: b5ed152cc96db8d638bde56aa627fc3fe4019038
4
- data.tar.gz: 052b32431e70c55e0fe3497ca2e586ad86c031aa
3
+ metadata.gz: 2d63099f6c8f19f6a16138fd55d0d5bb556c229a
4
+ data.tar.gz: 3681c9613c481488fbf5697521865990a0116481
5
5
  SHA512:
6
- metadata.gz: 087e9073b09d19c6001d386634165febbc9b9689343cdb00be535c3b2a0710b3862d3b8d79e7130efaa420e9c9fad4705c40f374ec50f09d406059428ef96f6c
7
- data.tar.gz: aa5cb190539f3883c00b36204c8d4da0b19f66ed9c65dbb377aee7f50325dc20a5c2d022f7e838e09026a11a174f5f04cbf707084c3b1e37192328bcdd4c5318
6
+ metadata.gz: 9f4692e1e48867a3b07e973861f7708b93f4053978a12f656b0a257442d1af6bb1cab4b47c398b360caec5c7aefb243b3b7c08d5d26dd23ca60eda65e4663382
7
+ data.tar.gz: 4258b470c8ae5efcd9b04636ef8ef0c62687a4aa39394b3b5157f750ef4204c729b71126821acc8374acd36cbca172accc2e787d6456b4a4f351a586e3404c95
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /ishin-*.gem
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 options = {}
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
- when Struct
17
- struct_to_hash(result, object, options)
18
- when Hash
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) unless options[:recursive]
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
- assign_value(result, key, value, options, new_options)
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 result, object, options
50
- new_options = decrement_recursion_depth options.clone
43
+ def self.struct_to_hash(result, object, options)
44
+ result.replace(object.to_h)
45
+ return unless should_recurse?(options)
51
46
 
52
- object.members.each do |member|
53
- key = options[:symbolize] ? member : member.to_s
54
- value = object[member]
55
- assign_value(result, key, value, options, new_options)
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
- assign_value(result, key, value, options, new_options)
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? options, value
71
- options[:recursive] && options[:recursion_depth] > 0 && !is_native_type?(value)
70
+ def self.should_recurse?(options)
71
+ options[:recursive] && options[:recursion_depth] > 0
72
72
  end
73
73
 
74
- def self.is_native_type? value
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
@@ -1,3 +1,3 @@
1
1
  module Ishin
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
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.0
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-25 00:00:00.000000000 Z
11
+ date: 2015-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake