hasharay_ext 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/.standard.yml +2 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +82 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/hasharay_ext/version.rb +5 -0
- data/lib/hasharay_ext.rb +97 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2ec145e647348b3f4801babbf82f8095c5f45c118a7ec6d1e7a702956a5a0096
|
4
|
+
data.tar.gz: 6f2a3c60af66104b3ae818140d42aa5a079cf663255b6078b4da370ea8960d19
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6c0f24289287d1d9aae0571e6a9383b1c0dc0449f421a0db0a60bd72f9ea52e8b248410e6b5babb86f1a718135541e0b644e3cc2b1ebcec324ed7d941775cdef
|
7
|
+
data.tar.gz: 7ad2a1d7e3db97d45ab1ba74b8cf29f7c587fd638a3f634966c243144d9e9e4e25df42e73975d34398545554649f5545f024533d6f6cfa56a6354ff48ea409ca
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in hasharay_ext.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "activesupport"
|
9
|
+
|
10
|
+
gem "rake", "~> 13.0"
|
11
|
+
|
12
|
+
gem "rspec", "~> 3.0"
|
13
|
+
|
14
|
+
gem "standard", "~> 1.3"
|
15
|
+
|
16
|
+
gem "pry"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hasharay_ext (0.1.0)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (6.1.4.4)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 1.6, < 2)
|
13
|
+
minitest (>= 5.1)
|
14
|
+
tzinfo (~> 2.0)
|
15
|
+
zeitwerk (~> 2.3)
|
16
|
+
ast (2.4.2)
|
17
|
+
coderay (1.1.3)
|
18
|
+
concurrent-ruby (1.1.9)
|
19
|
+
diff-lcs (1.5.0)
|
20
|
+
i18n (1.9.1)
|
21
|
+
concurrent-ruby (~> 1.0)
|
22
|
+
method_source (1.0.0)
|
23
|
+
minitest (5.15.0)
|
24
|
+
parallel (1.21.0)
|
25
|
+
parser (3.1.0.0)
|
26
|
+
ast (~> 2.4.1)
|
27
|
+
pry (0.14.1)
|
28
|
+
coderay (~> 1.1)
|
29
|
+
method_source (~> 1.0)
|
30
|
+
rainbow (3.1.1)
|
31
|
+
rake (13.0.6)
|
32
|
+
regexp_parser (2.2.0)
|
33
|
+
rexml (3.2.5)
|
34
|
+
rspec (3.10.0)
|
35
|
+
rspec-core (~> 3.10.0)
|
36
|
+
rspec-expectations (~> 3.10.0)
|
37
|
+
rspec-mocks (~> 3.10.0)
|
38
|
+
rspec-core (3.10.2)
|
39
|
+
rspec-support (~> 3.10.0)
|
40
|
+
rspec-expectations (3.10.2)
|
41
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
42
|
+
rspec-support (~> 3.10.0)
|
43
|
+
rspec-mocks (3.10.3)
|
44
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
45
|
+
rspec-support (~> 3.10.0)
|
46
|
+
rspec-support (3.10.3)
|
47
|
+
rubocop (1.25.0)
|
48
|
+
parallel (~> 1.10)
|
49
|
+
parser (>= 3.1.0.0)
|
50
|
+
rainbow (>= 2.2.2, < 4.0)
|
51
|
+
regexp_parser (>= 1.8, < 3.0)
|
52
|
+
rexml
|
53
|
+
rubocop-ast (>= 1.15.1, < 2.0)
|
54
|
+
ruby-progressbar (~> 1.7)
|
55
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
56
|
+
rubocop-ast (1.15.1)
|
57
|
+
parser (>= 3.0.1.1)
|
58
|
+
rubocop-performance (1.13.2)
|
59
|
+
rubocop (>= 1.7.0, < 2.0)
|
60
|
+
rubocop-ast (>= 0.4.0)
|
61
|
+
ruby-progressbar (1.11.0)
|
62
|
+
standard (1.7.0)
|
63
|
+
rubocop (= 1.25.0)
|
64
|
+
rubocop-performance (= 1.13.2)
|
65
|
+
tzinfo (2.0.4)
|
66
|
+
concurrent-ruby (~> 1.0)
|
67
|
+
unicode-display_width (2.1.0)
|
68
|
+
zeitwerk (2.5.4)
|
69
|
+
|
70
|
+
PLATFORMS
|
71
|
+
x86_64-linux
|
72
|
+
|
73
|
+
DEPENDENCIES
|
74
|
+
activesupport
|
75
|
+
hasharay_ext!
|
76
|
+
pry
|
77
|
+
rake (~> 13.0)
|
78
|
+
rspec (~> 3.0)
|
79
|
+
standard (~> 1.3)
|
80
|
+
|
81
|
+
BUNDLED WITH
|
82
|
+
2.3.3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Igor Kasyanchuk
|
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,117 @@
|
|
1
|
+
# Hash/Array Extenstion
|
2
|
+
|
3
|
+
The idea of this gem is to simplify fetching records from the Hash/Array.
|
4
|
+
I had a need to read values from the complex hashes, and I was tired of doing lots of "dig/fetch". Code was just ugly.
|
5
|
+
|
6
|
+
So, initial idea was to create a way to "query" Hash/Array similiar how we are querying CSS, but later idea was changed a little.
|
7
|
+
|
8
|
+
Right now I've extracted my code into this gem and now can do the following:
|
9
|
+
|
10
|
+
Instead of:
|
11
|
+
```ruby
|
12
|
+
h.fetch(:projects, []).map{|e| e[:name]}
|
13
|
+
```
|
14
|
+
I can just write:
|
15
|
+
```ruby
|
16
|
+
h.fpath('projects.name')
|
17
|
+
```
|
18
|
+
|
19
|
+
Even with a such simple example you can see that the code is much readable.
|
20
|
+
It was a nice win for my project and at least I'm very happy with it :)
|
21
|
+
|
22
|
+
Check more examples below to see if it can be useful for you too.
|
23
|
+
|
24
|
+
## More Complex Example
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# -----
|
28
|
+
# INITIAL DATA (see usage below)
|
29
|
+
# -----
|
30
|
+
hash = {
|
31
|
+
name: "john",
|
32
|
+
dob: Date.today,
|
33
|
+
projects: [ {name: "A", locations: ["Kyiv"]}, {name: "B", locations: ["Paris", "Berlin"]} ],
|
34
|
+
position: {
|
35
|
+
company: {
|
36
|
+
team: "position1",
|
37
|
+
office: "position2",
|
38
|
+
other: {
|
39
|
+
status: "unknown",
|
40
|
+
notes: ["note a", "note b"],
|
41
|
+
summaries: [
|
42
|
+
{worker: "John", level: "middle"},
|
43
|
+
{worker: "Bob", level: "senior"}
|
44
|
+
]
|
45
|
+
}
|
46
|
+
}
|
47
|
+
},
|
48
|
+
locations: [ { city: "Kyiv", country: "Ukraine" }, { city: "Odessa", country: "Ukraine"}]
|
49
|
+
}
|
50
|
+
|
51
|
+
# -----
|
52
|
+
# USAGE with HASH
|
53
|
+
# -----
|
54
|
+
hash.fpath("name") # => "john"
|
55
|
+
hash.fetch_path("projects.name") # => ["A", "B"]
|
56
|
+
hash.fpath("projects.locations") # => [["Kyiv"], ["Paris", "Berlin"]]
|
57
|
+
hash.fpath!("position.company.other.summaries") # => [{"level" => "middle", "worker" => "John"}, {"level" => "senior", "worker" => "Bob"}]
|
58
|
+
hash.fetch_path!("position.company.other.status") # => "unknown"
|
59
|
+
hash.fpath("position.company.team+office") # => {"team" => "position1", "office" => "position2"}
|
60
|
+
hash.fpath("not_exising_name") # => nil
|
61
|
+
hash.fpath("not_exising_projects.not_exising_name") # => nil
|
62
|
+
hash.fpath("not_exising_projects.not_exising_name", default: 42) # => 42
|
63
|
+
|
64
|
+
# USAGE with ARRAY
|
65
|
+
array = [{name: "igor"}, {name: "john"}]
|
66
|
+
array.fpath("name") # => ["igor", "john"]
|
67
|
+
|
68
|
+
array = [{user: {first_name: "john"}}, {user: {first_name: "bob"}}]
|
69
|
+
array.fpath("user.first_name") # => ["john", "bob"]
|
70
|
+
```
|
71
|
+
|
72
|
+
### Methods & Options
|
73
|
+
|
74
|
+
`fpath` or `fetch_path` will return vakue based on path to the value. If nothing - nil.
|
75
|
+
`fpath!` or `fetch_path!` working same way as `fpath` but raise error is some key is not available.
|
76
|
+
|
77
|
+
Available options: `def fpath(key, strict: false, separator: ".", default: nil)`.
|
78
|
+
|
79
|
+
You can also return multiple values, pay attention to the `hash.fpath("position.company.team+office")`. Note that "+" works only for the last key in the queried "path".
|
80
|
+
|
81
|
+
Note that keys in the returned hash are stringified.
|
82
|
+
|
83
|
+
More examples available in the specs.
|
84
|
+
|
85
|
+
## Installation
|
86
|
+
|
87
|
+
Add this line to your application's Gemfile:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
gem 'hasharay_ext'
|
91
|
+
```
|
92
|
+
|
93
|
+
And then execute:
|
94
|
+
|
95
|
+
$ bundle install
|
96
|
+
|
97
|
+
Or install it yourself as:
|
98
|
+
|
99
|
+
$ gem install hasharay_ext
|
100
|
+
|
101
|
+
## Ideas
|
102
|
+
|
103
|
+
- Add support for "*" operator to search in hash/array for needed values.
|
104
|
+
|
105
|
+
## Development
|
106
|
+
|
107
|
+
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.
|
108
|
+
|
109
|
+
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).
|
110
|
+
|
111
|
+
## Contributing
|
112
|
+
|
113
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/igorkasyanchuk/hasharay_ext.
|
114
|
+
|
115
|
+
## License
|
116
|
+
|
117
|
+
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 "hasharay_ext"
|
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
data/lib/hasharay_ext.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/all"
|
4
|
+
require_relative "hasharay_ext/version"
|
5
|
+
|
6
|
+
module HasharayExt
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
module Interface
|
10
|
+
def fpath!(path, separator: ".", default: nil)
|
11
|
+
fpath(path, strict: true, separator: separator, default: default)
|
12
|
+
end
|
13
|
+
alias_method :fetch_path!, :fpath!
|
14
|
+
end
|
15
|
+
|
16
|
+
class Logic
|
17
|
+
attr_reader :data, :strict, :separator, :default
|
18
|
+
|
19
|
+
def initialize(data, strict: true, separator: ".", default: nil)
|
20
|
+
@data = data
|
21
|
+
@strict = strict
|
22
|
+
@separator = separator
|
23
|
+
@default = default
|
24
|
+
end
|
25
|
+
|
26
|
+
def get(path)
|
27
|
+
raise ArgumentError.new("Not specified key") if path.blank?
|
28
|
+
|
29
|
+
object = data.clone
|
30
|
+
tree = path.split(separator)
|
31
|
+
|
32
|
+
tree.each_with_index do |raw, index|
|
33
|
+
raise ArgumentError.new("Not specified key") if raw.blank?
|
34
|
+
|
35
|
+
keywords = raw.split("+")
|
36
|
+
if index == (tree.size - 1) && keywords.size > 1
|
37
|
+
# last iteration
|
38
|
+
case object
|
39
|
+
when Array
|
40
|
+
return keywords.map do |keyword|
|
41
|
+
fetch(object, keyword)
|
42
|
+
end.transpose
|
43
|
+
when Hash
|
44
|
+
return keywords.each_with_object({}) { |e, res|
|
45
|
+
res[e] = fetch(object, e)
|
46
|
+
}
|
47
|
+
end
|
48
|
+
else
|
49
|
+
# every key
|
50
|
+
object = fetch(object, raw)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
object
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def fetch(object, key)
|
59
|
+
case object
|
60
|
+
when Hash
|
61
|
+
e = object.fetch(key, strict ? invalid_key!(key, object) : default)
|
62
|
+
if e.is_a?(Proc)
|
63
|
+
e.call
|
64
|
+
else
|
65
|
+
object = e
|
66
|
+
end
|
67
|
+
when Array
|
68
|
+
object = object.fpath(key, strict: strict, separator: separator)
|
69
|
+
end
|
70
|
+
object
|
71
|
+
end
|
72
|
+
|
73
|
+
def invalid_key!(key, object)
|
74
|
+
proc { raise(ArgumentError.new("Key `#{key}` not found on attribute ##{object} strict: #{strict}, separator: #{separator}")) }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class Array
|
80
|
+
include HasharayExt::Interface
|
81
|
+
|
82
|
+
def fpath(key, strict: false, separator: ".", default: nil)
|
83
|
+
map do |e|
|
84
|
+
e.present? ? e.fpath(key, strict: strict, separator: separator, default: default) : default
|
85
|
+
end
|
86
|
+
end
|
87
|
+
alias_method :fetch_path, :fpath
|
88
|
+
end
|
89
|
+
|
90
|
+
class Hash
|
91
|
+
include HasharayExt::Interface
|
92
|
+
|
93
|
+
def fpath(path, strict: false, separator: ".", default: nil)
|
94
|
+
HasharayExt::Logic.new(clone.deep_stringify_keys, strict: strict, separator: separator, default: default).get(path)
|
95
|
+
end
|
96
|
+
alias_method :fetch_path, :fpath
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hasharay_ext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Igor Kasyanchuk
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-02-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Useful method to fetch data from complex hashes and arrays
|
42
|
+
email:
|
43
|
+
- igorkasyanchuk@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".standard.yml"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- LICENSE.txt
|
54
|
+
- README.md
|
55
|
+
- Rakefile
|
56
|
+
- bin/console
|
57
|
+
- bin/setup
|
58
|
+
- lib/hasharay_ext.rb
|
59
|
+
- lib/hasharay_ext/version.rb
|
60
|
+
homepage: https://github.com/igorkasyanchuk/hasharay_ext
|
61
|
+
licenses:
|
62
|
+
- MIT
|
63
|
+
metadata:
|
64
|
+
homepage_uri: https://github.com/igorkasyanchuk/hasharay_ext
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.0.8
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Useful method to fetch data from complex hashes and arrays
|
84
|
+
test_files: []
|