struct_cast 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +27 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +30 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +8 -0
- data/bin/console +16 -0
- data/bin/setup +8 -0
- data/lib/struct_cast.rb +46 -0
- data/lib/struct_cast/version.rb +5 -0
- data/struct_cast.gemspec +33 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: fa8e0401a478ccfe0eecd3c8263bf8432dfff07ee7b24e52561354af75f57d69
|
4
|
+
data.tar.gz: ad59a4d98fe0883d5f0be7dd305aafca0d51b60575ae3e7c243dd83c467ebc68
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf140424f3e8f06b1cb14567cf3cfa20231348caebebdf7ff47dbbf21d9b4723fd81fdddff9cf5bd0a93df55dd0b9ef73656f2b394c02e13ec26e152b4b9e536
|
7
|
+
data.tar.gz: f8b6e536b6ba8457e6f324e8d4ea00a587725f0164a9e4b4acbb4dd9ac6344c3c67e1ce38b6e7be4cb1efa505095a77539b234e56fd42caa6a4653b3dcd1df4d
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Build
|
9
|
+
|
10
|
+
on: [push, pull_request]
|
11
|
+
|
12
|
+
jobs:
|
13
|
+
test:
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
strategy:
|
16
|
+
matrix:
|
17
|
+
ruby: [2.5, 2.6, 2.7]
|
18
|
+
steps:
|
19
|
+
- uses: actions/checkout@v2
|
20
|
+
- name: Set up Ruby
|
21
|
+
uses: ruby/setup-ruby@v1
|
22
|
+
with:
|
23
|
+
ruby-version: ${{ matrix.ruby }}
|
24
|
+
- name: Install dependencies
|
25
|
+
run: bundle install
|
26
|
+
- name: Run tests
|
27
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
|
4
|
+
inherit_mode:
|
5
|
+
merge:
|
6
|
+
- Exclude
|
7
|
+
|
8
|
+
AllCops:
|
9
|
+
NewCops: enable
|
10
|
+
|
11
|
+
Naming/MethodName:
|
12
|
+
IgnoredPatterns:
|
13
|
+
- Struct
|
14
|
+
|
15
|
+
Lint/AmbiguousBlockAssociation:
|
16
|
+
Exclude:
|
17
|
+
- spec/**/*
|
18
|
+
|
19
|
+
Metrics/BlockLength:
|
20
|
+
Exclude:
|
21
|
+
- spec/**/*
|
22
|
+
|
23
|
+
Style/Documentation:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Style/RedundantSelf:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/StringLiterals:
|
30
|
+
EnforcedStyle: double_quotes
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in struct_cast.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 12.0"
|
9
|
+
gem "rspec", "~> 3.0"
|
10
|
+
|
11
|
+
gem "rubocop", ">= 0.92.0"
|
12
|
+
gem "rubocop-performance", ">= 1.8.1"
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Yuji Hanamura
|
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,45 @@
|
|
1
|
+
# StructCast
|
2
|
+
|
3
|
+
[![Build](https://github.com/yujideveloper/struct_cast/workflows/Build/badge.svg)](https://github.com/yujideveloper/struct_cast/actions?query=workflow%3ABuild)
|
4
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/71ddeac3ead5a224e9ed/maintainability)](https://codeclimate.com/github/yujideveloper/struct_cast/maintainability)
|
5
|
+
|
6
|
+
StructCast provides the refinement module that defines the method for casting to a Struct object.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'struct_cast'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install struct_cast
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
``` ruby
|
27
|
+
using StructCast
|
28
|
+
|
29
|
+
Struct({ id: 1, name: "foo" }) #=> #<struct id=1, name="foo">
|
30
|
+
```
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
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.
|
35
|
+
|
36
|
+
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).
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yujideveloper/struct_cast.
|
41
|
+
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "struct_cast"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
ARGV.prepend "--context-mode=1"
|
16
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/struct_cast.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "struct_cast/version"
|
4
|
+
|
5
|
+
module StructCast
|
6
|
+
module CoreExt
|
7
|
+
refine ::Object do
|
8
|
+
def convert_to_hash
|
9
|
+
if self.respond_to? :to_hash
|
10
|
+
self.to_hash
|
11
|
+
elsif self.respond_to? :to_h
|
12
|
+
self.to_h
|
13
|
+
else
|
14
|
+
raise ::TypeError, "can't convert #{self.class.name} to Hash"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
refine ::Hash do
|
20
|
+
def symbolize_keys
|
21
|
+
self.transform_keys do |k|
|
22
|
+
raise ::TypeError, "can't convert key type to Symbol: #{k.class.name}" unless k.respond_to? :to_sym
|
23
|
+
|
24
|
+
k.to_sym
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
private_constant :CoreExt
|
30
|
+
|
31
|
+
[::Kernel, ::Kernel.singleton_class].each do |klass|
|
32
|
+
refine klass do
|
33
|
+
using CoreExt
|
34
|
+
|
35
|
+
def Struct(args)
|
36
|
+
hash = args.convert_to_hash.symbolize_keys
|
37
|
+
|
38
|
+
if hash.empty?
|
39
|
+
::Struct.new(nil, keyword_init: true).new
|
40
|
+
else
|
41
|
+
::Struct.new(*hash.keys, keyword_init: true).new(**hash)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/struct_cast.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/struct_cast/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "struct_cast"
|
7
|
+
spec.version = StructCast::VERSION
|
8
|
+
spec.authors = ["Yuji Hanamura"]
|
9
|
+
spec.email = ["yuji.developer@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = <<~SUMMARY
|
12
|
+
StructCast provides the refinement module that defines the method for casting to a Struct object.
|
13
|
+
SUMMARY
|
14
|
+
spec.description = spec.summary
|
15
|
+
spec.homepage = "https://github.com/yujideveloper/struct_cast"
|
16
|
+
spec.license = "MIT"
|
17
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
18
|
+
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
20
|
+
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
23
|
+
spec.metadata["changelog_uri"] = "https://github.com/yujideveloper/struct_cast/CHANGELOG.md"
|
24
|
+
|
25
|
+
# Specify which files should be added to the gem when it is released.
|
26
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
27
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
28
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: struct_cast
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuji Hanamura
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: StructCast provides the refinement module that defines the method for
|
14
|
+
casting to a Struct object.
|
15
|
+
email:
|
16
|
+
- yuji.developer@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".github/workflows/ruby.yml"
|
22
|
+
- ".gitignore"
|
23
|
+
- ".rspec"
|
24
|
+
- ".rubocop.yml"
|
25
|
+
- ".travis.yml"
|
26
|
+
- CHANGELOG.md
|
27
|
+
- Gemfile
|
28
|
+
- LICENSE.txt
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- bin/console
|
32
|
+
- bin/setup
|
33
|
+
- lib/struct_cast.rb
|
34
|
+
- lib/struct_cast/version.rb
|
35
|
+
- struct_cast.gemspec
|
36
|
+
homepage: https://github.com/yujideveloper/struct_cast
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata:
|
40
|
+
allowed_push_host: https://rubygems.org
|
41
|
+
homepage_uri: https://github.com/yujideveloper/struct_cast
|
42
|
+
source_code_uri: https://github.com/yujideveloper/struct_cast
|
43
|
+
changelog_uri: https://github.com/yujideveloper/struct_cast/CHANGELOG.md
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.5.0
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubygems_version: 3.1.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 4
|
62
|
+
summary: StructCast provides the refinement module that defines the method for casting
|
63
|
+
to a Struct object.
|
64
|
+
test_files: []
|