Sha256: 9f4b2f1e2241e5d01c90564ea31b386edf3512dff9e1a7934800b4cb24409266

Contents?: true

Size: 1.9 KB

Versions: 7

Compression:

Stored size: 1.9 KB

Contents

module Faker
  class Hipster < Base
    class << self
      def word
        random_word = translate('faker.hipster.words').sample
        random_word.match(/\s/) ? word : random_word
      end

      def words(num = 3, supplemental = false, spaces_allowed = false)
        resolved_num = resolve(num)
        word_list = (
          translate('faker.hipster.words') +
          (supplemental ? translate('faker.lorem.words') : [])
        )
        word_list = word_list * ((resolved_num / word_list.length) + 1)

        return word_list.shuffle[0, resolved_num] if spaces_allowed
        words = word_list.shuffle[0, resolved_num]
        words.each_with_index { |w, i| words[i] = word if w.match(/\s/) }
      end

      def sentence(word_count = 4, supplemental = false, random_words_to_add = 6)
        words(word_count + rand(random_words_to_add.to_i).to_i, supplemental, true).join(' ').capitalize + '.'
      end

      def sentences(sentence_count = 3, supplemental = false)
        [].tap do |sentences|
          1.upto(resolve(sentence_count)) do
            sentences << sentence(3, supplemental)
          end
        end
      end

      def paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3)
        sentences(resolve(sentence_count) + rand(random_sentences_to_add.to_i).to_i, supplemental).join(' ')
      end

      def paragraphs(paragraph_count = 3, supplemental = false)
        [].tap do |paragraphs|
          1.upto(resolve(paragraph_count)) do
            paragraphs << paragraph(3, supplemental)
          end
        end
      end

    private

      # If an array or range is passed, a random value will be selected.
      # All other values are simply returned.
      def resolve(value)
        case value
        when Array then value[rand(value.size)]
        when Range then rand((value.last+1) - value.first) + value.first
        else value
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
faker-1.6.6 lib/faker/hipster.rb
faker-1.6.5 lib/faker/hipster.rb
faker-1.6.4 lib/faker/hipster.rb
faker-1.6.3 lib/faker/hipster.rb
faker-1.6.2 lib/faker/hipster.rb
faker-1.6.1 lib/faker/hipster.rb
faker-1.6.0 lib/faker/hipster.rb