class Alchy::Game

Constants

SAVE_FILE
STARTERS

Attributes

discovered[RW]

Public Class Methods

new() click to toggle source
# File lib/alchy/game.rb, line 7
def initialize
  load_or_create_save_file
end

Public Instance Methods

append(element) click to toggle source
# File lib/alchy/game.rb, line 11
def append(element)
  unless discovered.include?(element)
    discovered.push(element)
    save
    read
  end
  return
end
make_with(mother, father) click to toggle source
# File lib/alchy/game.rb, line 27
def make_with(mother, father)
  if allowed?(mother) && allowed?(father)
    os = Offspring.new(mother: mother, father: father)
    append(os.child) if os.create
  else
    puts "You need to find both #{mother} and #{father} first!"
  end
  return
end
show() click to toggle source
# File lib/alchy/game.rb, line 20
def show
  (STARTERS + discovered).each do |element|
    puts element
  end
  return
end

Private Instance Methods

allowed?(element) click to toggle source
# File lib/alchy/game.rb, line 73
def allowed?(element)
  STARTERS.include?(element) || discovered.include?(element)
end
create_empty_save_file() click to toggle source
# File lib/alchy/game.rb, line 67
def create_empty_save_file
  self.discovered = []
  save
  read
end
load_or_create_save_file() click to toggle source
# File lib/alchy/game.rb, line 52
def load_or_create_save_file
  FileUtils.touch(SAVE_FILE)
  safely_load
end
purge() click to toggle source
# File lib/alchy/game.rb, line 47
def purge
  FileUtils.rm(SAVE_FILE)
  load_or_create_save_file
end
read() click to toggle source
# File lib/alchy/game.rb, line 39
def read
  self.discovered = YAML::load_file(SAVE_FILE)
end
safely_load() click to toggle source
# File lib/alchy/game.rb, line 57
def safely_load
  begin
    self.discovered = YAML::load_file(SAVE_FILE)
    create_empty_save_file if discovered.nil? || !discovered
    purge unless discovered.kind_of?(Array)
  rescue
    purge
  end
end
save() click to toggle source
# File lib/alchy/game.rb, line 43
def save
  File.open(SAVE_FILE, 'w') {|f| f.write self.discovered.to_yaml }
end