nested_ostruct 0.0.1 → 1.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fc845a4179005cf5dad00ff8a6213a5db89fa5a
4
- data.tar.gz: 74df1fbd3276445bc19257629ca94b5f93454761
3
+ metadata.gz: c0d7a6577bae2933f0c3ef94b7bf30deb4da32fd
4
+ data.tar.gz: 8664d8f2934defa05dc8fed95e3d4f246ac3114c
5
5
  SHA512:
6
- metadata.gz: 09f4be98b3bec2913bdfce4d6bed4cdcea4b861c20d80be0276b08da45acb39123703251389cfba7f72eb42e9da22de11463c4f176c1652b6c7242f85c2fb033
7
- data.tar.gz: e7282222fa845f9f204b1d5cdbb72d7bab9d5b86c85234c9fd228168dc239f41a6761a32cac006ae4e26cce0c49413a334eea3d51d119a5018984aba9b27be64
6
+ metadata.gz: 51c1153e035d004fe5806e20f9820dc512626f00a2fce80594de22eafa7cbd0c28b27edbf9eec237964112373c27f88cbaa989fc5de930404c7fffe0d9ae0d41
7
+ data.tar.gz: ed1f5572e5b199ea9939f77a1ae0f1da0fbc4bf83e852f4ecbe7442bebb8ee750996639a5286a2127f54ef7e8d6c44b6545216ebd9fc3191e68c33f1f217879a
@@ -1,16 +1,58 @@
1
1
  require 'ostruct'
2
2
 
3
- module NestedOstruct
4
- extend self
3
+ class NestedOstruct < OpenStruct
5
4
 
6
- def to_hash(object)
7
- object = object.to_h if object.is_a?(OpenStruct)
5
+ # removing Object#hash to prevent namespace collision with arg hash key "hash"
6
+ undef :hash
7
+
8
+
9
+
10
+ # use variable to store :initialize
11
+ old_initialize = instance_method(:initialize)
12
+
13
+ # Nested ostruct from hash. Does not convert hashes within arrays; for that use gem: recursive-open-struct
14
+ #
15
+ # @see https://github.com/aetherknight/recursive-open-struct
16
+ # @see https://github.com/RailsApps/rails-stripe-checkout/pull/1/files#diff-cfaad7bb2a17389cf6b4d9a2a734f3d9R12
17
+ #
18
+ # @param hash [Hash]
19
+ # @return [NestedOstruct]
20
+ define_method(:initialize) do |hash|
21
+ old_initialize.bind(self).(hash.inject({}){|r,p| r[p[0]] = p[1].kind_of?(Hash) ? NestedOstruct.new(p[1]) : p[1]; r })
22
+ end
23
+
24
+
25
+
26
+ # alias and remove super method
27
+ alias_method :old_to_h, :to_h
28
+ undef :to_h
29
+
30
+ # redefine :to_h to use same public class method :parse
31
+ #
32
+ # @return [Hash]
33
+ def to_h
34
+ NestedOstruct.parse(self)
35
+ end
36
+
37
+
38
+
39
+ # OpenStruct to Hash
40
+ #
41
+ # @param object [OpenStruct]
42
+ # @return [Hash]
43
+ def self.parse(object)
44
+ case
45
+ when object.is_a?(NestedOstruct)
46
+ object = object.old_to_h
47
+ when object.is_a?(OpenStruct)
48
+ object = object.to_h
49
+ end
8
50
 
9
51
  case object.class.to_s
10
52
  when /Hash/
11
53
  object.each do |key, value|
12
- if [Hash, Array, OpenStruct].include?(value.class)
13
- object[key] = to_hash(value)
54
+ if [Hash, Array, OpenStruct, NestedOstruct].include?(value.class)
55
+ object[key] = NestedOstruct.parse(value)
14
56
  else
15
57
  object[key] = value
16
58
  end
@@ -18,8 +60,8 @@ module NestedOstruct
18
60
 
