Sha256: b33490fb1a5dd20a8e8db433ab0e4484beffc5c4355c12954d1d2b3114f8efbd

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

require 'uri'

module Rack::PageSpeed::Filters
  class Base
    attr_reader :document, :options
    @@subclasses = []

    def initialize options = {}
      @options = options
    end

    class << self
      def inherited klass
        @@subclasses << klass
      end

      def available_filters
        @@subclasses
      end
      
      def requires_store
        instance_eval do
          def new options = {}
            options[:store] ? super(options) : raise("#{name} requires :store to be specified.")
          end
        end
      end

      def name _name = nil
        _name ? @name = _name : @name ||= underscore(to_s)
      end
      
      def priority _number = nil
        _number ? @priority = _number.to_i : @priority
      end

      private
      def underscore word
        word.split('::').last.
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
      end
    end

    private
    def file_for node
      path = case node.name
      when 'script'
        node['src']
      when 'img'
        node['src']        
      when 'link'
        node['href']
      end
      return false unless path
      path = ::File.join(options[:public], URI.parse(path).path)
      ::File.open path if ::File.exists? path      
    end
  end
  # shortcut
  Rack::PageSpeed::Filter = Base
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
rack-pagespeed-fork-0.1.0 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.1.2 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.1.1 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.1.0 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.0.9 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.0.8 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.0.7 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.0.6 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.0.5 lib/rack/pagespeed/filters/base.rb
rack-pagespeed-1.0.4 lib/rack/pagespeed/filters/base.rb