resource-struct 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +25 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +61 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/resource-struct.rb +3 -0
- data/lib/resource_struct/firm_struct.rb +102 -0
- data/lib/resource_struct/loose_struct.rb +99 -0
- data/lib/resource_struct/version.rb +5 -0
- data/lib/resource_struct.rb +10 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 07376a21fe1fd7e877cdd8755e5a269e8336d1ac9906cf046e2aa39947938117
|
4
|
+
data.tar.gz: 070d252877489353d147fc93ee84d3ab0e74608f24d82d6d08163e9acd65c97f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b72654d78fbdb3df534c058a4e1df047f0ed8c40a063dbfc77ff869b8123d370f34277878354fe5d537802637fec52490d89374428e1bd5a937455bf59b4f0a1
|
7
|
+
data.tar.gz: c3673ddd8220bc52a130ccfd5a512dd1f03dd3c7bfcadde3d28b075ba6b2619f2d8bfa346c853fbe20103ae35630cc6f8a6224bd6d67335a0a31ec63a16abfea
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.7
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Max: 20
|
17
|
+
|
18
|
+
Naming/FileName:
|
19
|
+
Enabled: true
|
20
|
+
Exclude:
|
21
|
+
- lib/resource-struct.rb
|
22
|
+
|
23
|
+
Metrics/BlockLength:
|
24
|
+
Exclude:
|
25
|
+
- spec/**/*_spec.rb
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in resource-struct.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "rubocop", "~> 1.21"
|
13
|
+
gem "rubocop-rake"
|
14
|
+
gem "rubocop-rspec"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
resource-struct (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.2)
|
10
|
+
diff-lcs (1.4.4)
|
11
|
+
parallel (1.21.0)
|
12
|
+
parser (3.0.3.2)
|
13
|
+
ast (~> 2.4.1)
|
14
|
+
rainbow (3.0.0)
|
15
|
+
rake (13.0.6)
|
16
|
+
regexp_parser (2.2.0)
|
17
|
+
rexml (3.2.5)
|
18
|
+
rspec (3.10.0)
|
19
|
+
rspec-core (~> 3.10.0)
|
20
|
+
rspec-expectations (~> 3.10.0)
|
21
|
+
rspec-mocks (~> 3.10.0)
|
22
|
+
rspec-core (3.10.1)
|
23
|
+
rspec-support (~> 3.10.0)
|
24
|
+
rspec-expectations (3.10.1)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.10.0)
|
27
|
+
rspec-mocks (3.10.2)
|
28
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
29
|
+
rspec-support (~> 3.10.0)
|
30
|
+
rspec-support (3.10.3)
|
31
|
+
rubocop (1.23.0)
|
32
|
+
parallel (~> 1.10)
|
33
|
+
parser (>= 3.0.0.0)
|
34
|
+
rainbow (>= 2.2.2, < 4.0)
|
35
|
+
regexp_parser (>= 1.8, < 3.0)
|
36
|
+
rexml
|
37
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
38
|
+
ruby-progressbar (~> 1.7)
|
39
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
40
|
+
rubocop-ast (1.15.0)
|
41
|
+
parser (>= 3.0.1.1)
|
42
|
+
rubocop-rake (0.6.0)
|
43
|
+
rubocop (~> 1.0)
|
44
|
+
rubocop-rspec (2.6.0)
|
45
|
+
rubocop (~> 1.19)
|
46
|
+
ruby-progressbar (1.11.0)
|
47
|
+
unicode-display_width (2.1.0)
|
48
|
+
|
49
|
+
PLATFORMS
|
50
|
+
x86_64-darwin-18
|
51
|
+
|
52
|
+
DEPENDENCIES
|
53
|
+
rake (~> 13.0)
|
54
|
+
resource-struct!
|
55
|
+
rspec (~> 3.0)
|
56
|
+
rubocop (~> 1.21)
|
57
|
+
rubocop-rake
|
58
|
+
rubocop-rspec
|
59
|
+
|
60
|
+
BUNDLED WITH
|
61
|
+
2.2.33
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Alex Riedler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Resource::Struct
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/resource/struct`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'resource-struct'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install resource-struct
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/resource-struct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "resource/struct"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ResourceStruct
|
4
|
+
#
|
5
|
+
# FirmStruct provides a struct by which accessing undefined fields raises a MethodMissing error.
|
6
|
+
# This protects against accessing fields that are not present in API Responses.
|
7
|
+
#
|
8
|
+
# If you need to check whether a field exists in an api response, you can via name? methods.
|
9
|
+
#
|
10
|
+
# struct = FirmStruct.new({ "foo" => 1, "bar" => [{ "baz" => 2 }, 3] })
|
11
|
+
#
|
12
|
+
# struct.foo? # => true
|
13
|
+
# struct.brr? # => false
|
14
|
+
# struct.foo # => 1
|
15
|
+
# struct.bar # => [FirmStruct<{ "baz" => 2 }>, 3]
|
16
|
+
# struct.brr # => NoMethodError
|
17
|
+
# struct[:foo] # => 1
|
18
|
+
# struct[:brr] # => nil
|
19
|
+
# struct[:bar, 0, :baz] # => 2
|
20
|
+
# struct[:bar, 0, :brr] # => nil
|
21
|
+
#
|
22
|
+
class FirmStruct
|
23
|
+
def initialize(hash)
|
24
|
+
raise ::ArgumentError, "first argument must be a Hash, found #{hash.class.name}" unless hash.is_a?(Hash)
|
25
|
+
|
26
|
+
@hash = hash
|
27
|
+
@ro_struct = {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def method_missing(name, *args, &blk)
|
31
|
+
return self[name] if ___key?(name)
|
32
|
+
return !!self[___convert_key(name[...-1])] if name.end_with?("?")
|
33
|
+
|
34
|
+
super
|
35
|
+
end
|
36
|
+
|
37
|
+
def respond_to_missing?(name, include_private = false)
|
38
|
+
___key?(name) || name.end_with?("?") || super
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_h
|
42
|
+
@hash.to_h
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_hash
|
46
|
+
@hash.to_hash
|
47
|
+
end
|
48
|
+
|
49
|
+
def inspect
|
50
|
+
"#{self.class.name}<#{@hash.inspect}>"
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_s
|
54
|
+
@hash.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
def ==(other)
|
58
|
+
@hash == other.instance_variable_get(:@hash)
|
59
|
+
end
|
60
|
+
|
61
|
+
def [](key, *sub_keys)
|
62
|
+
ckey = ___convert_key(key)
|
63
|
+
|
64
|
+
result =
|
65
|
+
if @ro_struct.key?(ckey)
|
66
|
+
@ro_struct[ckey]
|
67
|
+
else
|
68
|
+
@ro_struct[ckey] = ___convert_value(@hash[ckey])
|
69
|
+
end
|
70
|
+
|
71
|
+
return result if sub_keys.empty?
|
72
|
+
|
73
|
+
return unless result
|
74
|
+
|
75
|
+
raise TypeError, "#{result.class.name} does not have #dig method" unless result.respond_to?(:dig)
|
76
|
+
|
77
|
+
result.dig(*sub_keys)
|
78
|
+
end
|
79
|
+
alias dig []
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def ___convert_value(value)
|
84
|
+
case value
|
85
|
+
when ::Array
|
86
|
+
value.map { |v| ___convert_value(v) }.freeze
|
87
|
+
when Hash
|
88
|
+
self.class.new(value)
|
89
|
+
else
|
90
|
+
value
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def ___key?(key)
|
95
|
+
@hash.key?(___convert_key(key))
|
96
|
+
end
|
97
|
+
|
98
|
+
def ___convert_key(key)
|
99
|
+
key.is_a?(::Symbol) ? key.to_s : key
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ResourceStruct
|
4
|
+
#
|
5
|
+
# LooseStruct provides a struct by which accessing undefined fields returns nil
|
6
|
+
#
|
7
|
+
# struct = LooseStruct.new({ "foo" => 1, "bar" => [{ "baz" => 2 }, 3] })
|
8
|
+
#
|
9
|
+
# struct.foo? # => true
|
10
|
+
# struct.brr? # => false
|
11
|
+
# struct.foo # => 1
|
12
|
+
# struct.bar # => [LooseStruct<{ "baz" => 2 }>, 3]
|
13
|
+
# struct.brr # => nil
|
14
|
+
# struct[:foo] # => 1
|
15
|
+
# struct[:brr] # => nil
|
16
|
+
# struct[:bar, 0, :baz] # => 2
|
17
|
+
# struct[:bar, 0, :brr] # => nil
|
18
|
+
#
|
19
|
+
class LooseStruct
|
20
|
+
def initialize(hash)
|
21
|
+
raise ::ArgumentError, "first argument must be a Hash, found #{hash.class.name}" unless hash.is_a?(Hash)
|
22
|
+
|
23
|
+
@hash = hash
|
24
|
+
@ro_struct = {}
|
25
|
+
end
|
26
|
+
|
27
|
+
def method_missing(name, *_args)
|
28
|
+
return self[name] if ___key?(name)
|
29
|
+
return !!self[___convert_key(name[...-1])] if name.end_with?("?")
|
30
|
+
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
34
|
+
def respond_to_missing?(_name, _include_private = false)
|
35
|
+
true
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_h
|
39
|
+
@hash.to_h
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_hash
|
43
|
+
@hash.to_hash
|
44
|
+
end
|
45
|
+
|
46
|
+
def inspect
|
47
|
+
"#{self.class.name}<#{@hash.inspect}>"
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_s
|
51
|
+
@hash.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
def ==(other)
|
55
|
+
@hash == other.instance_variable_get(:@hash)
|
56
|
+
end
|
57
|
+
|
58
|
+
def [](key, *sub_keys)
|
59
|
+
ckey = ___convert_key(key)
|
60
|
+
|
61
|
+
result =
|
62
|
+
if @ro_struct.key?(ckey)
|
63
|
+
@ro_struct[ckey]
|
64
|
+
else
|
65
|
+
@ro_struct[ckey] = ___convert_value(@hash[ckey])
|
66
|
+
end
|
67
|
+
|
68
|
+
return result if sub_keys.empty?
|
69
|
+
|
70
|
+
return unless result
|
71
|
+
|
72
|
+
raise TypeError, "#{result.class.name} does not have #dig method" unless result.respond_to?(:dig)
|
73
|
+
|
74
|
+
result.dig(*sub_keys)
|
75
|
+
end
|
76
|
+
alias dig []
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def ___convert_value(value)
|
81
|
+
case value
|
82
|
+
when ::Array
|
83
|
+
value.map { |v| ___convert_value(v) }.freeze
|
84
|
+
when Hash
|
85
|
+
self.class.new(value)
|
86
|
+
else
|
87
|
+
value
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def ___key?(key)
|
92
|
+
@hash.key?(___convert_key(key))
|
93
|
+
end
|
94
|
+
|
95
|
+
def ___convert_key(key)
|
96
|
+
key.is_a?(::Symbol) ? key.to_s : key
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resource-struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Riedler
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-16 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Openstruct like access without all the headaches of Hash method overrides
|
14
|
+
etc...
|
15
|
+
email:
|
16
|
+
- alex@riedler.ca
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- CHANGELOG.md
|
24
|
+
- Gemfile
|
25
|
+
- Gemfile.lock
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- bin/console
|
30
|
+
- bin/setup
|
31
|
+
- lib/resource-struct.rb
|
32
|
+
- lib/resource_struct.rb
|
33
|
+
- lib/resource_struct/firm_struct.rb
|
34
|
+
- lib/resource_struct/loose_struct.rb
|
35
|
+
- lib/resource_struct/version.rb
|
36
|
+
homepage: https://github.com/AlexRiedler/resource-struct
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata:
|
40
|
+
homepage_uri: https://github.com/AlexRiedler/resource-struct
|
41
|
+
source_code_uri: https://github.com/AlexRiedler/resource-struct
|
42
|
+
changelog_uri: https://raw.githubusercontent.com/AlexRiedler/resource-struct/master/CHANGELOG.md
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: 2.7.0
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubygems_version: 3.1.4
|
59
|
+
signing_key:
|
60
|
+
specification_version: 4
|
61
|
+
summary: Ruby structs for resource responses
|
62
|
+
test_files: []
|