Module: Connie

Defined in:
lib/connie.rb,
lib/connie/connie.rb,
lib/connie/parser.rb,
lib/dictionaries/geo.rb,
lib/connie/dictionary.rb,
lib/connie/generators.rb,
lib/dictionaries/names.rb,
lib/dictionaries/numbers.rb,
lib/dictionaries/creative.rb

Defined Under Namespace

Modules: Creative, Geo, Names, Numbers Classes: Dictionary, DictionaryGenerator, DictionaryNameNotAllowed, DictionaryNotFound, Parser

Constant Summary collapse

VERSION =
File.exist?('VERSION') ? File.read('VERSION') : ""

Class Method Summary collapse

Class Method Details

.[](dictionary_name) ⇒ Object



12
13
14
# File 'lib/connie/connie.rb', line 12

def self.[] dictionary_name
  @dictionaries[dictionary_name.to_sym] or Dictionary.new(dictionary_name.to_s)
end

.dictionariesObject



10
# File 'lib/connie/connie.rb', line 10

def self.dictionaries;@dictionaries;end

.dictionaries_pathsObject



26
27
28
# File 'lib/connie.rb', line 26

def self.dictionaries_paths
  @dictionaries_paths
end

.digitObject



43
44
45
# File 'lib/connie/connie.rb', line 43

def self.digit
  rand(9)
end

.formats(format, min = 1, max = 0) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/connie/connie.rb', line 47

def self.formats format, min = 1, max = 0
  array = max > 0 ? Array.new(rand(max-min)+min) : Array.new(min)
    
  generator = case format
  when :W then lambda {Connie.letter(:uppercase)}
  when :w then lambda {Connie.letter}
  when :d then lambda {Connie.digit.to_s}
  end
  
  array.map{generator.call}.join
end

.letter(variant = nil) ⇒ Object

Returns a random letter



37
38
39
40
41
# File 'lib/connie/connie.rb', line 37

def self.letter(variant=nil)
  index = rand(26)*2
  index +=1 if variant == :uppercase
  @alphabet[index]
end

.pick_a_line_from(file_path, line_no = false) ⇒ Object

Picks a random line from a text file or a precise line if a number is provided



21
22
23
24
25
26
27
28
29
30
# File 'lib/connie/connie.rb', line 21

def self.pick_a_line_from(file_path, line_no = false)
  File.open file_path, 'r' do |file|
    unless line_no
      file.inject { |choice, line| rand < 1/file.lineno.to_f ? line.strip : choice }
    else
      line = line_no % (file.lineno - 1) # cycles around the file
      file.readlines[line-1].strip
    end
  end
end

.register_dictionary(dictionary) ⇒ Object



16
17
18
# File 'lib/connie/connie.rb', line 16

def self.register_dictionary(dictionary)
  @dictionaries[dictionary.name.to_sym] = dictionary
end

.reload_dictionariesObject



32
33
34
# File 'lib/connie/connie.rb', line 32

def self.reload_dictionaries
  @dictionaries = {}
end