DSL Study in ruby5


We delete gloval valuable.

```ruby:events.rb
@a_target_sale = 0
@j_target_sale = 0
@a_sale = 0
@j_sale = 0

set_target_sales "set target_sales in America" do
  @a_target_sale = 50
end

set_target_sales "set target_sales in Japan" do
  @j_target_sale = 50
end

select_sales "select_sales in America" do
  @a_sale = 50
end

select_sales "select_sales in Japan" do
  @j_sale = 200
end

event "sales is enough in America" do
  @a_target_sale < @a_sale
end

event "sales is enough in Japan" do
  @j_target_sale < @j_sale
end

event "sales is soso in America" do
  @a_target_sale == @a_sale
end

event "sales is soso in Japan" do
  @j_target_sale == @j_sale
end

event "sales is not enough in America" do
  @a_target_sale > @a_sale
end

event "sales is not enough in Japan" do
  @j_target_sale > @j_sale
end
```

```ruby:act.rb
lambda {
  events = []
  setups = []
  target_sales = []

  Kernel.send :define_method, :event do |description, &block|
    events << {:description => description, :condition => block}
  end

  Kernel.send :define_method, :select_sales do |description, &block|
    setups << {:description => description, :condition => block}
  end

  Kernel.send :define_method, :set_target_sales do |description, &block|
    target_sales << {:description => description, :condition => block}
  end

  Kernel.send :define_method, :each_event do |&block|
    events.each do |event|
      puts "----------START event: #{event[:description]}----------"
      block.call event
    end
  end

  Kernel.send :define_method, :each_setup do
    setups.each do |setup|
      puts "#{setup[:description]}"
      setup[:condition].call
    end
  end

  Kernel.send :define_method, :each_target_sale do
    target_sales.each do |target_sale|
      puts "START target_sale: #{target_sale[:description]}"
      target_sale[:condition].call
    end
  end
}.call

load 'events.rb'

each_event do |event|
  each_target_sale
  each_setup
  puts "ALERT: #{event[:description]}" if event[:condition].call
end
```

```bash
ruby act.rb
```

Here are result.

----------START event: sales is enough in America----------
START target_sale: set target_sales in America
START target_sale: set target_sales in Japan
select_sales in America
select_sales in Japan
----------START event: sales is enough in Japan----------
START target_sale: set target_sales in America
START target_sale: set target_sales in Japan
select_sales in America
select_sales in Japan
ALERT: sales is enough in Japan
----------START event: sales is soso in America----------
START target_sale: set target_sales in America
START target_sale: set target_sales in Japan
select_sales in America
select_sales in Japan
ALERT: sales is soso in America
----------START event: sales is soso in Japan----------
START target_sale: set target_sales in America
START target_sale: set target_sales in Japan
select_sales in America
select_sales in Japan
----------START event: sales is not enough in America----------
START target_sale: set target_sales in America
START target_sale: set target_sales in Japan
select_sales in America
select_sales in Japan
----------START event: sales is not enough in Japan----------
START target_sale: set target_sales in America
START target_sale: set target_sales in Japan
select_sales in America
select_sales in Japan