delegate_key 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: 0b13a172ea1a1e209de4d4e64561660ee217d2a0dcebe43db3e80f846d7a7af2
4
+ data.tar.gz: 6300b0d335bd59bd3db3b27fa7efc303920dbeb1e10f564dfbbc632229326599
5
+ SHA512:
6
+ metadata.gz: 457b8cdc5a40d4d51923b5f7d3124c1cb76d1dfdfebf86fd6c5557e346bcd454a42a647a5b08a59726cbc527737a37f8773bf95cd71191c486ba8ca8420dbdfc
7
+ data.tar.gz: 3dc1ce94bf89e6e94436f0bdfefe09acb5f9028641fcaea9bf7ca272084f910842edb3b8458e4338ce2af2484b36008344a6a131bec88b864d556bdf3aa624b1
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage
5
+ /pkg
6
+ /spec/reports
7
+ /spec/examples.txt
8
+ /test/tmp
9
+ /test/version_tmp
10
+ /tmp
11
+
12
+ /.bundle
13
+ /vendor/bundle
14
+ /lib/bundler/man
15
+
16
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant 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, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ 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 ilyas.garaev@flatstack.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
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 delegate_key.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Ilyas Garaev
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,143 @@
1
+ # DelegateKey
2
+
3
+ Provides a `delegate_key` class method to easily create methods which return hash value by key.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'delegate_key'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install delegate_key
20
+
21
+ ## Usage
22
+
23
+ ### Options
24
+
25
+ - `:to` - specifies the target object;
26
+
27
+ - `:prefix` - prefixes the new method with the target name or a custom prefix;
28
+
29
+ - `:private` - if set to true, changes method visibility to private.
30
+
31
+ `delegate_key` method receives one or more method names (specified as symbols or strings) and the name of the target object via the `:to` option (also a symbol or string).
32
+
33
+ ```ruby
34
+ class Foo
35
+ delegate_key :key, to: :hash
36
+
37
+ def hash
38
+ { key: "value" }
39
+ end
40
+ end
41
+
42
+ Foo.new.key # => "value"
43
+ ```
44
+
45
+ If hash key is a string, use string as argument:
46
+
47
+ ```ruby
48
+ class Foo
49
+ delegate_key "key", to: :hash
50
+
51
+ def hash
52
+ { "key" => "value" }
53
+ end
54
+ end
55
+
56
+ Foo.new.key # => "value"
57
+
58
+ class Foo
59
+ delegate_key :key, to: :hash
60
+
61
+ def hash
62
+ { "key" => "value" }
63
+ end
64
+ end
65
+
66
+ Foo.new.key # => nil
67
+ ```
68
+
69
+ Multiple delegates to the same target are allowed:
70
+
71
+ ```ruby
72
+ class Foo
73
+ delegate_key :bar, :baz, to: :hash
74
+
75
+ def hash
76
+ { bar: "value for bar", baz: "value for baz" }
77
+ end
78
+ end
79
+
80
+ Foo.new.bar # => "value for bar"
81
+ Foo.new.baz # => "value for baz"
82
+ ```
83
+
84
+ Delegates can optionally be prefixed using the `:prefix` option. If the value is `true`, the delegate methods are prefixed with the name of the object being delegated to.
85
+
86
+ ```ruby
87
+ class Foo
88
+ delegate_key :key, to: :hash, prefix: true
89
+
90
+ def hash
91
+ { key: "value" }
92
+ end
93
+ end
94
+
95
+ Foo.new.hash_key # => "value"
96
+ Foo.new.key # => NoMethodError: undefined method `key' for #<Foo:0x00007fc2452354b0>
97
+ ```
98
+
99
+ It is also possible to supply a custom prefix:
100
+
101
+ ```ruby
102
+ class Foo
103
+ delegate_key :key, to: :hash, prefix: :custom
104
+
105
+ def hash
106
+ { key: "value" }
107
+ end
108
+ end
109
+
110
+ Foo.new.custom_key # => "value"
111
+ ```
112
+
113
+ The delegated methods are public by default. Pass `private: true` to change that.
114
+
115
+ ```ruby
116
+ class Foo
117
+ delegate_key :key, to: :hash, private: true
118
+
119
+ def hash
120
+ { key: "value" }
121
+ end
122
+ end
123
+
124
+ Foo.new.key # => NoMethodError: private method `key' called for #<Foo:0x00007fc24531dd28>
125
+ ```
126
+
127
+ ## Development
128
+
129
+ 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.
130
+
131
+ 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).
132
+
133
+ ## Contributing
134
+
135
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/delegate_key. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
136
+
137
+ ## License
138
+
139
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
140
+
141
+ ## Code of Conduct
142
+
143
+ Everyone interacting in the DelegateKey project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/delegate_key/blob/master/CODE_OF_CONDUCT.md).
data/bin/ci ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env sh
2
+
3
+ set -e
4
+
5
+ bin/rspec spec
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "delegate_key"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ require "pry"
10
+ Pry.start
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env sh
2
+
3
+ set -e
4
+
5
+ BUNDLE_PATH=".bundle"
6
+
7
+ bundle check --path $BUNDLE_PATH > /dev/null 2>&1 ||
8
+ bundle install --path=$BUNDLE_PATH $BUNDLER_ARGS
@@ -0,0 +1,24 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "delegate_key/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "delegate_key"
7
+ spec.version = DelegateKey::VERSION
8
+ spec.authors = ["Ilyas Garaev"]
9
+ spec.email = ["g.ilyas.ilm@gmail.com"]
10
+
11
+ spec.summary = "Provides a delegate_key class method"
12
+ spec.description = "Provides a delegate_key class method to easily create methods which return hash value by key"
13
+ spec.homepage = "https://github.com/ilyasgaraev/delegate_key"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
17
+ spec.test_files = spec.files.grep(%r{^spec/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.required_ruby_version = ">= 2.1"
21
+
22
+ spec.add_development_dependency "pry"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,33 @@
1
+ class Module
2
+ def delegate_key(*methods, to: nil, prefix: nil, private: nil)
3
+ unless to
4
+ raise ArgumentError, "Delegation needs a target. Supply a keyword argument 'to' (e.g. delegate_key :key, to: :hash)."
5
+ end
6
+
7
+ if prefix == true && /^[^a-z_]/.match?(to)
8
+ raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
9
+ end
10
+
11
+ method_prefix = if prefix
12
+ "#{prefix == true ? to : prefix}_"
13
+ else
14
+ ""
15
+ end
16
+
17
+ location = caller_locations(1, 1).first
18
+ file, line = location.path, location.lineno
19
+
20
+ method_names = methods.map do |method|
21
+ method_def = [
22
+ "def #{method_prefix}#{method}",
23
+ " #{to}.public_send(:[], #{method.inspect})",
24
+ "end"
25
+ ].join ";"
26
+
27
+ module_eval(method_def, file, line)
28
+ end
29
+
30
+ private(*method_names) if private
31
+ method_names
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module DelegateKey
2
+ VERSION = "1.0.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delegate_key
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ilyas Garaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rspec
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: Provides a delegate_key class method to easily create methods which return
42
+ hash value by key
43
+ email:
44
+ - g.ilyas.ilm@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rspec"
51
+ - CODE_OF_CONDUCT.md
52
+ - Gemfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - bin/ci
56
+ - bin/console
57
+ - bin/rspec
58
+ - bin/setup
59
+ - delegate_key.gemspec
60
+ - lib/delegate_key.rb
61
+ - lib/delegate_key/version.rb
62
+ homepage: https://github.com/ilyasgaraev/delegate_key
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '2.1'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubygems_version: 3.0.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Provides a delegate_key class method
85
+ test_files: []