Class: EivuVideoGameInfo::Models::Game

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Concerns::ActiveRecordable
Defined in:
lib/eivu_video_game_info/models/game.rb

Constant Summary collapse

SLUGIFY_RULES_LIST =

order matters for the array below will be run in order they are defined

[
  REGEX_COUNTRY  = /\(([^)]+)\)/, # replace (value)
  REGEX_MISC_TAG = /\[([^)]+)\]/, # replace [value]
  RULE_THE_MID = ' the '.freeze,
  RULE_THE_START = /^the /,
  RULE_GBS_PLAYER = /gbs player v\d+(\.\d+)? -/,
  RULE_AND = ' and '.freeze,
  RULE_DISNEYS = 'disney\'s'.freeze,
  RULE_SPECIAL_CHARS = /[^a-z0-9]/
].freeze
SLUGIFY_SECONDARY_RULES_LIST =
[
  RULE_GBS_PLAYER,
  REGEX_VERSION = /v\d+(\.\d+)?[a-b]?/
]
LEADING_DIGITS =
/^(\d{4})/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_country(rom_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eivu_video_game_info/models/game.rb', line 33

def extract_country(rom_name)
  case rom_name.scan(REGEX_COUNTRY).flatten&.first
  when 'U'
    'USA'
  when 'J'
    'Japan'
  when 'E'
    'Europe'
  when 'K'
    'Korea'
  when nil
    nil
  else
    'Unknown'
  end
end

.fetch_rom_info(filename) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/eivu_video_game_info/models/game.rb', line 50

def fetch_rom_info(filename)
  format = File.extname(filename).delete('.')
  platform_id = PlatformFormat.find_by(format:)&.platform_id
  return nil if platform_id.nil?

  slugs = [slugify_rom(filename), slugify_rom_xtra(filename)]
  # try to find an exact match of the slug via either slug
  slugs.detect do |slug|
    game = Game.find_by(slug:, platform_id:)
    return game if game.present?

    slug.gsub!(LEADING_DIGITS, '')
    Game.find_by(slug:, platform_id:)
  end

  # if no exact match, return a match if there is only a single partial match
  slugs.detect do |slug|
    matches = Game.where(platform_id:).where('slug like ?', "#{slug}%")
    return matches.first if matches.size == 1

    slug.gsub!(LEADING_DIGITS, '')
    matches = Game.where(platform_id:).where('slug like ?', "#{slug}%")
    return matches.first if matches.size == 1
  end
end

.fetch_rom_info_as_json(filename) ⇒ Object



76
77
78
# File 'lib/eivu_video_game_info/models/game.rb', line 76

def fetch_rom_info_as_json(filename)
  fetch_rom_info(filename)&.as_json
end

.slugify_rom(rom_name) ⇒ Object



93
94
95
# File 'lib/eivu_video_game_info/models/game.rb', line 93

def slugify_rom(rom_name)
  slugify_string(File.basename(rom_name, '.*'))
end

.slugify_rom_xtra(rom_name) ⇒ Object



97
98
99
# File 'lib/eivu_video_game_info/models/game.rb', line 97

def slugify_rom_xtra(rom_name)
  slugify_string_xtra(File.basename(rom_name, '.*'))
end

.slugify_string(string) ⇒ Object



80
81
82
83
84
# File 'lib/eivu_video_game_info/models/game.rb', line 80

def slugify_string(string)
  value = I18n.transliterate(string.dup.downcase.gsub('_', ' '))
  SLUGIFY_RULES_LIST.each { |rule| value.gsub!(rule, '') }
  value
end

.slugify_string_xtra(string) ⇒ Object



86
87
88
89
90
91
# File 'lib/eivu_video_game_info/models/game.rb', line 86

def slugify_string_xtra(string)
  value = I18n.transliterate(string.dup.downcase.gsub('_', ' '))

  (SLUGIFY_SECONDARY_RULES_LIST + SLUGIFY_RULES_LIST).each { |rule| value.gsub!(rule, '') }
  value
end

Instance Method Details

#as_json(options = {}) ⇒ Object



102
103
104
105
106
# File 'lib/eivu_video_game_info/models/game.rb', line 102

def as_json(options = {})
  response = attributes&.symbolize_keys
  response[:platform] = platform.short_name
  response
end