Sha256: c9d81a4b3764055f006b4b62ebd13a2644998f5ddc0a2a698ea84456ccde5526

Contents?: true

Size: 1015 Bytes

Versions: 16

Compression:

Stored size: 1015 Bytes

Contents

require 'singleton'

module BibTeX
  class Filter
    include Singleton
    
    class << self
      # Hook called by Ruby if Filter is subclassed
      def inherited(base)
        base.class_eval { include Singleton }
        subclasses << base
      end
    
      # Returns a list of all current Filters
      def subclasses
        @subclasses ||= []
      end
    end
    
    def apply(value)
      value
    end
    
    alias convert apply
    alias << apply
    
  end
  
  module Filters
    LOAD_PATH = [File.expand_path('..', __FILE__), 'filters'].join('/').freeze

    Dir.glob("#{LOAD_PATH}/*.rb").each do |filter|
      require filter
    end

    def self.resolve(filter)
      case
      when filter.respond_to?(:apply)
        filter
      when filter.respond_to?(:to_s)
        klass = Filter.subclasses.detect do |c|
          c.name == filter.to_s || c.name.split(/::/)[-1] =~ /^#{filter}$/i
        end
        klass && klass.instance
      else
        nil
      end      
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
bibtex-ruby-2.3.0 lib/bibtex/filters.rb
bibtex-ruby-2.2.2 lib/bibtex/filters.rb
bibtex-ruby-2.2.1 lib/bibtex/filters.rb
bibtex-ruby-2.2.0 lib/bibtex/filters.rb
bibtex-ruby-2.1.2 lib/bibtex/filters.rb
bibtex-ruby-2.1.1 lib/bibtex/filters.rb
bibtex-ruby-2.1.0 lib/bibtex/filters.rb
bibtex-ruby-2.0.12 lib/bibtex/filters.rb
bibtex-ruby-2.0.11 lib/bibtex/filters.rb
bibtex-ruby-2.0.10 lib/bibtex/filters.rb
bibtex-ruby-2.0.9 lib/bibtex/filters.rb
bibtex-ruby-2.0.8 lib/bibtex/filters.rb
bibtex-ruby-2.0.7 lib/bibtex/filters.rb
bibtex-ruby-2.0.6 lib/bibtex/filters.rb
bibtex-ruby-2.0.5 lib/bibtex/filters.rb
bibtex-ruby-2.0.4 lib/bibtex/filters.rb