Sha256: 4d6c732387abeb403fcf437c97ee2e38e9d03fe5eb15ceeb85a18ad7a25e2f5b

Contents?: true

Size: 1.29 KB

Versions: 15

Compression:

Stored size: 1.29 KB

Contents

module RubiGen
  # A spec knows where a generator was found and how to instantiate it.
  # Metadata include the generator's name, its base path, and the source
  # which yielded it (PathSource, GemPathSource, etc.)
  class Spec
    attr_reader :name, :path, :source

    def initialize(name, path, source)
      @name, @path, @source = name, path, source
    end

    # Look up the generator class.  Require its class file, find the class
    # in ObjectSpace, tag it with this spec, and return.
    def klass
      unless @klass
        require class_file
        @klass = lookup_class
        @klass.spec = self
      end
      @klass
    end

    def class_file
      "#{path}/#{name}_generator.rb"
    end

    def class_name
      "#{name.camelize}Generator"
    end
    
    def usage_file
      "#{path}/USAGE"
    end
    
    def visible?
      File.exists? usage_file
    end

    private
      # Search for the first Class descending from RubiGen::Base
      # whose name matches the requested class name.
      def lookup_class
        ObjectSpace.each_object(Class) do |obj|
          return obj if obj.ancestors.include?(RubiGen::Base) and
                        obj.name.split('::').last == class_name
        end
        raise NameError, "Missing #{class_name} class in #{class_file}"
      end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rubigen-1.0.2 lib/rubigen/spec.rb
rubigen-1.0.3 lib/rubigen/spec.rb
rubigen-1.0.4 lib/rubigen/spec.rb
rubigen-1.0.5 lib/rubigen/spec.rb
rubigen-1.2.1 lib/rubigen/spec.rb
rubigen-1.3.0 lib/rubigen/spec.rb
rubigen-1.0.7 lib/rubigen/spec.rb
rubigen-1.2.0 lib/rubigen/spec.rb
rubigen-1.0.8 lib/rubigen/spec.rb
rubigen-1.0.6 lib/rubigen/spec.rb
rubigen-1.1.0 lib/rubigen/spec.rb
rubigen-1.1.1 lib/rubigen/spec.rb
rubigen-1.2.3 lib/rubigen/spec.rb
rubigen-1.2.2 lib/rubigen/spec.rb
rubigen-1.2.4 lib/rubigen/spec.rb