class LoremIpsumAmet::Word

Public Class Methods

new(base, words, join_element) click to toggle source
# File lib/lorem_ipsum_amet/word.rb, line 4
def initialize(base, words, join_element)
  @base = base
  @words = words
  @join_element = join_element
end

Public Instance Methods

text() click to toggle source
# File lib/lorem_ipsum_amet/word.rb, line 10
def text
  joined_text.gsub(/\s+\$JOIN\s+/, join_element)
end

Private Instance Methods

base_text() click to toggle source
# File lib/lorem_ipsum_amet/word.rb, line 20
def base_text
  # Using a trick here: putting the string $JOIN to keep the
  # reference to the paragraphs, which in case of \n would get
  # lost when #split would get called
  @base.paragraphs.join(' $JOIN ')
end
join_element() click to toggle source
# File lib/lorem_ipsum_amet/word.rb, line 16
def join_element
  @join_element ||= "\n"
end
joined_text() click to toggle source
# File lib/lorem_ipsum_amet/word.rb, line 31
def joined_text
  text = ([base_text] * times_to_repeat).join(' $JOIN ')
  joins = text.split[0...@words].select { |word| word == '$JOIN' }.size

  words_without_joins = @words + joins
  text.split[0...words_without_joins].join(' ')
end
times_to_repeat() click to toggle source
# File lib/lorem_ipsum_amet/word.rb, line 27
def times_to_repeat
  (@words / base_text.split.size) + 1
end