Sha256: 2a76c97d094a01f14621bb20d68b2e3fed1fa1396a2b1be431d956c2465741f2

Contents?: true

Size: 1.34 KB

Versions: 15

Compression:

Stored size: 1.34 KB

Contents

module Copy
  class Router
    def initialize(path, views)
      @path  = determine_path(path)
      @views = views
    end
    
    def format
      @format ||= if @path.index('.')
        @path.split('.').last.to_sym
      else
        :html
      end
    end
    
    def template_file
      @template_file ||= if file = Dir.glob(File.join(@views, "#{path_with_format}*")).first
        file
      elsif index = Dir.glob(File.join(@views, path_without_format, "index.#{format}*")).first
        index
      end
    end
    
    def renderer
      @renderer ||= template_file.split('.').last.to_sym
    end
    
    def template
      @template ||= template_file.gsub(%r{^#{@views}\/*}, '').gsub(%r{.#{renderer}$}, '').to_sym
    end
    
    def layout
      @layout ||= if format == :html && File.exists?(File.join(@views, "layout.html.#{renderer}"))
        :'layout.html'
      else
        false
      end
    end
    
    def success?
      template_file && renderer && template
    end
    
    private
      def determine_path(path)
        if path == '/'
          '/index'
        else
          # Remove trailing slash, if present
          path.strip.gsub(/\/$/, '')
        end
      end
      
      def path_without_format
        @path.split('.').first
      end

      def path_with_format
        "#{path_without_format}.#{format}"
      end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
copy_no_dependency-0.2.0 lib/copy/router.rb
copy-0.2.0 lib/copy/router.rb
copy-0.1.7 lib/copy/router.rb
copy-0.1.6 lib/copy/router.rb
copy-0.1.5 lib/copy/router.rb
copy-0.1.4 lib/copy/router.rb
copy-0.1.3 lib/copy/router.rb
copy-0.1.2 lib/copy/router.rb
copy-0.1.1 lib/copy/router.rb
copy-0.1.0 lib/copy/router.rb
copy-0.0.41 lib/copy/router.rb
copy-0.0.4 lib/copy/router.rb
copy-0.0.3 lib/copy/router.rb
copy-0.0.2 lib/copy/router.rb
copy-0.0.1 lib/copy/router.rb