resource-struct 0.1.0 → 0.2.0

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
  SHA256:
3
- metadata.gz: 07376a21fe1fd7e877cdd8755e5a269e8336d1ac9906cf046e2aa39947938117
4
- data.tar.gz: 070d252877489353d147fc93ee84d3ab0e74608f24d82d6d08163e9acd65c97f
3
+ metadata.gz: f9513a73bc3c6331f8174bd02c30c72fcbc655fb2e00c8d88cb1cd80fa3c60ae
4
+ data.tar.gz: 3d1355e235d443b74098434f453a482d090790a25f57c65ce8a57f931813863a
5
5
  SHA512:
6
- metadata.gz: b72654d78fbdb3df534c058a4e1df047f0ed8c40a063dbfc77ff869b8123d370f34277878354fe5d537802637fec52490d89374428e1bd5a937455bf59b4f0a1
7
- data.tar.gz: c3673ddd8220bc52a130ccfd5a512dd1f03dd3c7bfcadde3d28b075ba6b2619f2d8bfa346c853fbe20103ae35630cc6f8a6224bd6d67335a0a31ec63a16abfea
6
+ metadata.gz: 88f21e99bce0cf3620cf0644f240176af22bd64f99a51781c293cd407467062cbe784d50274115e4214443801f34d10c79858a27910e9bf54556356e5e95d753
7
+ data.tar.gz: bfa0b4576f987a5a5a7d39d62abcc7ac4f00fd6f2d27c8bb20d124fbf092914bab0212c8c4987451a86d47e3c06d31ab3452c777f0f25cb29a128e6ca048e3cf
data/.rubocop.yml CHANGED
@@ -15,6 +15,15 @@ Layout/LineLength:
15
15
  Metrics/MethodLength:
16
16
  Max: 20
17
17
 
18
+ Metrics/AbcSize:
19
+ Max: 25
20
+
21
+ Metrics/CyclomaticComplexity:
22
+ Max: 15
23
+
24
+ Metrics/PerceivedComplexity:
25
+ Max: 15
26
+
18
27
  Naming/FileName:
19
28
  Enabled: true
20
29
  Exclude:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2022-01-01
4
+ ### Changed
5
+ - Indifferent access support for input hash (support for symbol-based hashes in initializer)
6
+
3
7
  ## [0.1.0] - 2021-12-14
4
8
 
