lib/cog/seed.rb

Summary

Maintainability
A
35 mins
Test Coverage
module Cog
  
  # Template for a class in a target language
  class Seed
    
    include Generator
    
    autoload :Feature, 'cog/seed/feature'
    autoload :Var, 'cog/seed/var'
    
    # @return [String] name of the class
    attr_reader :name
    
    # @return [String,nil] path to the header file
    attr_reader :header_path
    
    # @return [String,nil] name of the scope in which classes generated by this seed will be found
    attr_reader :in_scope
    
    # @api developer
    # @param name [String] name of the class
    def initialize(name)
      @name = name.to_s.camelize.to_ident
      @features = [] # [Feature]
    end

    # @return [String] begin the include guard
    def guard
      x = [@in_scope, @name].compact.collect &:upcase
      include_guard_begin "__COG_SPROUT__#{x.join '_'}_H__"
    end
    
    # @return [Array<Feature>] a sorted list of features
    def features
      @features.sort
    end
    
    # Render the class in the currently active language
    # @param path [String] file system path without the extension, relative to the project root. The extension will be determined based on the currently active language
    # @option opt [String] :language key for the language to use. The language must define a seed extension
    def stamp_class(path, opt={})
      Cog.activate_language opt[:language] do
        l = Cog.active_language
        raise Errors::ActiveLanguageDoesNotSupportSeeds.new :language => l if l.nil? || l.seed_extension.nil?
        
        @in_header = false
        @header_path = if l.seed_header
          "#{path}.#{l.seed_header}"
        end
        stamp "cog/#{l.key}/seed.#{l.seed_extension}", "#{path}.#{l.seed_extension}"
        if l.seed_header
          @in_header = true
          stamp "cog/#{l.key}/seed.#{l.seed_header}", @header_path
        end
      end
    end

    def in_header?
      @in_header
    end

    # Sort by name
    def <=>(other)
      @name <=> other
    end
      
  end
end