Sha256: 37aa452496701d7f3a3a661ad8fa1111baee1b3e48a8ff4a3bf691c14134e2e2

Contents?: true

Size: 1.77 KB

Versions: 5

Compression:

Stored size: 1.77 KB

Contents

module Picky

  module Query

    # Describes the combination of a token (the text) and
    # the index (the bundle): [text, index_bundle]
    #
    # A combination is a single part of an allocation:
    # [..., [text2, index_bundle2], ...]
    #
    # An allocation consists of a number of combinations:
    # [[text1, index_bundle1], [text2, index_bundle2], [text3, index_bundle1]]
    #
    class Combination # :nodoc:all

      attr_reader :token,
                  :category

      def initialize token, category
        @token    = token
        @category = category
      end

      # Returns the category's name.
      #
      def category_name
        @category_name ||= category.name
      end

      # Returns the weight of this combination.
      #
      # Note: Caching is most of the time useful.
      #
      def weight
        @weight ||= category.weight(token)
      end

      # Returns an array of ids for the given text.
      #
      # Note: Caching is most of the time useful.
      #
      def ids
        @ids ||= category.ids(token)
      end

      # The identifier for this combination.
      #
      def identifier
        "#{category.bundle_for(token).identifier}:#{token.identifier}"
      end

      # Note: Required for uniq!
      #
      # TODO Ok with category or is the bundle needed?
      #
      def hash
        [token.to_s, category].hash
      end

      # Combines the category names with the original names.
      # [
      #  [:title,    'Flarbl', :flarbl],
      #  [:category, 'Gnorf',  :gnorf]
      # ]
      #
      def to_result
        [category_name, *token.to_result]
      end

      # Example:
      #  "exact title:Peter*:peter"
      #
      def to_s
        "#{category.bundle_for(token).identifier} #{to_result.join(':')}"
      end

    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
picky-3.6.10 lib/picky/query/combination.rb
picky-3.6.9 lib/picky/query/combination.rb
picky-3.6.8 lib/picky/query/combination.rb
picky-3.6.7 lib/picky/query/combination.rb
picky-3.6.6 lib/picky/query/combination.rb