Sha256: f2c3089008d1bd1e0d74a65a9a87037648b887d6348f7aebad32e199ba023d8f

Contents?: true

Size: 1.99 KB

Versions: 132

Compression:

Stored size: 1.99 KB

Contents

module Picky

  module Generators

    module Partial

      # Generates the right substrings for use in the substring strategy.
      #
      class SubstringGenerator

        attr_reader :from, :to

        def initialize from, to
          @from, @to = from, to

          if @to.zero?
            def each_subtoken token, &block
              token.each_subtoken @from, &block
            end
          else
            if @from < 0 && @to < 0
              def each_subtoken token, &block
                token.each_subtoken @from - @to - 1, (0..@to), &block
              end
            else
              def each_subtoken token, &block
                token.each_subtoken @from, (0..@to), &block
              end
            end
          end

        end

      end

      # The subtoken partial strategy.
      #
      # If given "florian"
      # it will index "floria", "flori", "flor", "flo", "fl", "f"
      # (Depending on what the given from value is, the example is with option from: 1)
      #
      class Substring < Strategy

        # The from option signifies where in the symbol it
        # will start in generating the subtokens.
        #
        # Examples:
        #
        # With :hello, and to: -1 (default)
        # * from: 1 # => [:hello, :hell, :hel, :he, :h]
        # * from: 4 # => [:hello, :hell]
        #
        # With :hello, and to: -2
        # * from: 1 # => [:hell, :hel, :he, :h]
        # * from: 4 # => [:hell]
        #
        def initialize options = {}
          from = options[:from] || 1
          to   = options[:to]   || -1
          @generator = SubstringGenerator.new from, to
        end

        # Delegator to generator#from.
        #
        def from
          @generator.from
        end

        # Delegator to generator#to.
        #
        def to
          @generator.to
        end

        # Yields each generated partial.
        #
        def each_partial token, &block
          @generator.each_subtoken token, &block
        end

      end

    end

  end

end

Version data entries

132 entries across 132 versions & 1 rubygems

Version Path
picky-4.31.3 lib/picky/generators/partial/substring.rb
picky-4.31.2 lib/picky/generators/partial/substring.rb
picky-4.31.1 lib/picky/generators/partial/substring.rb
picky-4.31.0 lib/picky/generators/partial/substring.rb
picky-4.30.0 lib/picky/generators/partial/substring.rb
picky-4.29.0 lib/picky/generators/partial/substring.rb
picky-4.28.1 lib/picky/generators/partial/substring.rb
picky-4.27.1 lib/picky/generators/partial/substring.rb
picky-4.27.0 lib/picky/generators/partial/substring.rb
picky-4.26.2 lib/picky/generators/partial/substring.rb
picky-4.26.1 lib/picky/generators/partial/substring.rb
picky-4.26.0 lib/picky/generators/partial/substring.rb
picky-4.25.3 lib/picky/generators/partial/substring.rb
picky-4.25.2 lib/picky/generators/partial/substring.rb
picky-4.25.1 lib/picky/generators/partial/substring.rb
picky-4.25.0 lib/picky/generators/partial/substring.rb
picky-4.24.0 lib/picky/generators/partial/substring.rb
picky-4.23.2 lib/picky/generators/partial/substring.rb
picky-4.23.1 lib/picky/generators/partial/substring.rb
picky-4.23.0 lib/picky/generators/partial/substring.rb