args_to_attrs 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a96c6432fb49be9f8a2d91993fa73bf2a0e523d5af768a296096793472f2e784
4
+ data.tar.gz: a1f27bc79aefd4893a43fff02fbb513dcd7ccdfef11d07a8514e5fb4335cedd8
5
+ SHA512:
6
+ metadata.gz: 1b61a71d67d01c8e129841d0d340e67cb93bbe29d314f27daeaf2a9dddb6fd40f1cefc231ccf763fc45ea1160c57477b4d532b5cc5aef49d8937960d95010174
7
+ data.tar.gz: 85f1321be21b38d87701b7fb4fc755ea7f1fc36bffe78d4fcca770dea93dc73b6a89c21bdb38d31d686119bf0f489329dba1062c00332d5f5796bb311219f0d6
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .vscode
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.1.0 - 2022-03-13
4
+
5
+ Initial implementation
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Vladimir Gorodulin
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,31 @@
1
+ require_relative "lib/args_to_attrs/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "args_to_attrs"
5
+ spec.version = ArgsToAttrs::VERSION
6
+ spec.authors = ["Vladimir Gorodulin"]
7
+ spec.email = ["ru.hostmaster@gmail.com"]
8
+ spec.summary = %q{Set instance attributes from method arguments}
9
+ spec.description = %q{Set instance attributes from method arguments}
10
+ spec.homepage = "https://github.com/gorodulin/args_to_attrs"
11
+ spec.license = "MIT"
12
+
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
14
+
15
+ spec.metadata = {
16
+ "changelog_uri" => "https://github.com/gorodulin/args_to_attrs/CHANGELOG.md",
17
+ "homepage_uri" => spec.homepage,
18
+ "source_code_uri" => "https://github.com/gorodulin/args_to_attrs",
19
+ }
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(bin|spec|config|tmp)/}) }
25
+ end
26
+
27
+ spec.require_paths = ["lib"]
28
+ spec.add_development_dependency "rspec", "~> 3.2"
29
+ spec.add_development_dependency "pry"
30
+ spec.add_development_dependency "guard-rspec", "~> 4.7"
31
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArgsToAttrs
4
+
5
+ VERSION = "0.1.0"
6
+
7
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ArgsToAttrs
4
+
5
+ module InstanceMethods
6
+ def args_to_attrs!(expand_keyrest: false)
7
+ arguments = Array.new
8
+ rest_keyword_args = {}
9
+ receiver.method(self.eval('__method__')).parameters.each do |kind, name|
10
+ case kind
11
+ when :key, :keyreq, :req, :opt
12
+ arguments << name
13
+ when :keyrest
14
+ next unless expand_keyrest
15
+ rest_keyword_args = self.local_variable_get(name)
16
+ arguments = arguments.union(rest_keyword_args.keys)
17
+ end
18
+ end
19
+ assignment_order = block_given? ? yield(arguments.to_a) : arguments
20
+ assignment_order.each do |name|
21
+ value = rest_keyword_args.fetch(name) { self.local_variable_get(name) }
22
+ method_name = :"#{name}="
23
+ if receiver.class.private_method_defined?(method_name) ||
24
+ receiver.class.public_method_defined?(method_name) ||
25
+ receiver.class.protected_method_defined?(method_name)
26
+ receiver.send(method_name, value)
27
+ else
28
+ receiver.instance_variable_set(:"@#{name}", value)
29
+ end
30
+ end
31
+ true
32
+ rescue NameError => e
33
+ raise e unless e.message =~ /wrong local variable name/
34
+
35
+ raise "Argument forwarding is not supported"
36
+ end
37
+ end
38
+ end
39
+
40
+ Binding.include ArgsToAttrs::InstanceMethods unless Binding.include?(ArgsToAttrs::InstanceMethods)
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: args_to_attrs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Gorodulin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-03-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
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
+ - !ruby/object:Gem::Dependency
42
+ name: guard-rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.7'
55
+ description: Set instance attributes from method arguments
56
+ email:
57
+ - ru.hostmaster@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CHANGELOG.md
64
+ - LICENSE
65
+ - args_to_attrs.gemspec
66
+ - lib/args_to_attrs.rb
67
+ - lib/args_to_attrs/version.rb
68
+ homepage: https://github.com/gorodulin/args_to_attrs
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ changelog_uri: https://github.com/gorodulin/args_to_attrs/CHANGELOG.md
73
+ homepage_uri: https://github.com/gorodulin/args_to_attrs
74
+ source_code_uri: https://github.com/gorodulin/args_to_attrs
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.7.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.1.4
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Set instance attributes from method arguments
94
+ test_files: []