struct-initializer 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 +7 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/Rakefile +12 -0
- data/lib/struct/initializer/autoinclude.rb +3 -0
- data/lib/struct/initializer/version.rb +3 -0
- data/lib/struct/initializer.rb +19 -0
- data/struct-initializer.gemspec +36 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a7fe016ff5c10888c244f9619fcb1df0db9d88b0bd86d8b8bc040568d9e4d457
|
4
|
+
data.tar.gz: a143951c7a8b13c25863cb1258c7907f5af20c90bc57a90c6208929463884efd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 85b7019e5a8e0fb88422e7c3bcb67d7f5788f7ae2d403e1c2d7bf30d3d920c973d368e7d1e46e8cd5f1959f2da233b256a73c9fe34cd0d418b2604e1f077d8c4
|
7
|
+
data.tar.gz: 1429200eb55a06eda679dd7c00fc27d045ddedcb16deda9aba5d50ab3dfcb10152559c7aaa37920275f9211ca4cabae2ef7a87502721fed2a348015127f4db08
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
struct-initializer (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.15.0)
|
10
|
+
minitest-sprint (1.2.1)
|
11
|
+
path_expander (~> 1.1)
|
12
|
+
path_expander (1.1.0)
|
13
|
+
rake (13.0.6)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
arm64-darwin-20
|
17
|
+
x86_64-linux
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
minitest (~> 5.0)
|
21
|
+
minitest-sprint
|
22
|
+
rake (~> 13.0)
|
23
|
+
struct-initializer!
|
24
|
+
|
25
|
+
BUNDLED WITH
|
26
|
+
2.3.11
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Kasper Timm Hansen
|
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,64 @@
|
|
1
|
+
# Struct::Initializer
|
2
|
+
|
3
|
+
Reuse Struct.new's attr_reader and initialize generation in any class.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem and add to the application's Gemfile by executing:
|
8
|
+
|
9
|
+
$ bundle add struct-initializer
|
10
|
+
|
11
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
12
|
+
|
13
|
+
$ gem install struct-initializer
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Ruby's `Struct` class allows auto-generating public `attr_reader`'s and an `initialize` method based on the passed attributes, like so:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class Greeter < Struct.new(:name, :greeting)
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
But this only works with inheritance, so there's certain simple initializers that still have to be written manually. `Struct::Initializer` lifts the concept out of needing inheritance:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
class Greeter
|
28
|
+
extend Struct::Initializer
|
29
|
+
struct :name, :greeting
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Another benefit, you can also have the `attr_reader`'s be marked private, even with keyword arguments:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class Greeter
|
37
|
+
extend Struct::Initializer
|
38
|
+
private struct :name, :greeting, keyword_init: true
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
### Auto-include
|
43
|
+
|
44
|
+
If you want `struct` available on any object automatically, change the require to:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
gem "struct-initializer", require: "struct/initializer/autoinclude"
|
48
|
+
```
|
49
|
+
|
50
|
+
This extends `Object` with the `struct` macro, so it's automatically available on any object and `extend Struct::Initializer` from above isn't needed.
|
51
|
+
|
52
|
+
## Development
|
53
|
+
|
54
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
55
|
+
|
56
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kaspth/struct-initializer.
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Struct::Initializer
|
2
|
+
def struct(*names, keyword_init: false)
|
3
|
+
include Definition.new(names, keyword_init: keyword_init)
|
4
|
+
names
|
5
|
+
end
|
6
|
+
|
7
|
+
class Definition < Module
|
8
|
+
def initialize(names, keyword_init: false)
|
9
|
+
attr_reader(*names)
|
10
|
+
arguments = names.map { "#{_1}#{":" if keyword_init}" }.join(", ")
|
11
|
+
|
12
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
13
|
+
def initialize(#{arguments})
|
14
|
+
#{names.map { "@#{_1} = #{_1}" }.join("\n")}
|
15
|
+
end
|
16
|
+
RUBY
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/struct/initializer/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "struct-initializer"
|
7
|
+
spec.version = Struct::Initializer::VERSION
|
8
|
+
spec.authors = ["Kasper Timm Hansen"]
|
9
|
+
spec.email = ["hey@kaspth.com"]
|
10
|
+
|
11
|
+
spec.summary = "Reuse Struct.new's attr_reader and initialize generation in any class."
|
12
|
+
spec.homepage = "https://github.com/kaspth/struct-initializer"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = ">= 2.7.0"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(__dir__) do
|
23
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
24
|
+
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
spec.add_development_dependency "minitest-sprint"
|
33
|
+
|
34
|
+
# For more information and examples about making a new gem, check out our
|
35
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: struct-initializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kasper Timm Hansen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-04-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest-sprint
|
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
|
+
description:
|
28
|
+
email:
|
29
|
+
- hey@kaspth.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- CHANGELOG.md
|
35
|
+
- Gemfile
|
36
|
+
- Gemfile.lock
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/struct/initializer.rb
|
41
|
+
- lib/struct/initializer/autoinclude.rb
|
42
|
+
- lib/struct/initializer/version.rb
|
43
|
+
- struct-initializer.gemspec
|
44
|
+
homepage: https://github.com/kaspth/struct-initializer
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata:
|
48
|
+
homepage_uri: https://github.com/kaspth/struct-initializer
|
49
|
+
source_code_uri: https://github.com/kaspth/struct-initializer
|
50
|
+
changelog_uri: https://github.com/kaspth/struct-initializer/blob/main/CHANGELOG.md
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: 2.7.0
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubygems_version: 3.2.30
|
67
|
+
signing_key:
|
68
|
+
specification_version: 4
|
69
|
+
summary: Reuse Struct.new's attr_reader and initialize generation in any class.
|
70
|
+
test_files: []
|