Sha256: 4be0091d2c6b7ec80785e8116e5bb3ff07c7f9718ee9b481f8beba7bb2fda6b2

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

module Veewee
  class Templates

    attr_accessor :env

    def initialize(env)
      @env = env
      return self
    end

    def [](name)
      result = nil
      valid_paths(env.template_path).each do |template_dir|
        template = Veewee::Template.new(name, File.join(template_dir, name), @env)
        if template.exists?
          result = template
          return result
        end
      end
      return nil
    end

    # Fetch all Templates
    def each(&block)
      templates = Hash.new

      valid_paths(env.template_path).each do |template_dir|

        env.logger.debug("[Template] Searching #{template_dir} for templates")

        subdirs = Dir.glob("#{template_dir}/*")
        subdirs.each do |sub|
          if File.directory?("#{sub}")
            name = sub.sub(/#{template_dir}\//, '')
            template = Veewee::Template.new(name, sub, @env)
            if template.exists?
              env.logger.debug("[Template] template '#{name}' found")
              templates[name] = template
            end
          end
        end
      end

      if templates.length == 0
        env.logger.debug("[Template] no templates found")
      end

      Hash[templates.sort].each(&block)
    end

    private

    # Traverses path to see which exist or not
    # and checks if available
    def valid_paths(paths)
      valid_paths = paths.collect { |path|
        if File.exists?(path) && File.directory?(path)
          env.logger.info "Path #{path} exists"
          File.expand_path(path)
        else
          env.logger.info "Path #{path} does not exist, skipping"
          nil
        end
      }
      return valid_paths.compact
    end

  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
veewee-0.4.5.1 lib/veewee/templates.rb
veewee-0.4.5 lib/veewee/templates.rb
veewee-0.4.5.pre1 lib/veewee/templates.rb
veewee-0.4.4 lib/veewee/templates.rb
veewee-0.4.3 lib/veewee/templates.rb
veewee-0.4.2 lib/veewee/templates.rb
veewee-0.4.1 lib/veewee/templates.rb
veewee-0.4.0 lib/veewee/templates.rb
veewee-0.3.12 lib/veewee/templates.rb
veewee-0.3.11 lib/veewee/templates.rb
veewee-0.3.10 lib/veewee/templates.rb
veewee-0.3.9 lib/veewee/templates.rb
veewee-atlassian-0.3.11 lib/veewee/templates.rb
veewee-0.3.7 lib/veewee/templates.rb