Sha256: e92cc173bc270ce49dff8ac55621d30b0992e716dd529ca73ecff7bf2795c993

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

require "content_spinning/core_ext/string"
require "content_spinning/sentence"
require "content_spinning/spinner"
require "content_spinning/string"

class ContentSpinning

  class << self

    def spin(source, limit: nil)
      new(source).spin(limit: limit)
    end

  end

  def initialize(source)
    @source = source
  end

  attr_reader :source

  def count
    parse.count
  end

  SPIN_END = "}"
  SPIN_OR = "|"
  SPIN_START = "{"

  def parse
    return @root if defined?(@root)

    heap = [Sentence.new]

    source.scan(/ [{}|] | [^{}|]+ /x).each do |part|
      current = heap.last

      if part == SPIN_START
        spinner = ::ContentSpinning::Spinner.new
        current << spinner
        heap << spinner

        sentence = ::ContentSpinning::Sentence.new
        spinner << sentence
        heap << sentence
      elsif part == SPIN_OR
        heap.pop
        spinner = heap.last
        sentence = ::ContentSpinning::Sentence.new
        spinner << sentence
        heap << sentence
      elsif part == SPIN_END
        heap.pop
        heap.pop
      else
        current << ::ContentSpinning::String.new(part)
      end
    end

    @root = heap.first.cleaned
  end

  def spin(limit: nil)
    if limit && limit < count
      spin_with_limit(limit: limit)
    else
      spin_all
    end
  end

  def spin_all
    parse.spin
  end

  def spin_with_limit(limit:)
    parsed = parse

    results = Array.new(limit)
    results.map! do
      parsed.random
    end
  end

  def to_source
    parse.to_source
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
content_spinning-0.3.0 lib/content_spinning.rb