teyu 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: 87e7a03297458dbcd3b39f42889063698ab00c8e972b9161105073392af77d0a
4
+ data.tar.gz: 3d399d485dba2dabb465f49c023bdeefc18b6cbf4390892240ff21c7ce1fd129
5
+ SHA512:
6
+ metadata.gz: fd7163924f7aefd117dd6f724ed3d0da5b7bcd19af5ad085ca008a1442ef9f3127a829343fbad2a4ac8070d52e9d2304fda97ced1541b9eb19bc6cfb8e0b00e1
7
+ data.tar.gz: 971732d8029ead2666a40a78a071568ceaaedd6325b4805b94c4497fd26c83603a68bc82989169e6b26d86025a1521c23b7e9d27e85c8755c2d25a65c1b99fa5
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ ---
2
+ language: ruby
3
+ sudo: false
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.0
7
+ - 2.3.8
8
+ - 2.4.5
9
+ - 2.5.3
10
+ - 2.6.3
11
+ - ruby-head
12
+ matrix:
13
+ fast_finish: true
14
+
@@ -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 takanamito0928@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 teyu.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 takanamito
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,84 @@
1
+ # Teyu
2
+
3
+ A Ruby class extension for binding initialize method args to instance vars.
4
+ Inspired by [attr_extras](https://github.com/barsoom/attr_extras) gem.
5
+
6
+ <img src="https://user-images.githubusercontent.com/1746111/62459955-977f9900-b7bb-11e9-98c9-b2e474420941.jpg" width="50%">
7
+
8
+ Teyu(手湯) is hand bath at Yugawara station.
9
+ I scratched this gem in Yugawara hot spring hotel.
10
+
11
+ [Yugawara-machi | Yugawara station square maintenance construction was completed!](http://www.town.yugawara.kanagawa.jp.e.td.hp.transer.com/chousei/toshikeikakudoboku/p03787.html)
12
+
13
+ ## Usage
14
+
15
+ `teyu_init` defines `initialize` method and binds args to same name instance variables.
16
+
17
+ ```ruby
18
+ require 'teyu'
19
+
20
+ class Foo
21
+ extend Teyu
22
+ teyu_init :a, :b
23
+
24
+ # Equal to...
25
+ # def initialize(a, b)
26
+ # @a = a
27
+ # @b = b
28
+ # end
29
+ end
30
+
31
+ class Bar
32
+ extend Teyu
33
+ teyu_init :c, :d!
34
+
35
+ # def initialize(c, d:)
36
+ # @c = c
37
+ # @d = d
38
+ # end
39
+ end
40
+
41
+ class Baz
42
+ extend Teyu
43
+ teyu_init :e, f: 'fff'
44
+
45
+ # def initialize(e, f: 'fff')
46
+ # @e = e
47
+ # @f = f
48
+ # end
49
+ end
50
+ ```
51
+
52
+ ## Installation
53
+
54
+ Add this line to your application's Gemfile:
55
+
56
+ ```ruby
57
+ gem 'teyu'
58
+ ```
59
+
60
+ And then execute:
61
+
62
+ $ bundle
63
+
64
+ Or install it yourself as:
65
+
66
+ $ gem install teyu
67
+
68
+ ## Development
69
+
70
+ 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.
71
+
72
+ 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).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/st-tech/teyu. 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.
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
81
+
82
+ ## Code of Conduct
83
+
84
+ Everyone interacting in the Teyu project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/st-tech/teyu/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "teyu"
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__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Teyu
2
+ VERSION = "0.1.0"
3
+ end
data/lib/teyu.rb ADDED
@@ -0,0 +1,91 @@
1
+ require "teyu/version"
2
+
3
+ module Teyu
4
+ class Error < StandardError; end
5
+
6
+ def teyu_init(*arg_names)
7
+ define_initializer = DefineInitializer.new(self, arg_names)
8
+ define_initializer.apply
9
+ end
10
+
11
+ class DefineInitializer
12
+ def initialize(klass, arg_names)
13
+ @klass = klass
14
+ @arg_names = arg_names
15
+ end
16
+
17
+ def apply
18
+ sorter = Teyu::ArgsSorter.new(@arg_names)
19
+
20
+ validate_number_of_req_args = method(:validate_number_of_req_args)
21
+ validate_keyreq_args = method(:validate_keyreq_args)
22
+
23
+ @klass.send(:define_method, :initialize) do |*arg_values|
24
+ validate_number_of_req_args.call(sorter.req_args, arg_values)
25
+ validate_keyreq_args.call(sorter.keyreq_args, arg_values)
26
+
27
+ sorter.req_args.zip(arg_values).each do |name, value|
28
+ instance_variable_set(:"@#{name}", value)
29
+ end
30
+
31
+ sorter.key_args.each do |name, value|
32
+ instance_variable_set(:"@#{name}", value)
33
+ end
34
+
35
+ keyword_arg_values = arg_values[sorter.req_args.length..].find { |value| value.is_a?(Hash) } || {}
36
+ (sorter.keyword_args & keyword_arg_values.keys).each do |name|
37
+ instance_variable_set(:"@#{name}", keyword_arg_values[name])
38
+ end
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def validate_number_of_req_args(arg_names, arg_values)
45
+ req_arg_names_count = arg_names.count
46
+ req_arg_values_count = arg_values.filter { |value| !value.is_a?(Hash) }.count
47
+
48
+ raise ArgumentError, "wrong number of arguments (given #{req_arg_values_count}, expected #{req_arg_names_count})" if req_arg_names_count != req_arg_values_count
49
+ end
50
+
51
+ def validate_keyreq_args(arg_names, arg_values)
52
+ arg_names.each do |name|
53
+ keyword_arg_keys = arg_values.find { |value| value.is_a?(Hash) }&.keys || []
54
+ raise ArgumentError, "(missing keyword: #{name})" unless keyword_arg_keys.include?(name)
55
+ end
56
+ end
57
+ end
58
+
59
+ class ArgsSorter
60
+ REQUIRED_SYMBOL = '!'.freeze
61
+
62
+ def initialize(names)
63
+ @names = names
64
+ end
65
+
66
+ # method(a, b) => [:a, :b]
67
+ # @return [Array<Symbol>] req arg names
68
+ def req_args
69
+ @req_args ||= @names.take_while { |arg| !arg.is_a?(Hash) && !arg.to_s.end_with?(REQUIRED_SYMBOL) }
70
+ end
71
+
72
+ # method(a!:, b: 'b') => [:a, :b]
73
+ # @return [Array<Symbol>] keyword arg names
74
+ def keyword_args
75
+ @keyword_args ||= keyreq_args + key_args.keys
76
+ end
77
+
78
+ # method(a!:, b!:) => [:a, :b]
79
+ # @return [Array<Symbol>] keyreq arg names
80
+ def keyreq_args
81
+ @keyreq_args ||= @names.map(&:to_s).filter { |arg| arg.end_with?(REQUIRED_SYMBOL) }
82
+ .map { |arg| arg.delete_suffix(REQUIRED_SYMBOL).to_sym }
83
+ end
84
+
85
+ # method(a: 'a', b: 'b') => { a: 'a', b: 'b' }
86
+ # @return [Hash] keyword args with default value
87
+ def key_args
88
+ @key_args ||= @names.filter { |arg| arg.is_a?(Hash) }&.inject(:merge) || {}
89
+ end
90
+ end
91
+ end
data/teyu.gemspec ADDED
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "teyu/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "teyu"
8
+ spec.version = Teyu::VERSION
9
+ spec.authors = ["Takahiro Kiso (takanamito)", "ZOZO Technologies, Inc."]
10
+ spec.email = ["takanamito0928@gmail.com"]
11
+
12
+ spec.summary = 'A Ruby class extension for binding initialize method args to instance vars.'
13
+ spec.homepage = 'https://github.com/st-tech/teyu'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.17"
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'test-unit'
26
+ spec.add_development_dependency 'power_assert'
27
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: teyu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takahiro Kiso (takanamito)
8
+ - ZOZO Technologies, Inc.
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2019-08-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.17'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.17'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: test-unit
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: power_assert
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description:
71
+ email:
72
+ - takanamito0928@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/teyu.rb
87
+ - lib/teyu/version.rb
88
+ - teyu.gemspec
89
+ homepage: https://github.com/st-tech/teyu
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A Ruby class extension for binding initialize method args to instance vars.
112
+ test_files: []