Sha256: 66545ceb990e2be88bec921c10167ffd6bb1abd4186746c2727166cb19bdce09

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'my_chart/helper/string'
require 'my_chart/proto'

module MyChart::Type

  class << self

    include Enumerable

    def concrete type
      chart_class = detect{|klass| class_to_sym(klass) == type }
      raise Exception, "no such chart: #{type}" unless chart_class
      chart_class
    end

    def each &blk
      load_concrete_charts
      custom_charts.each &blk
    end

    def each_sym &blk
      map{ |klass| class_to_sym klass }.each &blk
    end

    def load_concrete_charts
      return if @loaded
      definitions.each do |c|
        class_eval File.read(c)
      end
      @loaded = true
    end

    private

    def class_to_sym klass
      basename(klass).anticapitalize.to_sym
    end

    def basename klass
      klass.name.split(/::/)[-1]
    end

    def definitions
      path = File.expand_path("../charts/*", __FILE__)
      Dir[path]
    end

    def custom_charts
      self.constants.map do |const|
        const_get(const)
      end.select do |klass|
        klass != MyChart::Proto
      end
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
my_chart-0.1.3 lib/my_chart/type.rb