class SoarEnvironment::Environment

Constants

SOAR_CONFIGURATION_KEYS

Attributes

environment[R]
environment_file[R]

Public Class Methods

new(environment_file = nil) click to toggle source
# File lib/soar_environment.rb, line 15
def initialize(environment_file = nil)
  @environment_file = environment_file
end

Public Instance Methods

load_environment() click to toggle source
# File lib/soar_environment.rb, line 19
def load_environment
  @environment = merge(load_file(@environment_file), load_env)
  raise ArgumentError.new("RACK_ENV not set in environment nor in properties or configuration") if (@environment['RACK_ENV'].nil?) or (@environment['RACK_ENV'].strip == "")
  @environment
end
supplement_with_configuration(configuration, keys = SOAR_CONFIGURATION_KEYS) click to toggle source
# File lib/soar_environment.rb, line 25
def supplement_with_configuration(configuration, keys = SOAR_CONFIGURATION_KEYS)
  raise ArgumentError.new("configuration not provided") if (configuration.nil?)
  raise ArgumentError.new("configuration not valid") if not (configuration.is_a?(Hash))
  keys.each do |key|
    @environment[key] ||= configuration[key]
  end
  @environment
end

Private Instance Methods

load_env() click to toggle source
# File lib/soar_environment.rb, line 41
def load_env
  env = (RUBY_PLATFORM == "java" ? java.lang.System.properties.merge(ENV) : ENV)
  env.to_h
end
load_file(filename) click to toggle source
# File lib/soar_environment.rb, line 46
def load_file(filename)
  if filename
    raise SoarEnvironment::LoadError.new("Failed to load file #{filename} : File does not exist") if not File.exist?(@environment_file)
    stringify_values(::YAML.load_file(filename))
  else
    {}
  end
rescue IOError, SystemCallError, ::Psych::Exception => ex
  raise SoarEnvironment::LoadError.new("Failed to load file #{@environment_file} : #{ex}")
end
merge(*hashes) click to toggle source

Produce a union of hashes, with duplicate keys overriden by the last specified hash to include them.

# File lib/soar_environment.rb, line 37
def merge(*hashes)
  hashes.inject { |m, s| m.merge(s) }
end
stringify_values(hash) click to toggle source
# File lib/soar_environment.rb, line 57
def stringify_values(hash)
  Hash[hash.map{ |k, v| [k, v.to_s] }]
end