resource-struct 0.3.0 → 0.4.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: 1c1790e36a2600fddc519f1f9fdd47a2bdf9f07003f7205f0584be5455867446
4
- data.tar.gz: f916a4b4a6bb2c27d727d9914732c5a33ce35617246814bfbe8e9743150c89c1
3
+ metadata.gz: 81f9dfcc67f4c1670776860dd53c7d0885eb1b1d53ca7b9bae7202c782cbad85
4
+ data.tar.gz: ae36515a1e79be8653e2b4497dee9ab5f8d522e740cfcafe93681d946a5e528e
5
5
  SHA512:
6
- metadata.gz: c2db5d94ea2c816e8bb7a2279709380e43f6a7f0a1f1e7ceb3c027e1f07d5b5ffc308014eeef7ef44bd815ca8f9d9a43c46540af890a60c8f515e2e477536841
7
- data.tar.gz: eb0426c1c8667be29487125496681cba12c2b25fabc68475675500ea997aec898639cd826ce2370f888f9d0ffca9427ad6e348de3713eeb9d67bed3e1b350605
6
+ metadata.gz: 835d23cd09aa4a26cce6cf30378e749f5eee0a9439501fd1120ee3ac6f77f6adaed05ea688632e23dce09dd7bee8616859176fbc135b2d027c388aa3eec10e1b
7
+ data.tar.gz: 102bada15cf4151340c2fd95c5927335e19ec9bd637a2e74ba3b1e2193ead54a3a3a5af8d451caf364f0465caf5663079eeeaf72e3b7c7acc7b1ef7c847e3f95
data/.rubocop.yml CHANGED
@@ -16,7 +16,7 @@ Metrics/MethodLength:
16
16
  Max: 20
17
17
 
18
18
  Metrics/AbcSize:
19
- Max: 25
19
+ Max: 30
20
20
 
21
21
  Metrics/CyclomaticComplexity:
22
22
  Max: 15
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2022-01-09
4
+ ### Feature
5
+ - Better support for `ArgumentError` on `FlexStruct`
6
+
7
+ ## [0.3.3] - 2022-01-05
8
+ ### Fix
9
+ - Support false values correctly, instead of returning nil
10
+ - Support `respond_to?` properly on `FlexStruct`
11
+
12
+ ## [0.3.2] - 2022-01-05
13
+ ### Fix
14
+ - Support for proper marshalling of object
15
+
16
+ ## [0.3.1] - 2022-01-05
17
+ ### Fix
18
+ - Support nil meaning empty hash as first argument to `FlexStruct` and `StrictStruct`
19
+
3
20
  ## [0.3.0] - 2022-01-04
4
21
  ### Feature
5
22
  - Support for `as_json` and `to_json`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- resource-struct (0.3.0)
4
+ resource-struct (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -16,7 +16,7 @@ module ResourceStruct
16
16
  @hash = hash || {}
17
17
  @ro_struct = {}
18
18
 
19
- raise ::ArgumentError, "first argument must be a Hash, found #{hash.class.name}" unless hash.is_a?(Hash)
19
+ raise ::ArgumentError, "first argument must be a Hash, found #{@hash.class.name}" unless @hash.is_a?(Hash)
20
20
  end
21
21
 
22
22
  def inspect
@@ -35,10 +35,12 @@ module ResourceStruct
35
35
  result =
36
36
  if @ro_struct.key?(ckey)
37
37
  @ro_struct[ckey]
38
- elsif key.is_a?(String)
39
- @ro_struct[ckey] = ___convert_value(@hash[key] || @hash[key.to_sym])
40
- else
41
- @ro_struct[ckey] = ___convert_value(@hash[key] || @hash[ckey])
38
+ elsif @hash.key?(key)
39
+ @ro_struct[ckey] = ___convert_value(@hash[key])
40
+ elsif key.is_a?(String) && @hash.key?(key.to_sym)
41
+ @ro_struct[ckey] = ___convert_value(@hash[key.to_sym])
42
+ elsif key.is_a?(Symbol) && @hash.key?(key.to_s)
43
+ @ro_struct[ckey] = ___convert_value(@hash[key.to_s])
42
44
  end
43
45
 
44
46
  return result if sub_keys.empty?
@@ -51,6 +53,17 @@ module ResourceStruct
51
53
  end
52
54
  alias [] dig
53
55
 
56
+ def marshal_dump
57
+ {
58
+ data: @hash
59
+ }
60
+ end
61
+
62
+ def marshal_load(obj)
63
+ @ro_struct = {}
64
+ @hash = obj[:data]
65
+ end
66
+
54
67
  private
55
68
 
56
69
  def ___convert_value(value)
@@ -65,7 +78,9 @@ module ResourceStruct
65
78
  end
66
79
 
67
80
  def ___key?(key)
68
- @hash.key?(key) || @hash.key?(___convert_key(key))
81
+ @hash.key?(key) ||
82
+ @hash.key?(___convert_key(key)) ||
83
+ key.is_a?(String) && @hash.key?(key.to_sym)
69
84
  end
70
85
 
71
86
  def ___convert_key(key)
@@ -27,24 +27,31 @@ module ResourceStruct
27
27
 
28
28
  if @hash.key?(key)
29
29
  @hash[key] = value
30
- elsif key.is_a?(String) || key.is_a?(Symbol) && @hash.key?(key.to_sym)
30
+ elsif key.is_a?(String) && @hash.key?(key.to_sym)
31
31
  @hash[key.to_sym] = value
32
+ elsif key.is_a?(Symbol) && @hash.key?(key.to_s)
33
+ @hash[key.to_s] = value
32
34
  else
33
35
  @hash[key] = value
34
36
  end
35
37
  end
36
38
 
37
39
  def method_missing(name, *args)
38
- args_length = args.length
39
- return self[name] if ___key?(name) && args_length.zero?
40
- return !!self[name[...-1]] if name.end_with?("?") && args_length.zero?
41
- return self[name[...-1]] = args.first if name.end_with?("=") && args_length == 1
40
+ if name.end_with?("=")
41
+ return self[name[...-1]] = args.first if args.length == 1
42
42
 
43
- nil
43
+ raise ArgumentError, "expected 1 argument received #{args.length} arguments"
44
+ elsif name.end_with?("?")
45
+ return !!self[name[...-1]] if args.empty?
46
+ elsif args.empty?
47
+ return self[name]
48
+ end
49
+
50
+ raise ArgumentError, "expected 0 arguments received #{args.length} arguments"
44
51
  end
45
52
 
46
- def respond_to_missing?(_name, _include_private = false)
47
- true
53
+ def respond_to_missing?(name, include_private = false)
54
+ ___key?(name) || ___key?(name.to_s.chomp("?")) || super
48
55
  end
49
56
  end
50
57
  LooseStruct = FlexStruct
@@ -31,7 +31,7 @@ module ResourceStruct
31
31
  end
32
32
 
33
33
  def respond_to_missing?(name, include_private = false)
34
- ___key?(name) || name.end_with?("?") || super
34
+ ___key?(name) || ___key?(name.to_s.chomp("?")) || super
35
35
  end
36
36
  end
37
37
  FirmStruct = StrictStruct
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ResourceStruct
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.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.3.0
4
+ version: 0.4.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: 2022-01-05 00:00:00.000000000 Z
11
+ date: 2022-01-10 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...