hash_with_dot_access 1.0.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: f3851671767e52da2777731df70b4a480f35b3c3a748c252017c55775ac28cf8
4
+ data.tar.gz: e332a15a45541e8ed07ae2af6a95268b537df5b82028d794729b39ba2f995784
5
+ SHA512:
6
+ metadata.gz: 0c9b5a2ad6e68ead89fbdcde10a1228193fe5fcd00f1f84f3eac26465e189675f47de19d4e87ecb5cc298629731f664f24f5f8ab9e3fa10e33d29c0f055b19aa
7
+ data.tar.gz: b3c7e8d97b4d586276fa7376cfc9326923c0e2ba5ec7b344f9adc85bb7fdc0ad4af4666283df0b5898077721b093f0d252a4d4e95e4d1159f7900cd2ceafb152
@@ -0,0 +1,9 @@
1
+ .DS_Store
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,77 @@
1
+ # Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at
59
+ [jared@whitefusion.io](mailto:jared@whitefusion.io). All complaints will
60
+ be reviewed and investigated and will result in a response that is deemed
61
+ necessary and appropriate to the circumstances. The project team is obligated
62
+ to maintain confidentiality with regard to the reporter of an incident.
63
+ Further details of specific enforcement policies may be posted separately.
64
+
65
+ Project maintainers who do not follow or enforce the Code of Conduct in good
66
+ faith may face temporary or permanent repercussions as determined by other
67
+ members of the project's leadership.
68
+
69
+ ## Attribution
70
+
71
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
72
+ available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct.html](https://www.contributor-covenant.org/version/1/4/code-of-conduct.html)
73
+
74
+ [homepage]: https://www.contributor-covenant.org
75
+
76
+ For answers to common questions about this code of conduct, see
77
+ [https://www.contributor-covenant.org/faq](https://www.contributor-covenant.org/faq)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in hash_with_dot_access.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hash_with_dot_access (1.0.0)
5
+ activesupport (>= 5.0.0, < 7.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.0.3.3)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ zeitwerk (~> 2.2, >= 2.2.2)
16
+ concurrent-ruby (1.1.7)
17
+ i18n (1.8.5)
18
+ concurrent-ruby (~> 1.0)
19
+ minitest (5.14.2)
20
+ rake (12.3.3)
21
+ thread_safe (0.3.6)
22
+ tzinfo (1.2.7)
23
+ thread_safe (~> 0.1)
24
+ zeitwerk (2.4.0)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ bundler
31
+ hash_with_dot_access!
32
+ rake (~> 12.0)
33
+
34
+ BUNDLED WITH
35
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020-present Jared White and Bridgetown contributors
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.
@@ -0,0 +1,100 @@
1
+ # Hash with Dot Access
2
+
3
+ Rails' ActiveSupport gem provides `HashWithIndifferentAccess` which allows you to access hash keys with either strings or symbols. This gem provides `HashWithDotAccess` which subclasses `HashWithIndifferentAccess` and allows you to access or set those keys using dot access (aka methods). It utilizes `method_missing` to access key data if available, and you can also set data using `keyname=`.
4
+
5
+ ## Example
6
+
7
+ ```ruby
8
+ require "hash_with_dot_access"
9
+
10
+ hsh = HashWithDotAccess::Hash.new({a: 1, b: 2, c: "abc"})
11
+ # => {"a"=>1, "b"=>2, "c"=>"abc"}
12
+
13
+ hsh.a
14
+ # => 1
15
+
16
+ hsh.c
17
+ # => "abc"
18
+
19
+ hsh.d = "Indeed!"
20
+ hsh.d
21
+ # => "Indeed!"
22
+
23
+ hsh[:d]
24
+ # => "Indeed!"
25
+
26
+ hsh["d"]
27
+ # => "Indeed!"
28
+
29
+ hsh2 = {test: "dot access"}.with_dot_access
30
+ hsh2.test
31
+ # => "dot access"
32
+
33
+ ## Nested hashes work too! Pairs nicely with lonely operator: &.
34
+
35
+ nested = {a: 1, b: {c: 3}}.with_dot_access
36
+ nested.b.c
37
+ # => 3
38
+
39
+ nested&.d&.e&.f
40
+ # => nil
41
+
42
+ ## You can also set default return values when key is missing
43
+
44
+ hsh = {a: 1, b: 2}.with_dot_access
45
+ hsh.default = 0
46
+ hsh.a
47
+ # => 1
48
+ hsh.x
49
+ # => 0
50
+ ```
51
+
52
+ ## Installation
53
+
54
+ Add this line to your application's Gemfile:
55
+
56
+ ```
57
+ $ bundle add hash_with_dot_access
58
+ ```
59
+
60
+ Then simply require `hash_with_dot_access`:
61
+
62
+ ```ruby
63
+ require "hash_with_dot_access"
64
+ ```
65
+
66
+ ## Caveats
67
+
68
+ As with any Ruby object which provides arbitrary data through dynamic method calls, you may encounter collisions between your key names and existing `Hash` methods. For example:
69
+
70
+ ```ruby
71
+ hsh = {each: "this won't work!"}.with_dot_access
72
+ hsh.each
73
+ # => #<Enumerator: {"each"=>"this won't work!"}:each>
74
+ #
75
+ # Uh oh!
76
+ ```
77
+
78
+ Of course, the easy fix is to simply use standard ways of accessing hash data in these cases:
79
+
80
+ ```ruby
81
+ hsh = {each: "this will work!"}.with_dot_access
82
+ hsh[:each]
83
+ # => "this will work!"
84
+ ```
85
+
86
+ ## Development
87
+
88
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bridgetownrb/hash_with_dot_access.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
97
+
98
+ ## Code of Conduct
99
+
100
+ Everyone interacting in the Hash with Dot Access project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bridgetownrb/hash_with_dot_access/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ require_relative "lib/hash_with_dot_access/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "hash_with_dot_access"
5
+ spec.version = HashWithDotAccess::VERSION
6
+ spec.author = "Bridgetown Team"
7
+ spec.email = "maintainers@bridgetownrb.com"
8
+
9
+ spec.summary = %q{Subclass of HashWithIndifferentAccess which provides dot access via method_missing}
10
+ spec.homepage = "https://github.com/bridgetownrb/hash_with_dot_access"
11
+ spec.license = "MIT"
12
+
13
+ # Specify which files should be added to the gem when it is released.
14
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
15
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "activesupport", [">= 5.0.0", "< 7.0"]
21
+
22
+ spec.add_development_dependency "bundler"
23
+ spec.add_development_dependency "rake", "~> 12.0"
24
+ end
@@ -0,0 +1,60 @@
1
+ require "active_support/core_ext/hash/indifferent_access"
2
+
3
+ module HashWithDotAccess
4
+ class Hash < ActiveSupport::HashWithIndifferentAccess
5
+ def respond_to_missing?(key, *)
6
+ return true if "#{key}".end_with?("=")
7
+
8
+ key?[key]
9
+ end
10
+
11
+ def method_missing(key, *args)
12
+ if "#{key}".end_with?("=")
13
+ self["#{key}".chop] = args.first
14
+ elsif self.key?(key)
15
+ self[key]
16
+ else
17
+ default
18
+ end
19
+ end
20
+
21
+ def update(other_hash)
22
+ if other_hash.is_a? HashWithDotAccess::Hash
23
+ super(other_hash)
24
+ else
25
+ other_hash.to_hash.each_pair do |key, value|
26
+ if block_given? && key?(key)
27
+ value = yield(convert_key(key), self[key], value)
28
+ end
29
+ regular_writer(convert_key(key), convert_value(value))
30
+ end
31
+ self
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def convert_value(value, options = {}) # :doc:
38
+ if value.is_a? ::Hash
39
+ if options[:for] == :to_hash
40
+ value.to_hash
41
+ else
42
+ value.with_dot_access
43
+ end
44
+ elsif value.is_a?(Array)
45
+ if options[:for] != :assignment || value.frozen?
46
+ value = value.dup
47
+ end
48
+ value.map! { |e| convert_value(e, options) }
49
+ else
50
+ value
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ class Hash
57
+ def with_dot_access
58
+ HashWithDotAccess::Hash.new(self)
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module HashWithDotAccess
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_with_dot_access
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bridgetown Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-10-01 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: 5.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '7.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 5.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '7.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '12.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '12.0'
61
+ description:
62
+ email: maintainers@bridgetownrb.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - ".gitignore"
68
+ - CODE_OF_CONDUCT.md
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - hash_with_dot_access.gemspec
75
+ - lib/hash_with_dot_access.rb
76
+ - lib/hash_with_dot_access/version.rb
77
+ homepage: https://github.com/bridgetownrb/hash_with_dot_access
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Subclass of HashWithIndifferentAccess which provides dot access via method_missing
100
+ test_files: []