Class: Gamerom::Repo

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

Overview

Repo - Represents a game ROM repository

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Repo

Returns a new instance of Repo.



25
26
27
28
# File 'lib/gamerom/repo.rb', line 25

def initialize(name)
  @name = name
  @repo = Gamerom::RepoAdapters.const_get(name.capitalize)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/gamerom/repo.rb', line 17

def name
  @name
end

Class Method Details

.listObject



19
20
21
22
23
# File 'lib/gamerom/repo.rb', line 19

def self.list
  REPOSITORIES.map do |repo|
    new repo
  end
end

Instance Method Details

#find(platform, game_identifier) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/gamerom/repo.rb', line 34

def find(platform, game_identifier)
  games(platform).find do |game|
    if Integer(game_identifier, exception: false)
      game.id == game_identifier.to_i
    else
      game.name.downcase == game_identifier.downcase
    end
  end
end

#games(platform, options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gamerom/repo.rb', line 44

def games(platform, options = {})
  platform_database = "#{Gamerom::CACHE_DIR}/#{@name}/#{platform}.yml"
  update_database platform unless File.exist? platform_database
  games = YAML.load_file(platform_database).map do |game|
    Game.new(game.merge(platform: platform, repo: self))
  end

  unless options[:region].nil?
    games.select! do |game|
      game.region == options[:region]
    end
  end

  unless options[:keyword].nil?
    games.select! do |game|
      game.name =~ /#{options[:keyword]}/i
    end
  end

  games
end

#install(game, &block) ⇒ Object



30
31
32
# File 'lib/gamerom/repo.rb', line 30

def install(game, &block)
  @repo.install game, &block
end

#platformsObject



66
67
68
# File 'lib/gamerom/repo.rb', line 66

def platforms
  @repo.platforms
end

#regions(platform) ⇒ Object



70
71
72
# File 'lib/gamerom/repo.rb', line 70

def regions(platform)
  games(platform).map(&:region).sort.uniq
end

#to_sObject



74
75
76
# File 'lib/gamerom/repo.rb', line 74

def to_s
  @name
end

#update_database(platform) ⇒ Object



78
79
80
81
82
# File 'lib/gamerom/repo.rb', line 78

def update_database(platform)
  games = @repo.games platform
  FileUtils.mkdir_p("#{Gamerom::CACHE_DIR}/#{@name}")
  File.write("#{Gamerom::CACHE_DIR}/#{@name}/#{platform}.yml", games.to_yaml)
end