Class: RageFlip::Chaos

Inherits:
Object
  • Object
show all
Defined in:
lib/rage_flip/chaos.rb

Constant Summary collapse

DEFAULT_CHAOS_LEVEL =
10
CHAOS_LEVEL_FILE =
File.expand_path("~/.chaos_level.txt")

Class Method Summary collapse

Class Method Details

.current_chaos_levelObject



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

def self.current_chaos_level
  read_chaos_level
end

.process(text, chaos_level = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/rage_flip/chaos.rb', line 6

def self.process(text, chaos_level = nil)
  chaos_level ||= read_chaos_level

  text.each_char.map do |c|
    combining_chars = rand(1..chaos_level).times.map do
      "%c" % rand(0x300..0x36f)
    end
    [c, combining_chars]
  end.join
end

.read_chaos_levelObject



36
37
38
39
40
41
42
43
# File 'lib/rage_flip/chaos.rb', line 36

def self.read_chaos_level
  if File.exist?(CHAOS_LEVEL_FILE)
    level = File.read(CHAOS_LEVEL_FILE).strip.to_i
    (level > 0) ? level : DEFAULT_CHAOS_LEVEL
  else
    DEFAULT_CHAOS_LEVEL
  end
end

.set_chaos_level(instruction) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rage_flip/chaos.rb', line 17

def self.set_chaos_level(instruction)
  current_level = read_chaos_level

  case instruction
  when "more"
    new_level = current_level + 1
  when "less"
    new_level = [current_level - 1, 1].max # Don't go below 1
  else
    new_level = instruction.to_i
    if new_level <= 0
      return "Error: Chaos level must be a positive number"
    end
  end

  write_chaos_level(new_level)
  "chaos level is now #{new_level}"
end

.write_chaos_level(level) ⇒ Object



45
46
47
# File 'lib/rage_flip/chaos.rb', line 45

def self.write_chaos_level(level)
  File.write(CHAOS_LEVEL_FILE, level.to_s)
end