Class: Gamerom::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/gamerom/game.rb

Overview

Game - Represents a game ROM

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Game

Returns a new instance of Game.



11
12
13
14
15
16
# File 'lib/gamerom/game.rb', line 11

def initialize(attributes = {})
  @tags = []
  attributes.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/gamerom/game.rb', line 9

def id
  @id
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/gamerom/game.rb', line 9

def name
  @name
end

#platformObject

Returns the value of attribute platform.



9
10
11
# File 'lib/gamerom/game.rb', line 9

def platform
  @platform
end

#regionObject

Returns the value of attribute region.



9
10
11
# File 'lib/gamerom/game.rb', line 9

def region
  @region
end

#repoObject

Returns the value of attribute repo.



9
10
11
# File 'lib/gamerom/game.rb', line 9

def repo
  @repo
end

#tagsObject

Returns the value of attribute tags.



9
10
11
# File 'lib/gamerom/game.rb', line 9

def tags
  @tags
end

Instance Method Details

#filenamesObject



18
19
20
21
22
# File 'lib/gamerom/game.rb', line 18

def filenames
  YAML.load_file(state_filename).map do |filename|
    "#{filepath}/#{filename}"
  end
end

#filepathObject



24
25
26
# File 'lib/gamerom/game.rb', line 24

def filepath
  "#{Gamerom::GAME_DIR}/#{repo.name}/#{platform}/#{region}"
end

#installObject



28
29
30
31
32
# File 'lib/gamerom/game.rb', line 28

def install
  repo.install self do |filenames|
    update_state filenames
  end
end

#installed?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/gamerom/game.rb', line 34

def installed?
  File.exist? state_filename
end

#state_filenameObject



38
39
40
# File 'lib/gamerom/game.rb', line 38

def state_filename
  "#{Gamerom::STATE_DIR}/#{repo.name}/#{platform}/#{region}/#{id}"
end

#to_sObject



42
43
44
45
46
47
48
# File 'lib/gamerom/game.rb', line 42

def to_s
  install_status = ''
  install_status = " (#{shell.set_color "installed", :green})" if installed?
  tags = ''
  tags = " - tags: #{tags.join(", ")}" if respond_to?(:tags) && !tags.empty?
  "#{id} - #{name} - #{region}#{install_status}#{tags}"
end

#uninstallObject



50
51
52
53
# File 'lib/gamerom/game.rb', line 50

def uninstall
  FileUtils.rm_rf filenames
  FileUtils.rm_rf state_filename
end

#update_state(filenames) ⇒ Object



55
56
57
58
# File 'lib/gamerom/game.rb', line 55

def update_state(filenames)
  FileUtils.mkdir_p("#{Gamerom::STATE_DIR}/#{repo.name}/#{platform}/#{region}")
  File.write(state_filename, filenames.to_yaml)
end