module Grably::Core::Configuration
Grably
configuration module. We use jac for configuration @see github.com/vizor-games/jac
Constants
- CONFIGURATION_FILES
Default configuration file names
- ENV_BINCONFIG_KEY
Key where binary configuration stored if any
- ENV_PROFILE_KEY
Key where required profile is placed. To load grably with profile `foo,bar` one need to run `rake mp=foo,bar task1, task2, … taskN`
Public Class Methods
Reads binary configuration as YAML configuration stream so it could be merged with other streams
# File lib/grably/core/configuration.rb, line 60 def bin_config data = hex_to_string ENV[ENV_BINCONFIG_KEY] # Data will be walid YAML string. Trick is ident its contend and # attach to ^top profile [ "^top:\n" + data.split("\n").map { |x| ' ' + x }.join("\n"), ENV_BINCONFIG_KEY ] end
Tells if binary configuration is provided
# File lib/grably/core/configuration.rb, line 79 def bin_config? ENV[ENV_BINCONFIG_KEY] end
Converts hex string representation to plain string @see en.wikipedia.org/wiki/Hexadecimal
# File lib/grably/core/configuration.rb, line 72 def hex_to_string(str) str.each_char.each_slice(2).inject('') do |acc, elem| acc + elem.map(&:chr).inject(&:+).hex.chr end end
Read configuration from configuration files. @praram [String or Array] profile which should be loaded @param [String] dir base directory path for provided files may be nil @return [OpenStruct] resolved profile values
# File lib/grably/core/configuration.rb, line 41 def load(dir: nil, profile: []) profile += (ENV[ENV_PROFILE_KEY] || 'default').split(',') puts 'Loding profile ' + profile.join('/') read(profile, *load_configuration_streams(dir)) end
# File lib/grably/core/configuration.rb, line 47 def load_configuration_streams(dir) # Read all known files streams = CONFIGURATION_FILES .map { |f| [dir ? File.join(Dir.pwd, f) : f, f] } .select { |path, _name| File.exist?(path) } .map { |path, name| [IO.read(path), name] } streams << bin_config if bin_config? streams end
Generates configuration object for given profile and list of streams with YAML document @param profile [Array] list of profile names to merge @param streams [Array] list of YAML documents and their names to read @return [OpenStruct] instance which contains all resolved profile fields
# File lib/grably/core/configuration.rb, line 31 def read(profile, *streams) obj = Jac::Configuration.read(profile, *streams) obj.extend(self) obj end