u16suzuの blog

日々学んだことのメモブログです。

2016/7/31(Sun)の日記

今日はマクドRuby script 用のDSLを書いて遊んだ。

仕事でよく使うテスト用のスクリプトなんだけど、実行するタスクを引数で指定できるようになっている。

それをDSLで指定できるようにした。

@argv = [ {env: "pro"}, 'login', 'start' ]
@env = "dev"

p ARGV

def debug(a)
  puts "---\n#{a}\n---"
end

def before_all(&block)
  @before_all = block
end

def after_all(&block)
  @after_all = block
end

def env(name, &block)
  @env = @argv.select{ |o| o.is_a?(Hash) }.first[:env]
  yield(block) if @env == name
end

before_all do
  debug 'before_all'
end

after_all do
  debug 'after_all'
end

def task(name, &block)
  @before_all.call if @before_all != nil && @argv.include?("all")
  yield block if block_given? && (@argv.include?( name ) || @argv.include?('all') )
  @after_all.call if @after_all != nil && @argv.include?("all")
end

env 'test' do
  debug 'test'
end

env 'stg' do
  debug 'stg'
end

env 'pro' do
  debug 'pro'
end

task 'login' do
  debug 'login'
end

task 'start' do
  debug 'start'
end

task 'finish' do
  debug 'finish'
end