5
9
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- resource-struct (0.1.0)
4
+ resource-struct (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -48,6 +48,7 @@ GEM
48
48
 
49
49
  PLATFORMS
50
50
  x86_64-darwin-18
51
+ x86_64-darwin-19
51
52
 
52
53
  DEPENDENCIES
53
54
  rake (~> 13.0)
@@ -58,4 +59,4 @@ DEPENDENCIES
58
59
  rubocop-rspec
59
60
 
60
61
  BUNDLED WITH
61
- 2.2.33
62
+ 2.3.4
@@ -29,7 +29,7 @@ module ResourceStruct
29
29
 
30
30
  def method_missing(name, *args, &blk)
31
31
  return self[name] if ___key?(name)
32
- return !!self[___convert_key(name[...-1])] if name.end_with?("?")
32
+ return !!self[name[...-1]] if name.end_with?("?")
33
33
 
34
34
  super
35
35
  end
@@ -55,17 +55,20 @@ module ResourceStruct
55
55
  end
56
56
 
57
57
  def ==(other)
58
- @hash == other.instance_variable_get(:@hash)
58
+ other.is_a?(Hash) && ___all_keys_equal(other) ||
59
+ (other.is_a?(LooseStruct) || other.is_a?(FirmStruct)) && ___all_keys_equal(other.instance_variable_get(:@hash))
59
60
  end
60
61
 
61
- def [](key, *sub_keys)
62
+ def dig(key, *sub_keys)
62
63
  ckey = ___convert_key(key)
63
64
 
64
65
  result =
65
66
  if @ro_struct.key?(ckey)
66
67
  @ro_struct[ckey]
68
+ elsif key.is_a?(String)
69
+ @ro_struct[ckey] = ___convert_value(@hash[key] || @hash[key.to_sym])
67
70
  else
68
- @ro_struct[ckey] = ___convert_value(@hash[ckey])
71
+ @ro_struct[ckey] = ___convert_value(@hash[key] || @hash[ckey])
69
72
  end
70
73
 
71
74
  return result if sub_keys.empty?
@@ -76,7 +79,7 @@ module ResourceStruct
76
79
 
77
80
  result.dig(*sub_keys)
78
81
  end
79
- alias dig []
82
+ alias [] dig
80
83
 
81
84
  private
82
85
 
@@ -92,11 +95,24 @@ module ResourceStruct
92
95
  end
93
96
 
94
97
  def ___key?(key)
95
- @hash.key?(___convert_key(key))
98
+ @hash.key?(key) || @hash.key?(___convert_key(key))
96
99
  end
97
100
 
98
101
  def ___convert_key(key)
99
102
  key.is_a?(::Symbol) ? key.to_s : key
100
103
  end
104
+
105
+ def ___all_keys_equal(other)
106
+ return false unless @hash.count == other.count
107
+
108
+ @hash.reduce(true) do |acc, (k, v)|
109
+ if other.key?(k)
110
+ acc && other[k] == v
111
+ else
112
+ ck = ___convert_key(k)
113
+ acc && other.key?(ck) && other[ck] == v
114
+ end
115
+ end
116
+ end
101
117
  end
102
118
  end
@@ -26,7 +26,7 @@ module ResourceStruct
26
26
 
27
27
  def method_missing(name, *_args)
28
28
  return self[name] if ___key?(name)
29
- return !!self[___convert_key(name[...-1])] if name.end_with?("?")
29
+ return !!self[name[...-1]] if name.end_with?("?")
30
30
 
31
31
  nil
32
32
  end
@@ -52,17 +52,20 @@ module ResourceStruct
52
52
  end
53
53
 
54
54
  def ==(other)
55
- @hash == other.instance_variable_get(:@hash)
55
+ other.is_a?(Hash) && ___all_keys_equal(other) ||
56
+ (other.is_a?(LooseStruct) || other.is_a?(FirmStruct)) && ___all_keys_equal(other.instance_variable_get(:@hash))
56
57
  end
57
58
 
58
- def [](key, *sub_keys)
59
+ def dig(key, *sub_keys)
59
60
  ckey = ___convert_key(key)
60
61
 
61
62
  result =
62
63
  if @ro_struct.key?(ckey)
63
64
  @ro_struct[ckey]
65
+ elsif key.is_a?(String)
66
+ @ro_struct[ckey] = ___convert_value(@hash[key] || @hash[key.to_sym])
64
67
  else
65
- @ro_struct[ckey] = ___convert_value(@hash[ckey])
68
+ @ro_struct[ckey] = ___convert_value(@hash[key] || @hash[ckey])
66
69
  end
67
70
 
68
71
  return result if sub_keys.empty?
@@ -73,7 +76,7 @@ module ResourceStruct
73
76
 
74
77
  result.dig(*sub_keys)
75
78
  end
76
- alias dig []
79
+ alias [] dig
77
80
 
78
81
  private
79
82
 
@@ -89,11 +92,24 @@ module ResourceStruct
89
92
  end
90
93
 
91
94
  def ___key?(key)
92
- @hash.key?(___convert_key(key))
95
+ @hash.key?(key) || @hash.key?(___convert_key(key))
93
96
  end
94
97
 
95
98
  def ___convert_key(key)
96
99
  key.is_a?(::Symbol) ? key.to_s : key
97
100
  end
101
+
102
+ def ___all_keys_equal(other)
103
+ return false unless @hash.count == other.count
104
+
105
+ @hash.reduce(true) do |acc, (k, v)|
106
+ if other.key?(k)
107
+ acc && other[k] == v
108
+ else
109
+ ck = ___convert_key(k)
110
+ acc && other.key?(ck) && other[ck] == v
111
+ end
112
+ end
113
+ end
98
114
  end
99
115
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ResourceStruct
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resource-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Riedler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-12-16 00:00:00.000000000 Z
11
+ date: 2022-01-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Openstruct like access without all the headaches of Hash method overrides
14
14
  etc...
@@ -55,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  requirements: []
58
- rubygems_version: 3.1.4
58
+ rubygems_version: 3.1.6
59
59
  signing_key:
60
60
  specification_version: 4
61
61
  summary: Ruby structs for resource responses