Sha256: 507ecf0d72923d7b9509794654e965377c87c4e5de3c506bcd4562dc70971e8e

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

module CabbageDoc
  class Generator
    class Error < StandardError; end
    class InvalidType < Error; end
    class InvalidPriority < Error; end

    PRIORITIES = [:high, :medium, :low].freeze

    class << self
      def inherited(klass)
        all[klass.to_s.split('::').last.downcase.to_sym] = klass
      end

      def tags(value = nil)
        if value.nil?
          @_tags
        else
          @_tags = !!value
        end
      end

      def priority(value = nil)
        if value.is_a?(Symbol)
          raise InvalidPriority, value unless PRIORITIES.include?(value)
          @_priority = value
        else
          @_priority
        end
      end

      def all
        @_all ||= {}
      end

      def supports?(type, what)
        !!find(type).public_send(what)
      end

      def exists?(type)
        all.has_key?(type)
      end

      def perform(type, tag = nil)
        if type == :all
          all.map { |_, klass| klass.new(tag).perform }
        else
          find(type).new(tag).perform
        end
      end

      def find(type)
        klass = all[type]

        raise InvalidType, type unless klass

        klass
      end

      def load!
        Dir.glob(File.join(File.dirname(__FILE__), 'generators', '*.rb')).sort.each do |generator|
          require(generator)
        end
      end
    end

    attr_accessor :tag

    def initialize(tag = nil)
      self.tag = tag
    end

    def perform
      raise NotImplementedError
    end

    protected

    def collection
      @_collection ||= Collection.instance.tap do |collection|
        collection.load!
      end
    end

    def controllers
      @_controllers ||= config.controllers.call
    end

    def config
      @_config ||= Configuration.instance
    end
  end

  Generator.load!
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cabbage_doc-0.1.4 lib/cabbage_doc/generator.rb
cabbage_doc-0.1.3 lib/cabbage_doc/generator.rb
cabbage_doc-0.1.2 lib/cabbage_doc/generator.rb
cabbage_doc-0.1.1 lib/cabbage_doc/generator.rb
cabbage_doc-0.1.0 lib/cabbage_doc/generator.rb
cabbage_doc-0.0.9 lib/cabbage_doc/generator.rb
cabbage_doc-0.0.8 lib/cabbage_doc/generator.rb