Sha256: 58df278e4a66769b87c2faede970c543327b88b925830ca01da648db021e4683

Contents?: true

Size: 1.04 KB

Versions: 11

Compression:

Stored size: 1.04 KB

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(__dir__), 'filters'].join('/').freeze

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

    def self.resolve!(filter)
      resolve(filter) || raise(ArgumentError, "Failed to load filter #{filter.inspect}")
    end

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

Version data entries

11 entries across 11 versions & 2 rubygems

Version Path
bibtex-ruby-6.1.0 lib/bibtex/filters.rb
bibtex-ruby-6.0.0 lib/bibtex/filters.rb
bibtex-ruby-5.1.6 lib/bibtex/filters.rb
bibtex-ruby-5.1.5 lib/bibtex/filters.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/bibtex-ruby-5.1.4/lib/bibtex/filters.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/bibtex-ruby-5.1.4/lib/bibtex/filters.rb
bibtex-ruby-5.1.4 lib/bibtex/filters.rb
bibtex-ruby-5.1.3 lib/bibtex/filters.rb
bibtex-ruby-5.1.2 lib/bibtex/filters.rb
bibtex-ruby-5.1.1 lib/bibtex/filters.rb
bibtex-ruby-5.1.0 lib/bibtex/filters.rb