Sha256: 5079999303d29441f230ca0ebb066d29e352a1da8132cab3a9d20161fe0ab300

Contents?: true

Size: 1.02 KB

Versions: 12

Compression:

Stored size: 1.02 KB

Contents

require 'securerandom'

module FriendlyId

  # This class provides the slug candidate functionality.
  # @see FriendlyId::Slugged
  class Candidates

    include Enumerable

    def initialize(object, *array)
      @object = object
      @candidates = to_candidate_array(object, array.flatten(1))
    end


    # Visits each slug candidate, calls it, passes it to `normalize_friendly_id` and yields the result.
    def each(*args, &block)
      @candidates.each(*args) do |candidate|
        yield @object.normalize_friendly_id(candidate.map(&:call).join(' '))
      end
    end

    private

    def to_candidate_array(object, array)
      array.map do |candidate|
        case candidate
        when String
          [->{candidate}]
        when Array
          to_candidate_array(object, candidate).flatten
        when Symbol
          [object.method(candidate)]
        else
          if candidate.respond_to?(:call)
            [candidate]
          else
            [->{candidate.to_s}]
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
friendly_id-5.0.4 lib/friendly_id/candidates.rb
friendly_id-5.0.3 lib/friendly_id/candidates.rb
friendly_id-5.0.2 lib/friendly_id/candidates.rb
friendly_id-5.0.1 lib/friendly_id/candidates.rb
friendly_id-5.0.0 lib/friendly_id/candidates.rb
friendly_id-5.0.0.rc3 lib/friendly_id/candidates.rb
friendly_id-5.0.0.rc2 lib/friendly_id/candidates.rb
friendly_id-5.0.0.rc1 lib/friendly_id/candidates.rb
friendly_id-5.0.0.beta4 lib/friendly_id/candidates.rb
friendly_id-5.0.0.beta3 lib/friendly_id/candidates.rb
friendly_id-5.0.0.beta2 lib/friendly_id/candidates.rb
friendly_id-5.0.0.beta1 lib/friendly_id/candidates.rb