Sha256: d4611001e77e5fc1bc2af390167acd75be4900a5915bee0f8bb61a6251e336df

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module EnterRockstar
  module Generator
    # poetic number generator
    class Poetic
      STRATEGIES = {
        'random' => '_random'
      }.freeze

      attr_reader :tokens, :amount, :strategy

      def initialize(data_file:, amount: 5, strategy: 'random')
        @tokens = JSON.parse EnterRockstar::Utils.load_json(data_file)
        @amount = Integer(amount) rescue 5
        @strategy = STRATEGIES[strategy] || '_random'
      end

      def number(num)
        # split the number into parts
        array = num.to_s.split(/\B|\b/)

        all_results = []
        @amount.times do
          result = send(@strategy, array)
          all_results.push result.join(' ')
        end

        all_results
      end

      private

      def _random(array)
        result = []
        array.each do |digit|
          if digit == '.'
            result << '.'
            next
          end

          # digits less than 4 should use longer words
          digit = digit.to_i < 4 ? (digit.to_i + 10).to_s : digit
          result << @tokens[digit].sample
        end

        result
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
enter-rockstar-0.2.1 lib/enter_rockstar/generator/poetic.rb
enter-rockstar-0.2 lib/enter_rockstar/generator/poetic.rb