Sha256: 66c61e224033c1ffd51bdcc820749f5d4b0e3008ef1d202048daf18153cf9821

Contents?: true

Size: 1.53 KB

Versions: 14

Compression:

Stored size: 1.53 KB

Contents

# coding: utf-8
#
module Internals

  #
  #
  module Query

    # A single qualifier.
    #
    class Qualifier # :nodoc:all

      attr_reader :normalized_qualifier, :codes

      #
      #
      # codes is an array.
      #
      def initialize normalized_qualifier, codes
        @normalized_qualifier = normalized_qualifier
        @codes                = codes.map &:to_sym
      end

      # Will overwrite if the key is present in the hash.
      #
      def inject_into hash
        codes.each do |code|
          hash[code] = normalized_qualifier
        end
      end

    end

    # Collection class for qualifiers.
    #
    class Qualifiers # :nodoc:all

      include Singleton

      attr_reader :qualifiers, :normalization_mapping

      delegate :<<, :to => :qualifiers

      #
      #
      def initialize
        @qualifiers = []
        @normalization_mapping = {}
      end
    
      # TODO Spec.
      #
      def self.add name, qualifiers
        instance << Qualifier.new(name, qualifiers)
      end
    
      # Uses the qualifiers to prepare (optimize) the qualifier handling.
      #
      def prepare
        qualifiers.each do |qualifier|
          qualifier.inject_into normalization_mapping
        end
      end

      # Normalizes the given qualifier.
      #
      # Returns nil if it is not allowed, the normalized qualifier if it is.
      #
      # Note: Normalizes.
      #
      def normalize qualifier
        return nil if qualifier.blank?

        normalization_mapping[qualifier.to_sym]
      end

    end
    
  end
  
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
picky-2.1.2 lib/picky/internals/query/qualifiers.rb
picky-2.1.1 lib/picky/internals/query/qualifiers.rb
picky-2.1.0 lib/picky/internals/query/qualifiers.rb
picky-2.0.0 lib/picky/internals/query/qualifiers.rb
picky-2.0.0.pre3 lib/picky/internals/query/qualifiers.rb
picky-2.0.0.pre2 lib/picky/internals/query/qualifiers.rb
picky-2.0.0.pre1 lib/picky/internals/query/qualifiers.rb
picky-1.5.4 lib/picky/internals/query/qualifiers.rb
picky-1.5.3 lib/picky/internals/query/qualifiers.rb
picky-1.5.2 lib/picky/internals/query/qualifiers.rb
picky-1.5.1 lib/picky/internals/query/qualifiers.rb
picky-1.5.0 lib/picky/internals/query/qualifiers.rb
picky-1.4.3 lib/picky/internals/query/qualifiers.rb
picky-1.4.2 lib/picky/internals/query/qualifiers.rb