rubyに大和魂を注入するgemを作成

rubyを初めて3年くらい経つが、プライベートではgemを作ったことがなかったのでお試しでgemを作成してみた。


仕様

rubyは日本で開発されたプログラムということなので、構文自体を日本語化してしまうgemです。

今回はifを実装します。

具体的には下記のように書けるようになります。

# before

if xxx
'if'
elsif yyy
'elsif1'
elsif zzz
'elsif2'
else
'else'
end

# after
もし(xxx).だったら{'if'}
.または(yyy).だったら{'elsif1'}
.または(zzz).だったら{'elsif2'}
.それ以外だったら{'else'}
.


gem作成


gem、bundlerをアップデート

まずはgemとbundlerをアップデートしておきます。

$ gem update --system

$ gem update bundler


テンプレートを生成

テストはrspec、ライセンスはMIT、CODE_OF_CONDUCT生成はYESを選択

$ bundle gem yamatodamashii

Creating gem 'yamatodamashii'...
Do you want to generate tests with your gem?
Type 'rspec' or 'minitest' to generate those test files now and in the future. rspec/minitest/(none): rspec
Do you want to license your code permissively under the MIT license?
This means that any other developer or company will be legally allowed to use your code for free as long as they admit you created it. You can read more about the MIT license at https://choosealicense.com/licenses/mit. y/(n): y
MIT License enabled in config
Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email address is specified as a contact in the generated code of conduct so that people know who to contact in case of a violation. For suggestions about how to enforce codes of conduct, see https://bit.ly/coc-enforcement. y/(n): y
Code of conduct enabled in config
create yamatodamashii/Gemfile
create yamatodamashii/lib/yamatodamashii.rb
create yamatodamashii/lib/yamatodamashii/version.rb
create yamatodamashii/yamatodamashii.gemspec
create yamatodamashii/Rakefile
create yamatodamashii/README.md
create yamatodamashii/bin/console
create yamatodamashii/bin/setup
create yamatodamashii/.gitignore
create yamatodamashii/.travis.yml
create yamatodamashii/.rspec
create yamatodamashii/spec/spec_helper.rb
create yamatodamashii/spec/yamatodamashii_spec.rb
create yamatodamashii/LICENSE.txt
create yamatodamashii/CODE_OF_CONDUCT.md
Initializing git repo in ~/Documents/git/yamatodamashii
Gem 'yamatodamashii' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html


gemspecのTODOを修正


yamatodamashii.gemspec

-  spec.summary       = %q{TODO: Write a short summary, because RubyGems requires one.}

- spec.description = %q{TODO: Write a longer description or delete this line.}
- spec.homepage = "TODO: Put your gem's website or public repo URL here."
+ spec.summary = %q{This injects Yamato Damashii into ruby.}
+ spec.description = %q{This injects Yamato Damashii into ruby.}
+ spec.homepage = "https://github.com/ham0215/yamatodamashii"
spec.license = "MIT"

- # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
- # to allow pushing to a single host or delete this section to allow pushing to any host.
- if spec.respond_to?(:metadata)
- spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
-
- spec.metadata["homepage_uri"] = spec.homepage
- spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
- spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
- else
- raise "RubyGems 2.0 or newer is required to protect against " \
- "public gem pushes."
- end



実装

libとspecを実装。

下記がPUSHしたリポジトリです。

https://github.com/ham0215/yamatodamashii

# spec実行

bundle exec rspec


gemファイル作成

$ gem build yamatodamashii.gemspec

WARNING: description and summary are identical
WARNING: See http://guides.rubygems.org/specification-reference/ for help
Successfully built RubyGem
Name: yamatodamashii
Version: 0.1.0
File: yamatodamashii-0.1.0.gem


rubygemsのアカウントを作成

rubygemsで公開するにはアカウントが必要なので作成する。

https://rubygems.org/


API Key登録

リリース前にAPIKeyを登録しておく

$ curl -u [user_name] https://rubygems.org/api/v1/api_key.yaml > ~/.gem/credentials; chmod 0600 ~/.gem/credentials


リリース

$ rake release

yamatodamashii 0.1.0 built to pkg/yamatodamashii-0.1.0.gem.
Tagged v0.1.0.
Pushed git commits and tags.
Pushed yamatodamashii 0.1.0 to rubygems.org


感想

初めてgemを公開してみましたが、公開方法は様々な記事があるため特に詰まることもなく簡単にできました。

肝心のgemですが、今回はifしか作る時間がなかったので暇な時にもう少し追加すると思います(たぶん)

明日は@kamina_zzzさんの「Rancher のはなし」です。お楽しみに!!