Module: Sabrina::Config

Defined in:
lib/sabrina/config.rb

Overview

This utility module handles the Sabrina configuration. You should not need to deal with it directly, except perhaps for ad-hoc runtime config loading.

Each key present in the config can also be called as a module method, for example: Sabrina::Config.rom_defaults.

Upon the first load of the library, a directory called .sabrina should have been created in your home directory (a level above My Documents in Windows XP, or your Cygwin home if you are running Cygwin). This directory should contain a sample.json config file.

Any .json files in the directory except sample.json will be automatically loaded and merged into the default config. Any config file may contain only some of the necessary keys as long as the key hierarchy tree is preserved.

The most obvious use of the above would likely be to supply the library with easily distributable config files for specific ROMs.

Keep in mind that user config files will not be auto-updated on library updates. New versions might break support for old features or add new ones. If you run into errors, try moving the files away from .sabrina and let the library generate a new sample config, then look at it to see what has changed.

Constant Summary collapse

USER_CONFIG_DIR =

The user config directory.

Dir.home + '/.sabrina/'
USER_CONFIG_SAMPLE =

A sample config file to create. This will not actually be loaded and should not be modified.

'sample.json'

Class Method Summary collapse

Class Method Details

.create_user_config0

Creates USER_CONFIG_DIR, and USER_CONFIG_SAMPLE if the directory is empty.

Returns:

  • (0)


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sabrina/config.rb', line 68

def create_user_config
  FileUtils.mkpath(USER_CONFIG_DIR) unless Dir.exist?(USER_CONFIG_DIR)

  c = @sabrina_config
  c.delete_if { |key, _val| key.to_s.start_with?('charmap') }

  f = File.new(USER_CONFIG_DIR + USER_CONFIG_SAMPLE, 'w+b')
  f.write(JSON.pretty_generate(c), symbolize_names: true)
  f.close
  0
end

.deep_merge(h1, h2) ⇒ Hash

A simple function for recursive merging of nested hashes.

Parameters:

  • h1 (Hash)
  • h2 (Hash)

Returns:

  • (Hash)


41
42
43
44
45
# File 'lib/sabrina/config.rb', line 41

def deep_merge(h1, h2)
  h1.merge(h2) do |_key, x, y|
    x.is_a?(Hash) && y.is_a?(Hash) ? deep_merge(x, y) : y
  end
end

.load(h) ⇒ 0

Loads a hash into the internal config.

Parameters:

  • h (Hash)

    the option hash to be merged.

Returns:

  • (0)


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sabrina/config.rb', line 51

def load(h)
  @sabrina_config ||= {}
  @sabrina_config = deep_merge(@sabrina_config, h)

  @sabrina_config.each_key do |key|
    m = key.downcase.to_sym
    next if respond_to?(m)
    define_singleton_method(m) { @sabrina_config[key] }
  end

  0
end

.load_user_config0

Loads all .json files from USER_CONFIG_DIR, exempting USER_CONFIG_SAMPLE.

Returns:

  • (0)


84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sabrina/config.rb', line 84

def load_user_config
  files = Dir[USER_CONFIG_DIR + '*.json']

  # create_user_config if files.empty?

  files.each do |x|
    next if x.end_with?(USER_CONFIG_SAMPLE)
    load(JSON.parse(File.read(x)))
  end
  0
end

.rom_params(id) ⇒ Hash

Returns a hash of all config keys for a ROM type identified by the 4-byte id.

Parameters:

  • id (String)

Returns:

  • (Hash)


101
102
103
104
105
106
107
108
109
# File 'lib/sabrina/config.rb', line 101

def rom_params(id)
  defs = @sabrina_config[:rom_defaults]

  params = @sabrina_config[:rom_data].fetch(id.to_sym) do
    fail "Unsupported ROM type: \'#{id}\'."
  end

  defs.merge(params)
end