Sha256: 9372c28aefac17a536ffa5caadb26ad78ed8ed6912dccdf40f47ef1d04aa7aab

Contents?: true

Size: 1.35 KB

Versions: 13

Compression:

Stored size: 1.35 KB

Contents

# A 'global' config.
#
# Being global, we are restricted to connecting to one NetSuite upstream URL
# per ruby process.
class NSConnector::Config
	@@options = {}
	ArgumentError = Class.new(Exception)
	DEFAULT = {
		:use_threads => true,
		:no_threads => 4
	}

	class << self
		# Read a key stored in @@options: Config[:key]
		def [](key)
			key = key.to_sym
			val = @@options[key]

			val.nil? ? DEFAULT[key] : val
		end

		# Write a key stored in @@options: Config[:key] = 1
		def []=(key, value)
			@@options[key.to_sym] = value
		end

		# Overwrite the current 'global' config with +options+
		def set_config! options
			@@options = {}
			options.each do |k,v|
				@@options[k.to_sym] = v
			end
		end

		# Check if the current config is valid.
		# Returns:
		# true:: if all required keys are supplied
		# Raises:
		# ArgumentError:: if any keys are missing
		def check_valid!
			unless @@options then
				raise NSConnector::Config::ArgumentError,
					'Need a configuration set. '\
					'See: NSConnector::Config.set_config!'
			end

			required = [
				:account_id, 
				:email,
				:password,
				:role,
				:restlet_url
			]

			missing_keys = (required - @@options.keys)
			unless missing_keys.empty?
				raise NSConnector::Config::ArgumentError, 
					'Missing configuration key(s): '\
					"#{missing_keys.join(', ')}"
			end

			# All good
			return true
		end
	end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ns_connector-0.1.0 lib/ns_connector/config.rb
ns_connector-0.0.17 lib/ns_connector/config.rb
ns_connector-0.0.16 lib/ns_connector/config.rb
ns_connector-0.0.15 lib/ns_connector/config.rb
ns_connector-0.0.14 lib/ns_connector/config.rb
ns_connector-0.0.13 lib/ns_connector/config.rb
ns_connector-0.0.12 lib/ns_connector/config.rb
ns_connector-0.0.11 lib/ns_connector/config.rb
ns_connector-0.0.10 lib/ns_connector/config.rb
ns_connector-0.0.9 lib/ns_connector/config.rb
ns_connector-0.0.8 lib/ns_connector/config.rb
ns_connector-0.0.7 lib/ns_connector/config.rb
ns_connector-0.0.6 lib/ns_connector/config.rb