Sha256: 48a0ff796315d3dee3224717138eb1c7e15e1eb1999fd18829d50b23a25de893

Contents?: true

Size: 1.15 KB

Versions: 53

Compression:

Stored size: 1.15 KB

Contents

module Picky

  # Collection class for qualifiers.
  #
  class QualifierMapper

    attr_reader :mapping
      
    #
    #
    def initialize categories
      @mapping = {}
      categories.each { |category| add category }
    end

    #
    #
    def add category
      category.qualifiers.each do |qualifier|
        sym_qualifier = qualifier.intern
        Picky.logger.warn %Q{Warning: Qualifier "#{qualifier}" already mapped to category #{mapping[sym_qualifier].identifier} (ambiguous qualifier mapping).} if mapping.has_key? sym_qualifier
        mapping[sym_qualifier] = category
      end
    end

    # Normalizes the given qualifier.
    #
    # Returns nil if it is not allowed, the normalized qualifier if it is.
    #
    def map qualifier
      return nil if qualifier.empty?

      mapping[qualifier.intern]
    end
      
    # Restricts the given categories.
    #
    def restrict user_qualified
      if @restricted
        user_qualified ? @restricted & user_qualified : @restricted 
      else
        user_qualified
      end
    end
    def restrict_to *qualifiers
      @restricted = qualifiers.map { |qualifier| map qualifier }.compact
    end

  end

end

Version data entries

53 entries across 53 versions & 1 rubygems

Version Path
picky-4.12.8 lib/picky/qualifier_mapper.rb
picky-4.12.7 lib/picky/qualifier_mapper.rb
picky-4.12.6 lib/picky/qualifier_mapper.rb
picky-4.12.5 lib/picky/qualifier_mapper.rb
picky-4.12.4 lib/picky/qualifier_mapper.rb
picky-4.12.3 lib/picky/qualifier_mapper.rb
picky-4.12.2 lib/picky/qualifier_mapper.rb
picky-4.12.1 lib/picky/qualifier_mapper.rb
picky-4.12.0 lib/picky/qualifier_mapper.rb
picky-4.11.3 lib/picky/qualifier_mapper.rb
picky-4.11.2 lib/picky/qualifier_mapper.rb
picky-4.11.1 lib/picky/qualifier_mapper.rb
picky-4.11.0 lib/picky/qualifier_mapper.rb