tree_reject 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c1ffe46be0f2a714ce8f7c6411dca2cce60394f61e309f00f3ecbf21fdca46bf
4
+ data.tar.gz: 5b85c983a4d6f86f71ed1cdf372feb4c5d0d0ff7df247ac3b98c2617ff783a73
5
+ SHA512:
6
+ metadata.gz: 31c899b6343cf3c0db651d90fbc545b03757053827f8993fbbbabcfebb59a19de3179879280d88266bc463388cd2bf06738e5f55d4dd4fb3c7dd3d450630491e
7
+ data.tar.gz: 1aca2595815757c63e752892d88fe69d161c81a0ee7476f77d72684486c503ef9c20c93981e0add37626f9b82b1a677c5346a87a6e04f85422209e0a93500778
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.iml
@@ -0,0 +1,13 @@
1
+ PreCommit:
2
+ RuboCop:
3
+ enabled: true
4
+ required: false
5
+ on_warn: fail
6
+
7
+ HardTabs:
8
+ enabled: true
9
+ required: false
10
+
11
+ CommitMsg:
12
+ TrailingPeriod:
13
+ enabled: false
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,11 @@
1
+ inherit_gem:
2
+ salsify_rubocop: conf/rubocop.yml
3
+
4
+ AllCops:
5
+ TargetRubyVersion: 2.4
6
+ Exclude:
7
+ - 'vendor/**/*'
8
+ - 'gemfiles/vendor/**/*'
9
+
10
+ Style/FrozenStringLiteralComment:
11
+ Enabled: true
@@ -0,0 +1 @@
1
+ tree_reject
@@ -0,0 +1 @@
1
+ ruby-2.5.3
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.5.3
4
+ before_install: gem install bundler -v 1.12.4
5
+ script:
6
+ - bundle exec rubocop
7
+ - bundle exec rspec
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
6
+
7
+ ## Unreleased
8
+
9
+ ## 0.1.0 - 2019-03-28
10
+ ### Added
11
+ - Initial version
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+
4
+ source 'https://rubygems.org'
5
+
6
+
7
+ # override the :github shortcut to be secure by using HTTPS
8
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}.git" }
9
+
10
+ # Specify your gem's dependencies in tree_reject.gemspec
11
+ gemspec
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Salsify, Inc
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.
22
+
@@ -0,0 +1,97 @@
1
+ # tree_reject
2
+
3
+ [![Build Status](https://travis-ci.org/salsify/tree_reject.svg?branch=master)](https://travis-ci.org/salsify/tree_reject)
4
+
5
+
6
+ `tree_reject` is a Ruby gem that removes deeply nested keys from Ruby Hashes or hash-like objects.
7
+
8
+ For example if you have the Hash:
9
+
10
+ ```ruby
11
+ hash = {
12
+ a: {
13
+ aa: {
14
+ aaa: 'aaa',
15
+ aab: 'aab'
16
+ }
17
+ },
18
+ b: {
19
+ ba: 'ba'
20
+ }
21
+ }
22
+ ```
23
+
24
+ and `tree_reject` the Hash:
25
+ ```ruby
26
+ {
27
+ a: {
28
+ aa: :aaa
29
+ }
30
+ }
31
+ ```
32
+
33
+ your new hash will be:
34
+ ```ruby
35
+ {
36
+ a: {
37
+ aa: {
38
+ aab: 'aab'
39
+ }
40
+ },
41
+ b: {
42
+ ba: 'ba'
43
+ }
44
+ }
45
+ ```
46
+
47
+ ## Installation
48
+
49
+ Add this line to your application's Gemfile:
50
+
51
+ ```ruby
52
+ gem 'tree_reject'
53
+ ```
54
+
55
+ And then execute:
56
+
57
+ $ bundle
58
+
59
+ Or install it yourself as:
60
+
61
+ $ gem install tree_reject
62
+
63
+ ## Usage
64
+
65
+ `tree_reject` extends Ruby's built-in `Hash`:
66
+
67
+ ```ruby
68
+ my_hash.tree_reject(ignored_keys)
69
+ ```
70
+
71
+ It is also available for objects that support `to_h`.
72
+
73
+ ```ruby
74
+ TreeReject.tree_reject(my_hash_like, ignored_keys)
75
+ ```
76
+
77
+ ## Development
78
+
79
+ After checking out the repo, run `bin/setup` to install dependencies. Then,
80
+ run `rake spec` to run the tests. You can also run `bin/console` for an
81
+ interactive prompt that will allow you to experiment.
82
+
83
+ To install this gem onto your local machine, run `bundle exec rake install`.
84
+
85
+ To release a new version, update the version number in `version.rb`, and then
86
+ run `bundle exec rake release`, which will create a git tag for the version,
87
+ push git commits and tags, and push the `.gem` file to
88
+ [rubygems.org](https://rubygems.org)
89
+ .
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at
94
+ https://github.com/salsify/tree_reject.## License
95
+
96
+ The gem is available as open source under the terms of the
97
+ [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+
6
+ require 'rspec/core/rake_task'
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'tree_reject'
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -v
5
+
6
+ bundle update
7
+
8
+ overcommit --install
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tree_reject/version'
4
+
5
+ module TreeReject
6
+ extend self
7
+
8
+ module TreeRejectHash
9
+ def tree_reject(ignorned_keys)
10
+ TreeReject.tree_reject(self, ignorned_keys)
11
+ end
12
+ end
13
+
14
+ # returns a new map with the ignored keys removed
15
+ # ignored_keys must be an array of IKItems
16
+ # IKItem is the type <Symbol | Array<IkItem> | Hash<Symbol, IKItem>>
17
+ # keys are only removed if their path in the trees match
18
+ def tree_reject(map, ignored_keys)
19
+ ignored_leaves = extract_leaves(ignored_keys)
20
+ ignored_subtrees = extract_subtrees(ignored_keys, ignored_leaves)
21
+
22
+ map.to_h.each_with_object({}) do |(k, v), cleaned|
23
+ if ignored_leaves.include?(k)
24
+ next
25
+ elsif v.is_a?(Hash) || v.respond_to?(:attributes)
26
+ cleaned_v = tree_reject(v.to_h, ignored_subtrees[k])
27
+ cleaned[k] = cleaned_v unless cleaned_v == {}
28
+ elsif !v.nil?
29
+ cleaned[k] = v
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ # extract the top level leaves from the ignored_keys tree structure
37
+ def extract_leaves(ignored_keys)
38
+ [ignored_keys].flatten.reject { |ignored_key| ignored_key.is_a?(Hash) }
39
+ end
40
+
41
+ # extract the top level subtrees from the ignored_keys tree structure, skipping ones included ignored_leaves
42
+ def extract_subtrees(ignored_keys, ignored_leaves)
43
+ [ignored_keys].flatten.select { |ignored_key| ignored_key.is_a?(Hash) }.inject({}) do |h, ignored_subtree|
44
+ ignored_subtree.each_pair do |k, v|
45
+ next if ignored_leaves.include?(k)
46
+
47
+ if h.key?(k)
48
+ h[k] << v
49
+ else
50
+ h[k] = [v]
51
+ end
52
+ end
53
+ h
54
+ end
55
+ end
56
+ end
57
+
58
+ Hash.send(:include, TreeReject::TreeRejectHash)
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TreeReject
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'tree_reject/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'tree_reject'
9
+ spec.version = TreeReject::VERSION
10
+ spec.authors = ['Salsify, Inc']
11
+ spec.email = ['engineering@salsify.com']
12
+
13
+ spec.summary = 'Remove deeply nested keys from hash.'
14
+ spec.description = spec.summary
15
+ spec.homepage = 'https://github.com/salsify/tree_reject'
16
+
17
+ spec.license = 'MIT'
18
+
19
+
20
+ # Set 'allowed_push_post' to control where this gem can be published.
21
+ if spec.respond_to?(:metadata)
22
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
23
+
24
+ else
25
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
26
+ end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ spec.bindir = 'bin'
30
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
31
+ spec.require_paths = ['lib']
32
+
33
+ spec.required_ruby_version = '>= 2.4'
34
+
35
+ spec.add_development_dependency 'bundler', '~> 1.12'
36
+ spec.add_development_dependency 'overcommit'
37
+ spec.add_development_dependency 'rake', '~> 10.0'
38
+ spec.add_development_dependency 'rspec', '~> 3.4'
39
+
40
+ spec.add_development_dependency 'salsify_rubocop', '~> 0.60.0'
41
+
42
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tree_reject
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Salsify, Inc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: overcommit
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
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: salsify_rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.60.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.60.0
83
+ description: Remove deeply nested keys from hash.
84
+ email:
85
+ - engineering@salsify.com
86
+ executables:
87
+ - console
88
+ - setup
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".overcommit.yml"
94
+ - ".rspec"
95
+ - ".rubocop.yml"
96
+ - ".ruby-gemset"
97
+ - ".ruby-version"
98
+ - ".travis.yml"
99
+ - CHANGELOG.md
100
+ - Gemfile
101
+ - LICENSE.txt
102
+ - README.md
103
+ - Rakefile
104
+ - bin/console
105
+ - bin/setup
106
+ - lib/tree_reject.rb
107
+ - lib/tree_reject/version.rb
108
+ - tree_reject.gemspec
109
+ homepage: https://github.com/salsify/tree_reject
110
+ licenses:
111
+ - MIT
112
+ metadata:
113
+ allowed_push_host: https://rubygems.org
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '2.4'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubygems_version: 3.0.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Remove deeply nested keys from hash.
133
+ test_files: []