class Palette

Constants

COMMENT
GAP_BETWEEN_LISTS
PALETTE_TITLE
REPETITION_PREFIX

Public Class Methods

new(raw_text, filename) click to toggle source
# File lib/dunmanifestin/palette.rb, line 24
def initialize raw_text, filename
  @raw_text = raw_text
  @filename = filename
end

Public Instance Methods

churn() click to toggle source
# File lib/dunmanifestin/palette.rb, line 58
def churn
  @churn ||= Churn.new novelty: novelty
end
file_basename() click to toggle source
# File lib/dunmanifestin/palette.rb, line 54
def file_basename
  File.basename(@filename, '.pal')
end
lines() click to toggle source
# File lib/dunmanifestin/palette.rb, line 87
def lines
  @lines ||= text.gsub(/>>(.+?)<</m, '').split("\n") | text.scan(/>>(.+?)<</m).flatten
end
name() click to toggle source
# File lib/dunmanifestin/palette.rb, line 29
def name
  return file_basename if not titled?
  lines.first.sub(/\|(\w+).*$/, '\1')
end
novelty() click to toggle source
# File lib/dunmanifestin/palette.rb, line 62
def novelty
  lines.first.sub(/^.*\*(\d+).*$/, '\1').to_i
end
phrases() click to toggle source
# File lib/dunmanifestin/palette.rb, line 66
def phrases
  @phrases ||= begin
    lines[valid_phrase_range]
      .map { |line| line.sub(COMMENT, '') }
      .map(&:strip)
      .reject(&:empty?)
      .flat_map { |line|
        if line =~ REPETITION_PREFIX
          [line.sub(REPETITION_PREFIX, '')] * line.to_i
        else
          [line]
        end
      }
      .map(&Phrase.method(:new))
  end
end
sample(genre, inflections = [], constraints = []) click to toggle source
# File lib/dunmanifestin/palette.rb, line 34
def sample genre, inflections = [], constraints = []
  if inflections.any?
    # don't use the churn if there are inflections, since
    # the inflected text would be cached by the churn and
    # could then `recur` in places where we don't want any
    # inflections.
    phrases.sample.reify genre, inflections
  else
    if constraints.include? :recur
      churn.sample { phrases.sample.reify genre }
    else
      churn.generate { phrases.sample.reify genre }
    end
  end
rescue NoMethodError
  "{#{name} ??}"
end
text() click to toggle source
# File lib/dunmanifestin/palette.rb, line 91
def text
  @text ||= @raw_text.strip
end
titled?() click to toggle source
# File lib/dunmanifestin/palette.rb, line 95
def titled?
  lines[0][0] == '|'
end
valid_phrase_range() click to toggle source
# File lib/dunmanifestin/palette.rb, line 83
def valid_phrase_range
  titled? ? 1..-1 : 0..-1
end