19
61
  when /Array/
20
62
  object.each_with_index do |value, i|
21
- if [Hash, Array, OpenStruct].include?(value.class)
22
- object[i] = to_hash(value)
63
+ if [Hash, Array, OpenStruct, NestedOstruct].include?(value.class)
64
+ object[i] = NestedOstruct.parse(value)
23
65
  else
24
66
  object[i] = value
25
67
  end
@@ -28,4 +70,5 @@ module NestedOstruct
28
70
 
29
71
  object
30
72
  end
73
+
31
74
  end
@@ -1,3 +1,3 @@
1
1
  module NestedOstruct
2
- VERSION = '0.0.1'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -7,12 +7,12 @@ class TestNestedOstruct < Minitest::Test
7
7
  skip "write tests or Zen Spider will kneecap you"
8
8
  end
9
9
 
10
- def test_call_to_hash
10
+ def setup
11
11
  hash_in_array = [{:totally => 'awesome!'}]
12
12
 
13
13
  nest_2 = OpenStruct.new
14
14
  nest_2.three = 'awesome'
15
- nest_2.four = 'rad'
15
+ nest_2.four = 'beer'
16
16
  nest_2.array = [5,[6,7,[8,9]]]
17
17
 
18
18
  nest_1 = OpenStruct.new
@@ -21,22 +21,51 @@ class TestNestedOstruct < Minitest::Test
21
21
  nest_1.array = hash_in_array
22
22
  nest_1.hash = nest_2
23
23
 
24
- obj = OpenStruct.new
25
- obj.array = [1,2,3,4]
26
- obj.hash = nest_1
27
- obj.name = :rick_hunter
24
+ @obj = OpenStruct.new
25
+ @obj.array = [1,2,3,4]
26
+ @obj.hash = nest_1
27
+ @obj.name = :rick_hunter
28
28
 
29
- act = NestedOstruct.to_hash(obj)
29
+ @hash_1 = {
30
+ :array =>[1, 2, 3, 4],
31
+ :hash => {
32
+ :one => "awesome",
33
+ :two => "rad",
34
+ :array => [{:totally => "awesome!"}],
35
+ :hash => {
36
+ :three => "awesome",
37
+ :four => "beer",
38
+ :array => [5, [6, 7, [8, 9]]]
39
+ }
40
+ },
41
+ :name => :rick_hunter
42
+ }
43
+ end
44
+
45
+ def test_parse
46
+ act = NestedOstruct.parse(@obj)
30
47
  exp = Hash
31
48
  assert_kind_of exp, act
32
49
 
33
- exp = {:array =>[1, 2, 3, 4],
34
- :hash =>
35
- {:one => "awesome",
36
- :two => "rad",
37
- :array => [{:totally => "awesome!"}],
38
- :hash => {:three => "awesome", :four => "rad", :array => [5, [6, 7, [8, 9]]]}},
39
- :name => :rick_hunter}
40
- assert_equal exp, act
50
+ assert_equal @hash_1, act
51
+ end
52
+
53
+ def test_new
54
+ act = NestedOstruct.new(@hash_1)
55
+ exp = NestedOstruct
56
+ assert_kind_of exp, act
57
+
58
+ assert_equal [1,2,3,4], act.array
59
+ assert_kind_of NestedOstruct, act.hash
60
+ assert_equal "awesome", act.hash.one
61
+ assert_equal "rad", act.hash.two
62
+ assert_equal [{:totally => "awesome!"}], act.hash.array
63
+ assert_kind_of NestedOstruct, act.hash.hash
64
+ assert_equal "beer", act.hash.hash.four
65
+ assert_kind_of Array, act.hash.hash.array
66
+ end
67
+
68
+ def test_to_h
69
+ assert_equal @hash_1, NestedOstruct.parse(@obj).to_h
41
70
  end
42
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested_ostruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - So Awesome Man
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-20 00:00:00.000000000 Z
11
+ date: 2017-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest