#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
# グラフ描画ライブラリAlGraphサンプル
#

require 'al_template'
require './line_plot'
require './bar_plot'
require './pie_chart'

class AlController

  # LinePlot, BarPlot, PieChartの定義済みメソッド draw_sample_XX に対して
  # アクション action_line_XX, action_bar_XX, action_pie_XX を動的に定義
  [
    {:class => LinePlot, :action_prefix => 'action_line_'},
    {:class => BarPlot, :action_prefix => 'action_bar_'},
    {:class => PieChart, :action_prefix => 'action_pie_'}
  ].each do |item|
    item[:class].instance_methods.grep(/^draw_sample_/).each do |draw_method|
      action_method = draw_method.to_s.sub(/draw_/, item[:action_prefix])
      define_method(action_method) do
        graph = item[:class].new  # 例: graph = LinePlot.new
        graph.send(draw_method)   # 例: graph.draw_sample_01
      end
    end
  end

  def action_index
    AlTemplate.run('index.rhtml')
  end

  def action_line_plot
    AlTemplate.run('line_plot.rhtml')
  end

  def action_bar_plot
    AlTemplate.run('bar_plot.rhtml')
  end

  def action_pie_chart
    AlTemplate.run('pie_chart.rhtml')
  end

end
