⚡👼 Enel
Remove duplicate logic that use thor.
🎶 Images
🐤 Before
⚡
1 🐢🐰🐉(1)
2 🐢🐰🐉(2)
3 🐢🐰🐉(3)
4 🐢🐰🐉(4)
🐔 After
⚡👼
1
2
3
4
✨✨
☁️⬇️ Installation
Add this line to your application's Gemfile:
gem 'enel'
And then execute:
$ bundle
Or install it yourself as:
$ gem install enel
📜 Usage
🐭 Sample Case
🐤 Before use
- hoge.rb
require 'thor'
require_relative './commands/hoges'
class Hoge < Thor
desc 'hoge1', 'hoge1'
def hoge1
puts Commands::Hoge1.run
end
desc 'hoge2', 'hoge2'
def hoge2(args1)
puts Commands::Hoge2.run(args1)
end
desc 'hoge3', 'hoge3'
def hoge3(args1, *options)
puts Commands::Hoge3.run(args1, *options)
end
# hoge4, hoge5... more!
end
Hoge.start(ARGV)
- ./commands/hoges.rb
module Commands
class Hoge1
def run
"hoge1"
end
end
class Hoge2
def run(args1)
args1
end
end
class Hoge3
def run(args1, *options)
[args1, *options]
end
end
# hoge4, hoge5... more!
end
- result
$ ruby hoge.rb hoge1
hoge1
$ ruby hoge.rb hoge2 a
a
$ ruby hoge.rb hoge3 a b c
a
b
c
🐔 After use
require 'thor'
require 'active_support/inflector'
require 'enel'
require_relative 'commands/hoges'
class Hoge < ::Thor
extend Enel
desc 'hoge1', 'hoge1'
def hoge1
end
desc 'hoge2', 'hoge2'
def hoge2(args1)
end
desc 'hoge3', 'hoge3'
def hoge3(args1, *options)
end
# hoge4, hoge5... more!
define_call_command(proc { |*args|
command = args.first.to_s.camelize
params = args[1..-1]
puts Object.const_get("Commands::#{command}").new.run(*params)
})
end
Hoge.start(ARGV)
- result
$ ruby hoge.rb hoge1
hoge1
$ ruby hoge.rb hoge2 a
a
$ ruby hoge.rb hoge3 a b c
a
b
c
👬 Contributing 👭
- Fork it ( https://github.com/tbpgr/enel/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request