initialize-with 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c234fd115bbc996ec62a1ddbfa3bf60a57893bf2a2de4c281f0f81fbacc33354
4
+ data.tar.gz: 8b55ca2fd5e8a2373941ac8211f3e9ee3c3849f5ec9e869ae5b0bee84726c9ec
5
+ SHA512:
6
+ metadata.gz: 4d00e4fa7569aed8c9e6173424eaed3f054814bcf7338c9730bdca039fd6c05bfa8b0c53e17c7364f9b2a0784e00835947d52ddceefeac8ddac5f077402d5b31
7
+ data.tar.gz: 90753b26df54c571535d6ab99c30ccae6c41ad1cf5dc66c389b3a2f264d59182e097c03d7d5466b34fd6285450488b7ce6d8cbb4182c2107759cd6e901714839
@@ -0,0 +1,37 @@
1
+ name: Test & lint
2
+ on: [push, pull_request]
3
+
4
+ jobs:
5
+ tests:
6
+ name: Test
7
+ runs-on: ubuntu-latest
8
+
9
+ steps:
10
+ - name: Checkout code
11
+ uses: actions/checkout@v4
12
+ with:
13
+ fetch-depth: '20'
14
+
15
+ - name: Setup Ruby
16
+ uses: ruby/setup-ruby@v1
17
+
18
+ - run: bundle install
19
+
20
+ - name: RSpec
21
+ run: bundle exec rspec
22
+
23
+ lint:
24
+ name: Lint
25
+ runs-on: ubuntu-latest
26
+
27
+ steps:
28
+ - name: Checkout code
29
+ uses: actions/checkout@v4
30
+
31
+ - name: Setup Ruby
32
+ uses: ruby/setup-ruby@v1
33
+
34
+ - run: bundle install
35
+
36
+ - name: Run linter
37
+ run: bundle exec rubocop
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
14
+ *.gem
15
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ inherit_gem:
2
+ rubocop-rails-omakase: rubocop.yml
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.3.4
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.10
7
+ before_install: gem install bundler -v 1.17.2
@@ -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 nicktbarone@gmail.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 initialize_with.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Nicholas Barone
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,98 @@
1
+ # InitializeWith
2
+
3
+ ![ci](https://github.com/rangerscience/initialize_with/actions/workflows/ci.yml/badge.svg)
4
+ [![code-climate](https://codeclimate.com/github/rangerscience/initialize_with.svg)](https://codeclimate.com/github/rangerscience/initialize_with)
5
+
6
+ Utility gem to ease Ruby code development by replacing the common boiler plate:
7
+
8
+ ```ruby
9
+ class MyClass
10
+ def initialize param
11
+ @param = param
12
+ end
13
+ end
14
+ ```
15
+
16
+ with
17
+
18
+ ```ruby
19
+ class MyClass
20
+ include InitializeWith
21
+ initialize_with :param
22
+ end
23
+ ```
24
+
25
+ ## Installation
26
+
27
+ ```gemfile
28
+ gem 'initialize-with'
29
+ ```
30
+ ```ruby
31
+ require "initialize_with"
32
+ ```
33
+
34
+ Note that there is a very stale gem `initialize_with` (underscore instead of dash) and I am not creative with this naming!
35
+
36
+ ## Usage
37
+ The aim is function "as you expect", and otherwise, see the rspec tests for more involved examples.
38
+
39
+ If you were writing:
40
+ ```ruby
41
+ class MyClass
42
+ def initialize foo, bar: 42
43
+ @foo = foo
44
+ @bar = bar
45
+ call_some_function
46
+ end
47
+ end
48
+ ```
49
+
50
+ you can now write:
51
+ ```ruby
52
+ class MyClass
53
+ include InitializeWith
54
+ initialize_with :foo, bar: 42 do
55
+ call_some_function
56
+ end
57
+ end
58
+ ```
59
+
60
+ Additionally, it's safe to invoke multiple times:
61
+ ```ruby
62
+ class MyClass
63
+ include InitializeWith
64
+ initialize_with :foo
65
+ initialize_with bar: 42
66
+ end
67
+ ```
68
+
69
+ ### Inheritance
70
+ Simple inheritance works just fine:
71
+
72
+ ```ruby
73
+ class Parent
74
+ include InitializeWith
75
+ end
76
+
77
+ class MyClass
78
+ initialize_with :foo, bar: 42
79
+ end
80
+ ```
81
+
82
+ But note that there isn't a way to replicate something like `super` - you'll have to do things by hand (see the test suite for more).
83
+
84
+ ## Development
85
+
86
+ `bundle install` to get started, `rspec` for tests, `bin/console` for an IRB that's already `reqiured` the library.
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rangerscience/initialize_with. 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.
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
95
+
96
+ ## Code of Conduct
97
+
98
+ Everyone interacting in the InitializeWith project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rangerscience/initialize_with/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "initialize_with"
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
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "initialize_with/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "initialize-with"
8
+ spec.version = InitializeWith::VERSION
9
+ spec.authors = [ "Nicholas Barone" ]
10
+ spec.email = [ "nicktbarone@gmail.com" ]
11
+
12
+ spec.summary = %q(Sugar to skip the usual @param = param boilerplate in Ruby)
13
+ spec.description = %q(Adds some utility class methods to simplify some common Ruby patterns)
14
+ spec.homepage = "https://github.com/rangerscience/initialize_with"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = [ "lib" ]
25
+
26
+ spec.required_ruby_version = '>= 3.0.0'
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.5"
29
+ spec.add_development_dependency "rake", "~> 13.2"
30
+ spec.add_development_dependency "rspec", "~> 3.13"
31
+ spec.add_development_dependency "debug", "~> 1.6"
32
+ spec.add_development_dependency "rubocop", "~> 1.6"
33
+ spec.add_development_dependency "rubocop-rails-omakase", "~> 1.0"
34
+ end
@@ -0,0 +1,3 @@
1
+ module InitializeWith
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,56 @@
1
+ require "initialize_with/version"
2
+
3
+ module InitializeWith
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ def initialize(...)
9
+ self.class._validate_initialization_parameters!(...)
10
+ self.class._apply_initialization_parameters(self, ...)
11
+ self.class.instance_variable_get(:@_initialize_with_blocks)&.each { |blk| self.instance_exec(&blk) }
12
+ end
13
+
14
+ module ClassMethods
15
+ def initialize_with *args, **kwargs, &blk
16
+ @_initialize_with ||= []
17
+ @_initialize_with += args
18
+ args.each { |name| attr_reader name }
19
+
20
+ @_initialize_with_splat = kwargs.delete(:splat)
21
+ attr_reader @_initialize_with_splat if @_initialize_with_splat
22
+
23
+ @_initialize_with_optional ||= {}
24
+ @_initialize_with_optional.merge! kwargs
25
+ kwargs.each { |name, _default| attr_reader name }
26
+
27
+ @_initialize_with_blocks ||= []
28
+ @_initialize_with_blocks << blk if blk
29
+ end
30
+
31
+ def _validate_initialization_parameters! *args
32
+ @_initialize_with ||= []
33
+ @_initialize_with_optional ||= {}
34
+
35
+ max_args = @_initialize_with.size + @_initialize_with_optional.size
36
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected #{@_initialize_with.size})" unless args.size >= @_initialize_with.size
37
+ raise ArgumentError, "wrong number of arguments (given #{args.size}, expected #{@_initialize_with.size..(max_args)})" if @_initialize_with_splat.nil? && args.size > max_args
38
+ end
39
+
40
+ def _apply_initialization_parameters instance, *args
41
+ # [1, 2, 3 ]
42
+ # [ 4, 5] =>
43
+ # [1, 2, 3, 5]
44
+ names = @_initialize_with + @_initialize_with_optional.keys
45
+ values = args + @_initialize_with_optional.values.drop(args.size - @_initialize_with.size)
46
+
47
+ names.zip(values).each do |name, val|
48
+ instance.instance_variable_set("@#{name}", val)
49
+ end
50
+
51
+ if @_initialize_with_splat
52
+ instance.instance_variable_set("@#{@_initialize_with_splat}", args[names.size..-1])
53
+ end
54
+ end
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: initialize-with
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Barone
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-04 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: '2.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: debug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rails-omakase
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ description: Adds some utility class methods to simplify some common Ruby patterns
98
+ email:
99
+ - nicktbarone@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".github/workflows/ci.yml"
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".ruby-version"
109
+ - ".travis.yml"
110
+ - CODE_OF_CONDUCT.md
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - initialize_with.gemspec
117
+ - lib/initialize_with.rb
118
+ - lib/initialize_with/version.rb
119
+ homepage: https://github.com/rangerscience/initialize_with
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 3.0.0
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.5.16
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Sugar to skip the usual @param = param boilerplate in Ruby
142
+ test_files: []