Module: RandomText

Defined in:
lib/random_text.rb,
lib/random_text/dictionary.rb,
lib/random_text/random_strings.rb

Defined Under Namespace

Classes: Dictionary, RandomStrings

Constant Summary collapse

OTHER_LOWER =
'абвгдеёжзийклмнопрстуфхцчшщъыьэюя'.scan(/./u)
OTHER_UPPER =
'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'.scan(/./u)
DOWNCASE_TABLE =

Class Method Summary collapse

Class Method Details

.add_dictionary(path) ⇒ Object



14
15
16
17
18
19
# File 'lib/random_text.rb', line 14

def add_dictionary(path)
  const_name = classify(File.basename(path, File.extname(path)))
  dictionary = Dictionary.new(File.read(path))
  self.const_set(const_name, dictionary)
  dictionaries[const_name] = dictionary
end

.classify(s) ⇒ Object



49
50
51
# File 'lib/random_text.rb', line 49

def classify(s)
  s.split('_').map(&:capitalize).join
end

.dictionariesObject



10
11
12
# File 'lib/random_text.rb', line 10

def dictionaries
  @dictionaries ||= {}
end

.downcase(s) ⇒ Object



57
58
59
# File 'lib/random_text.rb', line 57

def downcase(s)
  s.downcase.gsub(/./u){ |c| DOWNCASE_TABLE[c] || c }
end

.run(binary, args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/random_text.rb', line 21

def run(binary, args)
  arg = args.join(' ').strip.downcase
  arg = 'p' if arg.empty?
  if m = arg.match(/^(\d+)?\s*(p|w|s|u)/)
    number = m[1] && m[1].to_i
    dictionary = dictionaries[classify(binary)]
    puts case m[2]
    when 'p'
      number ? dictionary.paragraphs(number) : dictionary.paragraph
    when 's'
      number ? dictionary.sentences(number) : dictionary.sentence
    when 'w'
      number ? dictionary.words(number) : dictionary.word
    when 'u'
      number ? dictionary.uniq_words(number) : dictionary.uniq_words
    end
  else
    abort <<-help
  #{binary} [specifier]
without specifier returns one paragraph
p, paragraph, s, sentence, w, word - one paragraph, sentence, word
X p, X paragraph, X s, X sentence, X w, X word - X paragraphs, sentences, words
u, uniq_words - all words
X u, X uniq_words - X uniq_words
  help
  end
end